#!/bin/bash

. /etc/lsb-release

OCFLAGS="-fno-signed-zeros -fno-signaling-nans -fno-trapping-math -fcx-limited-range -fassociative-math"
M=mace.phon.ox.ac.uk
if test $(uname -n) = "desk" -o $(uname -n) = "kitchen"
then
  MACHINES="localhost desk kitchen stars"
  MHG=ssh://$M/
  MBZR=bzr+ssh://$M
  MGIT=ssh://$M
  MRS=$M:
elif test $(uname -n) = "mace"
then
  MACHINES="localhost mace jalapeno cayenne tabasco basil clove boldo fennel wasabi borage phon-temp1 phon-temp2"
  MHG=
  MBZR=
  MGIT=
  MRS=
elif test $(uname -n) = "nglap"
then
  MACHINES="localhost"
  MHG=ssh://$M/
  MBZR=bzr+ssh://$M
  MGIT=ssh://$M
  MRS=$M:
else
  die.sh "Unknown machine" $(uname -n)
  exit 2
fi

exitstat=0
trap "die.sh ERROR in $PWD || exitstat=1" ERR
t=/tmp/ia$$
trap "cat $t/*" EXIT
mkdir -p $t

if test "$1" = -clean
then
  cb="rm"
  cm="make clean"
  cs="rm -rf build"
  shift
else
  cb="true"
  cm="true"
  cs="true"
fi

if test "$1" = "stable"
then
  o=$1
elif test "$1" = "working"
then
  o=$1
else
  die.sh "Need to specify 'stable' or 'working'" || exit 1
fi

dt=$(date +%Y%m%dT%H%M%S)
dn=lcl/$o$dt$DISTRIB_CODENAME
D=/scratch/$USER
d=$D/$dn

test -d $d || mkdir -p $d
ver=$(python -c 'import sys; print "%s.%s" % sys.version_info[:2]')
pp=$d/lib/python$ver/site-packages
export PYTHONPATH="$pp":$PYTHONPATH
mkdir -p $pp

