#!/usr/bin/env bash

DESCRIPTION="\
Description: Fetch, merge, and build the components of the
             LunarGLASS project
"

DEPENDENCIES="\
Dependencies: bash, wget, libffi-dev, flex, bison, git, subversion
"

USAGE="\
Usage: ./install [options] [fetch|merge|build|all]

       Just running ./install with no arguments will fetch, then merge, then
       build the LunarGLASS project. Note for developers: this script will
       destory any uncommitted changes.

       Options:
         -h --help            Print out this Usage info
         --version=<version>  Specify a revision to use

       Commands:
         fetch                Get the code from the Mesa and the LLVM projects
         merge                Merge in LunarGLASS changes
         build                Build LunarGLASS
         all                  [Default] perform a fetch, merge, and build
"

# Main functionality
function fetch {
    echo "Fetching ..."
    echo "Fetching Mesa"
    rm -rf mesa MesaLib-7.10.1* || exit 1
    wget ftp://freedesktop.org/pub/mesa/7.10.1/MesaLib-7.10.1.tar.bz2 || exit 1
    tar -xjf MesaLib-7.10.1.tar.bz2 || exit 1
    mv Mesa-7.10.1 mesa || exit 1
    rm MesaLib-7.10.1* || exit 1
    echo "Mesa fetched"
    echo
    echo "Fetching LLVM"
    pushd Core
    rm -rf LLVM
    mkdir LLVM
    pushd LLVM
    wget http://llvm.org/releases/2.9/llvm-2.9.tgz || exit 1
    tar -zxf llvm-2.9.tgz || exit 1
    rm llvm-2.9.tgz || exit 1
    popd
    popd
    echo "LLVM fetched"
    echo "Fetching done"
    echo
}

function merge {
    echo "Merging LunarGLASS changes ..."

    # Undo the effect of having checked out LunarGLASS before
    find . -name ".svn" | while read i; do rm -rf $i; done

    svn checkout -r $VERSION --force https://lunarglass.googlecode.com/svn/trunk/ . || exit 1

    # Revert all the directories (but leave the scripts intact to preserve local changes)
    find ./* -prune -type d -exec svn revert --depth=infinity {} \+ || exit 1
    echo "Merging done"
    echo
}

function build {
    echo "Building ..."
    echo "Building LLVM, this may take a while"
    mkdir -p Core/LLVM/llvm-2.9/build || exit 1
    pushd Core/LLVM/llvm-2.9/build
    ../configure || exit 1
    make || exit 1
    make install DESTDIR=`pwd`/install || exit 1
    popd
    echo "LLVM built"
    echo
    echo "Building LunarGLASS"
    pushd Standalone
    make || exit 1
    install StandAlone ../tools/LunarGLASS || exit 1
    popd
    echo "LunarGLASS built"
    echo
    echo "Building done, binary located at tools/LunarGLASS"
    echo "Tests located in test"
    echo "Example run: ./tools/LunarGLASS test/test.frag"
}

function all {
    fetch
    merge
    build
}

function showHelp {
    echo "$DESCRIPTION"
    echo "$DEPENDENCIES"
    echo "$USAGE"
}

VERSION="HEAD"

# Command-line argument Handling

# No arguments passed
if [[ -z "$*" ]]; then all; fi;

# Arguments passed
for i in $*; do
    case $i in
        fetch)
            fetch
            ;;
        merge)
            merge
            ;;
        build)
            build
            ;;
        all)
            all
            ;;
        -h|--h)
            showHelp
            exit 0
            ;;
        --version=*)
            VERSION=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
            ;;
        *)
            showHelp
            exit 1
            ;;
    esac
done

echo "Done"

