#!/bin/bash
# This is a script that will download and install all ptsg codes in double precision to /usr/local/
# edit `prefix' and `precision' to change this
# note that changing the prefix will break `make' for codes other than xgrafix/xoopic, 
# so you would have to tweak their makefiles accordingly

prefix=/usr/local
precision=double
force=false

msg(){
    echo "###################  $@  ####################"
}
succeed(){
    msg "$@ SUCCESSFULLY INSTALLED"
}
build(){
    msg "BUILDING $@"
}
already(){
    msg "$@ ALREADY INSTALLED"
}

mkdir xgbuild
cd xgbuild || exit 1
working=`pwd`

# get dependencies:

install="sudo apt-get -y install"
msg "UPDATING SYSTEM"
sudo apt-get update && sudo apt-get -y dist-upgrade

msg "INSTALLING DEPENDENCIES"
$install gcc g++ build-essential automake \
    libpng-dev tk-dev imagemagick \
    bison openmpi-bin openmpi-dev libx11-dev libxpm-dev fftw-dev  libfftw3-dev\
    h5utils hdf5-tools libhdf5-serial-dev

xgfile="xgrafix-2.70.3.tar.gz"
xofile="xoopic-2.70.2.tar.gz"
allfile="ptsgcodes.tar.gz"

msg "FETCHING CODES"
wget http://ptsg.berkeley.edu/pub/codes/$allfile

cd $working
# xgrafix
code=xgrafix
if [ $force != false ] || [ ! -f "$prefix/bin/xtest" ]; then
    msg "FETCHING XGRAFIX"
    wget http://ptsg.berkeley.edu/pub/codes/xgrafix/$xgfile
    tar -xvzf $xgfile || exit -1
    build "$code"
    cd $working/$code

    confopts="--prefix=$prefix --with-SCALAR=$precision --enable-fulloptimize"
    ./configure $confopts && make -j 4 && sudo make install || exit -1
    make clean
    succeed "$code"
else
    already "$code"
fi
# xoopic
cd $working
code=xoopic
if [ $force != false ] || [ ! -f "$prefix/bin/$code" ]; then
    msg "FETCHING XOOPIC"
    wget http://ptsg.berkeley.edu/pub/codes/xoopic/$xofile
    tar -xvzf $xofile || exit -1
    build "$code"
    cd $working/$code
    prefix=/usr/local
    confopts="--prefix=$prefix --enable-fulloptimize"
    ./configure $confopts && make -j 4 && sudo cp $code $prefix/bin/ || exit -1
    make clean 2>/dev/null
    succeed "$code"
else
    already "$code"
fi

cd $working
# all the rest
msg "EXTRACTING CODES"
tar -xvzf $allfile || exit -1

cd $working/ptsgcodes

for code in xem1 xes1; do
    if [ ! -f "$prefix/bin/$code" ]; then
        build "$code"
        cd $working/ptsgcodes/$code
        make && sudo mv $code $prefix/bin/ || exit -1
        make clean 2>/dev/null
        succeed "$code"
    else
        already "$code"
    fi
done

for code in xpdc1 xpdc2 xpdp1; do
    if [ $force != false ] || [ ! -f "$prefix/bin/$code" ]; then
        build "$code"
        cd $working/ptsgcodes/$code/src
        make && sudo mv ../$code $prefix/bin/ || exit -1
        make clean 2>/dev/null
        succeed "$code"
    else
        already "$code"
    fi
done

code=xpdp2
if [ $force != false ] || [ ! -f "$prefix/bin/$code" ]; then
    build "$code"
    cd $working/ptsgcodes/$code
    make && sudo mv xargon2d $prefix/bin/ || exit -1
    make clean 2>/dev/null
    cd $prefix/bin
    sudo ln -s xargon2d $code
    succeed "$code"
else
    already "$code"
fi


cd $working/..

# rm -rf $working

