#!/bin/sh

usage='echo "Usage: ccp4_build [--help] [--binary-install] [--bindir=DIR]" '

#  $PHASER_TAR   = $CCP4/src/phaser
#  $PHASER_LOG   = $CCP4/src/phaser/build-source/$PHASER_MTYPE/$PHASER_HOST/log
#      or        = $CCP4/src/phaser/build-final/$PHASER_MTYPE/$PHASER_HOST/log
#  $PHASER_LOC   = $CCP4/src/phaser/phaser-1.2
#  $PHASER_BUILD = $CCP4/src/phaser/phaser-1.2/build/$PHASER_MTYPE

phaseropts="--prefix=$CCP4/src/phaser --force-compile"
binary_inst="no"
for arg
do

    case $arg in

    -help | --help | -h | help)
      eval $usage; exit 0;;

    -bindir=* | --bindir=* | --bindi=* | --bind=* )
      bindir=`echo $arg | sed 's/[-a-z_]*=//'` ;;

    -binary-install | --binary-install | -binary-inst | --binary-inst | -binary | --binary)
      binary_inst="yes" ;;

    # all other arguments passed to phaser build
    *)
      phaseropts="$phaseropts $arg" ;;

    esac

done

bindir=${bindir:-$CCP4_BIN}
libdir=${libdir:-$CCP4_LIB}
incdir=${incdir:-$CCP4/include}

# Get Phaser environment variables, so we know where executable is.
PHASER_MTYPE=`bin/machine_type`
PHASER_VERSION=`grep PHASER_VERSION conf/version | awk '{print $3}'`

if test $binary_inst = "yes"; then

echo " "
echo " Configuring binary distribution of Phaser"
echo " "

# This is Phaser installer, with some minor changes for CCP4
./install $phaseropts --binary-install --component="mmtbx" --component="phaser"

else

# Check if shared libraries left-over from previous build
if test -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/lib/libcmtz.so ; then
  /bin/rm phaser-$PHASER_VERSION/build/$PHASER_MTYPE/lib/libcmtz.so
fi
if test -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/lib/libcctbx.so ; then
  /bin/rm phaser-$PHASER_VERSION/build/$PHASER_MTYPE/lib/libcctbx.so
fi

echo " "
echo " Building static phaser executable"
echo " "

# This is Phaser installer, with some minor changes for CCP4
./install $phaseropts --static_libraries --component="mmtbx" --component="phaser" 

if test ! -d "$bindir" ; then
  echo "making directory $bindir"
  echo " "
  mkdir -p $bindir
fi
if test ! -d "$libdir" ; then
  echo "making directory $libdir"
  echo " "
  mkdir -p $libdir
fi
if test ! -d "$incdir" ; then
  echo "making directory $incdir"
  echo " "
  mkdir -p $incdir
fi

# Install in CCP4 bindir
if test -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/exe/phaser ; then
  cp -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/exe/phaser $bindir
fi
if test -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/lib/libcmtz.a ; then
  cp -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/lib/libcctbx.a $libdir
fi
cp -rf phaser-$PHASER_VERSION/build/$PHASER_MTYPE/include/* $incdir

echo " "
echo "=========================================================================="
echo "   Phaser executable installed in $bindir"
echo "   cctbx library installed in $libdir"
echo "   cctbx include files installed in $incdir"
echo "=========================================================================="
echo " "

rm -rf phaser-$PHASER_VERSION/build/$PHASER_MTYPE/exe/
rm -rf phaser-$PHASER_VERSION/build/$PHASER_MTYPE/lib/

echo " "
echo " Building phaser Python bindings"
echo " "

# This is Phaser installer, with some minor changes for CCP4
./install $phaseropts --component="mmtbx" --component="phaser"

echo " "
echo "=========================================================================="
echo "       To get the full phaser/cctbx environment source the file"
echo "   `pwd`/phaser-$PHASER_VERSION/build/$PHASER_MTYPE/setpaths.sh  or"
echo "   `pwd`/phaser-$PHASER_VERSION/build/$PHASER_MTYPE/setpaths.csh"
echo "=========================================================================="
echo " "

# We remove the phaser executable to avoid confusion with static exe built previously
rm -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/exe/phaser
# We then have to remove the two dispatchers that point to it
rm -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/bin/phaser
rm -f phaser-$PHASER_VERSION/build/$PHASER_MTYPE/bin/phaser_$PHASER_VERSION

fi
