#!/usr/bin/env bash

if [ -z "$SAGE_LOCAL" ]; then
    echo "Error: SAGE_LOCAL undefined - exiting..."
    echo "Maybe run 'sage -sh'?"
    exit 1
fi

CUR=`pwd`

cd "$CUR/src"

# Apply all patches
for patch in ../patches/*.patch; do
    [ -r "$patch" ] || continue  # Skip non-existing or non-readable patches
    echo "Applying $patch"
    patch -p1 <"$patch"
    if [ $? -ne 0 ]; then
        echo >&2 "Error applying '$patch'"
        exit 1
    fi
done


cd unix


finished()
{
    if [ $? -ne 0 ]; then
        echo "Error building Tachyon Ray Tracer."
        exit 1
    fi

    echo "Installing the Tachyon binary..."
    cd "$CUR"
    if [ "$UNAME" = CYGWIN ]; then
        binary_name=tachyon.exe
    else
        binary_name=tachyon
    fi
    # The '*' is a place-holder for the arch just built (e.g. 'win32' for
    # Cygwin, other directories won't exist):
    cp -p -f src/compile/*/"${binary_name}" "$SAGE_LOCAL/bin"
    if [ $? -ne 0 ]; then
        echo "Error installing Tachyon Ray Tracer binary (\"${binary_name}\")."
        exit 1
    fi
    exit 0 # Necessary, to not execute further parts of the script!
}


if [ "$UNAME" = "CYGWIN" ]; then
    $MAKE win32
    finished
fi

if [ "$UNAME" = "Darwin" ]; then
    # TODO: This has to be (re)checked for current versions (0.98.9+).
    # make macosx-thr
    # The threaded version of the 0.98 beta doesn't work on OS X.
    if [ "$SAGE64" = "yes" ]; then
        echo "Building a 64-bit version of Tachyon on MacOS X."
        $MAKE macosx-64
    else
        $MAKE macosx
    fi
    finished
fi

if [ "$UNAME" = "FreeBSD" ]; then
    # Tachyon doesn't have a threaded option for FreeBSD (though it shouldn't
    # be too difficult to add).  There's no need for an explicit 64-bit
    # version since the only difference is a slight performance optimisation.
    $MAKE bsd
    finished
fi

if [ "$UNAME" = "Linux" ]; then
    case "`uname -m`" in
        i[3456]86)
            $MAKE linux-thr;;
        ia64)
            $MAKE linux-ia64-thr;;
        amd64|x86_64)
            $MAKE linux-64-thr;;
        ppc*|powerpc*)
            $MAKE linux-ppc;;
        armv6l|armv7l)
            sed "s/-m32//g" ../../patches/Make-arch > Make-arch
            $MAKE linux-thr;;
        *) # e.g. SPARC
            echo "Error: Sorry, your platform isn't supported by Tachyon and/or Sage. Exiting..."
            exit 1
    esac
    finished
fi

if [ "$UNAME" = "SunOS" ]; then
    if [ "x$SAGE64" = xyes ]; then
        # There was nothing suitable for 64-bit mode with
        # gcc, so I made a new target. David Kirkby, May 2010.
        echo "Building a 64-bit version of Tachyon on Solaris."
        $MAKE solaris-pthreads-gcc-64-bit
    else
        echo "Building a 32-bit version of Tachyon on Solaris."
        $MAKE solaris-pthreads-gcc
    fi
    finished
fi

if [ "x$UNAME" = xAIX ]; then
    CFLAGS="$CFLAGS -g -O2"
    export CFLAGS
    $MAKE aix-generic-thr
    finished
fi

if [ "x$UNAME" = xHP-UX ]; then
    CFLAGS="$CFLAGS -g -O2"
    export CFLAGS
    $MAKE hpux-generic-thr
    finished
fi


echo "Error: Tachyon build for $UNAME not implemented (for Sage) - please complain on sage-devel."
exit 1
