#!/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/ccp4.setup-sh

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

(. ./bin/ccp4.setup-sh && ./libexec/install_dispatchers.sh >/dev/null)

(cd html/AstexViewer_man && CCP4=$dir ./make_examples.sh)

$dir/bin/tclsh <<+
catch {
  set system(OPSYS) UNIX
  set env(CCP4) $dir
  set env(CCP4I_TCLTK) $dir/bin
  set env(CCP4I_TOP) $dir/share/ccp4i
  set env(DBCCP4I_TOP) $dir/share/dbccp4i
  source [file join $dir/share/ccp4i bin UNIX startup.tcl]
  source [file join $dir/share/ccp4i src system.tcl]
  source [file join $dir/share/ccp4i src utils.tcl]
  source [file join $dir/share/ccp4i etc types.def]
  InitialisePreferences configure configure
}
+

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

arpwarp_dir=arp_warp_7.5
if [ -d $master/$arpwarp_dir ]; then
   echo
   echo "----- Start ARP/wARP installation ($arpwarp_dir) -----"
   ( . $master/$subdir/bin/ccp4.setup-sh && \
    cd $master/$arpwarp_dir && ./install.sh ccp4=$master/$subdir \
    | egrep -v 'TERMINAL|RE-ACT|COMPLETED|^$')
   echo "----- End of ARP/wARP installation -----"
   echo
fi

################### 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/sfall -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 CCP4 programs from command line,"
echo "         source bin/ccp4.setup-sh"
echo "         (or bin/ccp4.setup-csh if you use csh)."
echo
