#!/bin/bash

cd `dirname "$0"`
dir=`pwd`
master=`dirname $dir`
subdir=`basename $dir`

################### confirm licence ###################

if [ -f $HOME/.agree2ccp4v6 ]; then
    agreed=yes
else
    more conditions.txt
    echo
    echo -n "Do you accept the terms of the license? y/n [n] "
    read ans
    if [ "$ans" = y -o $ans = Y -o $ans = yes -o $ans = YES ]; then 
        agreed=yes
    fi
fi

if [ "$agreed" = "yes" ]; then
    echo `date` "$subdir" >> $HOME/.agree2ccp4v6
else
    echo " You have indicated that you do not agree to the conditions";
    echo " of use for the CCP4 package. Please delete all CCP4 software";
    echo " from your system. If you have any further queries please refer ";
    echo " to the CCP4 web pages on http://www.ccp4.ac.uk/ or contact" ;
    echo " ccp4@ccp4.ac.uk";
    exit 1
fi

################### final setup ###################

echo 'Setting up paths...'

sed "s|\(CCP4_MASTER=\).*|\1$master|" include/ccp4.setup-sh.in | \
  sed "s|\(CCP4=\$CCP4_MASTER/\).*|\1$subdir|" > bin/ccpem.setup-sh

sed "s|\(setenv CCP4_MASTER.\).*|\1$master|" include/ccp4.setup-csh.in | \
  sed "s|\(setenv CCP4.*\$CCP4_MASTER/\).*|\1$subdir|" > bin/ccpem.setup-csh

#if [ "$(uname)" = "Linux" ]; then
## Since libgfortran and libstdc++ are backward compatible use the system
## versions if available (it may be more efficient).
## Otherwise, the bundled copies are "activated" by creating links.
## In case of libgfortran.so.3, SLES11 has older version that CentOS5
## and there are concerns about installation on heterogenous Linux network,
## so always use bundled libgfortran. (But not always bundled libstdc++:
## it would not work on RHEL4 due to too old glibc)
#    rm -f lib/libgfortran.so.3 lib/libstdc++.so.6 lib/libgomp.so.1
#    (cd lib && ln -s libgfortran.so.3.?.? libgfortran.so.3)
#    if ( ldd bin/pisa | grep -q 'libstdc++.so.6 => not found' ); then
#        (cd lib && ln -s libstdc++.so.6.?.? libstdc++.so.6)
#        echo "using bundled libstdc++.so.6"
#    fi
#    if ( ldd bin/phaser | grep -q 'libgomp.so.1 => not found' ); then
#        (cd lib && ln -s libgomp.so.1.?.? libgomp.so.1)
#        echo "using bundled libgomp.so.1"
#    fi
#fi

# the --minimal option is for quick testing only
[ "$1" = "--minimal" ] && exit

echo 'Compiling py-files'
(CCP4=$dir $dir/bin/ccpem-python -Wignore -m compileall \
 $dir/lib/py2 $dir/lib/python2.7 >/dev/null)


################### checks for common problems ###################

if [ -n "$CCP4" ]; then
    echo 'WARNING: $CCP4 is already set.'
    if [ "$CCP4" != "$dir" ]; then
        echo 'WARNING: $CCP4 is set to different directory.'
    fi
fi

if [ -n "$TCL_LIBRARY" ]; then
    echo 'WARNING: You have set $TCL_LIBRARY. Unset it or expect problems.'
fi
if [ -n "$TK_LIBRARY" ]; then
    echo 'WARNING: You have set $TK_LIBRARY. Unset it or expect problems.'
fi
if [ -n "$GFORTRAN_UNBUFFERED_ALL" ]; then
    echo 'WARNING: You have set $GFORTRAN_UNBUFFERED_ALL.'
    echo '         It makes some programs running much slower.'
fi

if ! $dir/bin/refmac5 -i >/dev/null; then
    echo "Starting an example CCP4 program failed !!!"
    echo "Make sure you downloaded version for you operating system."
fi

echo
echo "         Before running CCPEM programs from command line,"
echo "         source bin/ccpem.setup-sh"
echo "         (or bin/ccpem.setup-csh if you use csh)."
echo