S=/scratch/$USER/install.$o.tmp
s=$S/speechresearch
(
test -d $s/bin || mkdir -p $s/bin
rsync -avz --delete mace.phon.ox.ac.uk:bin/ $S/bin
mkdir $d/bin
install -p $S/bin/* $d/bin
)

(
rm -rf $D/local
mkdir -p $D/local/bin
cd $d/bin
install -p GPKprofile die.sh install_all.sh sshfs $D/local/bin
)

cat <<ENDEND >$d/bin/GPKlocal
#!/bin/sh
# This little script is simply a marker for /usr/local/bin
# or its equivalent.
# This allows GPKprofile can find it in a
# machine-independent manner.
. /etc/lsb-release
test \$DISTRIB_CODENAME = $DISTRIB_CODENAME
ENDEND
chmod a+x $d/bin/GPKlocal

cd $s
svn checkout https://speechresearch.svn.sourceforge.net/svnroot/speechresearch/trunk .

cd $s/gpklib
test -d .hg || hg init
hg pull $MHG/proj/exemplar/hg/gpklib
hg update

#ZZZ KLUGE
echo "PATCH1"
cp -p /home/gpk/speechresearch/gpklib/romberg/qromo.c romberg || die.sh no patch || exit 3;
ls -l romberg
#ZZZ KLUGE

rm -f make.inc
install -p make.Linux-generic.inc make.inc
install -p gpkieee754_Linux.c gpkieee754.c
install -p gpkieee754_Linux.h gpkieee754.h
ppt="$PYTHONPATH"
export PYTHONPATH=.:../gmisclib:$PYTHONPATH
echo CFLAGS += $(python ../gmisclib/bin/get_gcc_flags.py $MACHINES) $OCFLAGS  >>make.inc
export PYTHONPATH=$ppt
make -j BASE=$d clean
make BASE=$d install

cd $s/gpkio
make -j INCFILE=$d/include/gpklib/make.inc clean
make INCFILE=$d/include/gpklib/make.inc install

(
cd $S
rsync -avz --delete ${MRS}/proj/exemplar/DOWNLOADS/ DOWNLOADS/
cy=$(ls -dl DOWNLOADS/Cython* | grep ^d | awk '{print $8;}' | sort | tail -1 )
cd $cy
$cs
python setup.py install --prefix=$d
)

# exit 0

(
cd $s/gmisclib
$cs
rm -f *.cpp *.c
ppt=$PYTHONPATH
export PYTHONPATH=.:$PYTHONPATH
export CFLAGS="$(python bin/get_gcc_flags.py $MACHINES) $OCFLAGS"
export PYTHONPATH=$ppt
echo 'CFLAGS=' $CFLAGS
#ZZZ KLUGE
echo "PATCH2"
cp -p /home/gpk/speechresearch/gmisclib/{mcmc,mcmc_helper}.py . || die.sh no patch || exit 3;
ls -l .
#ZZZ KLUGE
python setup.py install --prefix=$d
)

(
cd $s
fp=$(ls -dl fpconst* | grep ^d | sort | tail -1 | awk '{print $8;}' )
cd $fp
$cs
python setup.py install --prefix=$d
) >$t/fpconst 2>&1 &

(
cd $s/voicing
$cs
# test -d .hg || hg init
# hg pull $MHG/proj/exemplar/hg/voicing
# hg update
python setup.py install --prefix=$d
) >$t/voicing 2>&1 &

(
cd $s/classifiers
$cs
python setup.py install --prefix=$d
) >$t/classifiers 2>&1 &

(
cd $s/gpk_avg_py
$cs
python setup.py install --prefix=$d
) >$t/gpk_avg_py 2>&1 &

(
cd $s/gpk_img_python
$cs
python setup.py install --prefix=$d
) >$t/gpk_img_python 2>&1 &

(
cd $s/gyropy
$cs
python setup.py install --prefix=$d
) >$t/gyropy 2>&1 &

# (
# test -d $S/newstem2 || mkdir -p $S/newstem2
# cd $S/newstem2
# test -d .hg || hg init
# hg pull $MHG/proj/exemplar/hg/newstem2
# hg update
# $cs
# python setup.py install --prefix=$d
# ) >$t/newstem2 2>&1 &


(
test -d $S/Exp1 || mkdir -p $S/Exp1
cd $S/Exp1
test -d .bzr || bzr init
bzr pull $MBZR/proj/exemplar/proc/Exp1
bzr update
$cs
python setup.py install --prefix=$d
) >$t/Exp1 2>&1 &

(
test -d $S/jc || mkdir -p $S/jc
cd $S/jc
test -d .bzr || bzr init
bzr pull $MBZR/proj/exemplar/proc/jc
bzr update
$cs
python setup.py install --prefix=$d
) >$t/jc 2>&1 &

(
test -d $S/select_paragraphs || mkdir -p $S/select_paragraphs
cd $S/select_paragraphs
test -d .hg || hg init
hg pull $MHG/proj/aesop/select_paragraphs
hg update
$cs
python setup.py install --prefix=$d
) >$t/select_paragraphs 2>&1 &

(
test -d $S/ItakuraSaitoDistance || mkdir -p $S/ItakuraSaitoDistance
cd $S/ItakuraSaitoDistance
# test -d .bzr || bzr init
test -d .git || git init
# bzr pull $MBZR/proj/exemplar/ItakuraSaitoDistance
git pull $MGIT/proj/exemplar/ItakuraSaitoDistance
# bzr update
git checkout master
cd IS-DTW
$cs
python setup.py install --prefix=$d
) >$t/IS-DTW 2>&1 &

wait

cd 
cd $D
rm -f $o && ln -s $dn $o
rm -f ${o}_$DISTRIB_CODENAME && ln -s $dn ${o}_$DISTRIB_CODENAME
wait

if test $exitstat = 1
then
  echo "ERROR"
  exit 1
fi
exit 0
