die () {
    echo "$@"
    exit 1
}

# patching
cd src

chmod -R a+rX .
if [ $UNAME = "SunOS" ]; then
    sage-apply-patches -d solaris || die "Error patching LiE."
fi

# Building LiE in parallel is broken
export MAKE="$MAKE -j1"

# building
$MAKE CC="$CC" || die "Error building LiE. Did you install bison?"

# "install" the LiE package by moving over the complete build
# directory to $SAGE_LOCAL/lib/lie
cd ..
sed -e "s'$PWD/src'$SAGE_LOCAL/lib/LiE'" src/lie > src/lie~
mv src/lie~ src/lie
chmod +x src/lie
rm -rf "$SAGE_LOCAL"/lib/lie # clean up old versions
rm -rf "$SAGE_LOCAL"/bin/lie "$SAGE_LOCAL"/lib/LiE
mv src/lie "$SAGE_LOCAL"/bin/
mv src/ "$SAGE_LOCAL"/lib/LiE
