#!/usr/bin/env bash

cd src

# Apply patches
for patch in ../patches/*.patch; do
    patch -p1 <"$patch"
    if [ $? -ne 0 ]; then
        echo >&2 "Error applying '$patch'"
        exit 1
    fi
done

# use 2to3 if we are running on python3
if python -c 'import sys; sys.exit(sys.version_info.major != 3)'; then
    2to3 -n -w --no-diffs pexpect.py
fi

# On some systems pexpect doesn't upgrade correctly without
# first removing the old version.
rm -f "$SAGE_LOCAL"/lib/python*/site-packages/pexpect.*

python setup.py install

if [ $? -ne 0 ]; then
    echo "Error installing pexpect"
    exit 1
fi
