#!/bin/csh -f
#
# =========================================================================
#
# PHASER installation finalization script
#
# Author: Paul Adams, October 2003
#
# Copyright: Lawrence Berkeley National Laboratory, 2003
#
# =========================================================================
#

set test = 0
set nproc = 1
set textal_maps = 1
set components=""
set targets=" "

foreach arg ( $argv[*] )
  switch ( $arg )
  case --from*:
    set from_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$from_tmp" == "" ) then
      echo "error: empty from path definition"
      exit 1
    else
      setenv PHASER_TAR $from_tmp
    endif
    breaksw
  case --to*:
    set to_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$to_tmp" == "" ) then
      echo "error: empty to path definition"
      exit 1
    else
      setenv PHASER_LOC $to_tmp
    endif
    breaksw
  case --arch*:
    set arch_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "arch_tmp" == "" ) then
      echo "error: empty architecture definition"
      exit 1
    else
      setenv PHASER_MTYPE $arch_tmp
    endif
    breaksw
  case --version*:
    set version_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "version_tmp" == "" ) then
      echo "error: empty version definition"
      exit 1
    else
      setenv PHASER_VERSION $version_tmp
    endif
    breaksw
  case --nproc*:
    set nproc_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$nproc_tmp" == "" ) then
      echo "error: empty number of processors defined"
      exit 1
    else
      set nproc=$nproc_tmp
    endif
    breaksw
  case --component*:
    set components_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$components_tmp" == "" ) then
      echo "error: empty component defined"
      exit 1
    else
      set components= ( $components $components_tmp )
    endif
    breaksw
  case --target*:
    set targets_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$targets_tmp" == "" ) then
      echo "error: empty target defined"
      exit 1
    else
      set targets= ( $targets $targets_tmp )
    endif
    breaksw
  case --test:
    set test=1
    breaksw
  case --help:
    echo "Usage: install-finalize --from=<path> --to=<path> --arch=<string> --version=<string> [options]"
    echo "Options:"
    echo "  --from=<path>       Assign path to location of PHASER installation files"
    echo "  --to=<path>         Determines where the PHASER components will be installed"
    echo "                      The full path will be <path>/phaser-[version]"
    echo "  --arch=<string>     Assign PHASER architecture type"
    echo "  --version=<string>  Assign PHASER version to be installed"
    echo "  --nproc=number      Number of processors to use in installation"
    echo "  --test              Run regression tests"
    echo "  --help              Print this help message"
    exit 0
  default:
    echo "install-finalize: unknown option $arg"
    exit 1
  endsw
end

if ( ( ! $?PHASER_TAR )   || \
     ( ! $?PHASER_LOC )   || \
     ( ! $?PHASER_MTYPE ) || \
     ( ! $?PHASER_VERSION ) ) then
  echo "install-finalize: missing command line parameters"
  echo "Usage: install-finalize --from=<path> --to=<path> --arch=<string> --version=<string> [options]"
  exit 1
endif 

setenv CCP4_VERSION "6.0"
setenv PHASER_HOST `hostname | awk 'BEGIN {FS="."}{print $1}'`

setenv PHASER_MVERSION `$PHASER_TAR/bin/machine_version`
source $PHASER_TAR/conf/packages
source $PHASER_TAR/conf/configure-options

setenv PHASER_LOC "$PHASER_LOC"/phaser-"$PHASER_VERSION"
setenv PHASER_BUILD $PHASER_LOC/build/$PHASER_MTYPE
setenv PHASER_TMP $PHASER_TAR/build-final/$PHASER_MTYPE/$PHASER_HOST/tmp
setenv PHASER_LOG $PHASER_TAR/build-final/$PHASER_MTYPE/$PHASER_HOST/log

echo "PHASER installation source directory set to: "
echo "  $PHASER_TAR"
echo "PHASER installation target directory <PHASER_LOC> set to: "
echo "  $PHASER_LOC"
echo "PHASER installation build directory set to: "
echo "  <PHASER_LOC>/build/$PHASER_MTYPE"
echo "PHASER temporary build directory set to: "
echo "  build-final/$PHASER_MTYPE/$PHASER_HOST/tmp"
echo "PHASER installation log directory set to: "
echo "  build-final/$PHASER_MTYPE/$PHASER_HOST/log"
echo "  "

if ( ! -d "$PHASER_LOC" ) then
  echo "making directory: "
  echo "  $PHASER_LOC"
  echo " "
  mkdir -p $PHASER_LOC  
endif

if ( ! -d "$PHASER_BUILD" ) then
  echo "making directory: "
  echo "  <PHASER_LOC>/build/$PHASER_MTYPE"
  echo " "
  mkdir -p $PHASER_BUILD  
endif

if ( ! -d "$PHASER_TMP" ) then
  echo "making directory: "
  echo "  build-final/$PHASER_MTYPE/$PHASER_HOST/tmp"
  echo " "
  mkdir -p $PHASER_TMP
endif

if ( ! -d "$PHASER_LOG" ) then
  echo "making directory: "
  echo "  build-final/$PHASER_MTYPE/$PHASER_HOST/log"
  echo " "
  mkdir -p $PHASER_LOG
endif

if ( ! -e $PHASER_TMP/.install_status ) then
  echo "creating file .install_status in: " 
  echo " build-final/$PHASER_MTYPE/$PHASER_HOST/tmp"
  echo " "
  touch $PHASER_TMP/.install_status
endif

#
# ==========================================================================
# Finalize PHASER installation for this platform
# ==========================================================================
#

# Phaser environment script:
# ==========================

echo " "
echo "generating PHASER environment setup script"
echo " "

if ( -e $PHASER_LOC/phaser_env ) then
  echo "removing out-of-date script $PHASER_LOC/phaser_env"
  /bin/rm -f $PHASER_LOC/phaser_env
endif

echo "creating $PHASER_LOC/phaser_env"
echo " "

echo '#\!/bin/csh -f' > $PHASER_LOC/phaser_env
echo '#' >> $PHASER_LOC/phaser_env
echo setenv PHASER $PHASER_LOC >> $PHASER_LOC/phaser_env
echo if \(  -e '$PHASER'/conf/installer-time-stamp \) then >> $PHASER_LOC/phaser_env
echo " " setenv PHASER_INSTALLER_DATE \`cat '$PHASER'/conf/installer-time-stamp\` >> $PHASER_LOC/phaser_env
echo endif >> $PHASER_LOC/phaser_env
echo setenv PHASER_VERSION $PHASER_VERSION >> $PHASER_LOC/phaser_env
echo setenv PHASER_ENVIRONMENT 1 >> $PHASER_LOC/phaser_env
echo setenv PHASER_MTYPE \`'$PHASER/bin/machine_type'\` >> $PHASER_LOC/phaser_env
echo setenv PHASER_MVERSION \`'$PHASER/bin/machine_version'\` >> $PHASER_LOC/phaser_env
echo '#' >> $PHASER_LOC/phaser_env
echo if \( \"'$PHASER_MTYPE'\" == \"unknown-unknown\" \) then >> $PHASER_LOC/phaser_env
echo "  exit 1" >> $PHASER_LOC/phaser_env
echo endif >> $PHASER_LOC/phaser_env
echo if \( \"'$PHASER_MTYPE'\" == \"intel-linux\" \) then >> $PHASER_LOC/phaser_env
echo " " if \( \"'$PHASER_MVERSION'\" != \"redhat8.0\" \) then >> $PHASER_LOC/phaser_env
echo "    setenv LD_ASSUME_KERNEL 2.4.1"  >> $PHASER_LOC/phaser_env
echo " " endif >> $PHASER_LOC/phaser_env
echo endif >> $PHASER_LOC/phaser_env
echo '#' >> $PHASER_LOC/phaser_env
echo if \( -e '$PHASER'/build/'$PHASER_MTYPE'/setpaths.csh \) then >> $PHASER_LOC/phaser_env
echo " " source '$PHASER'/build/'$PHASER_MTYPE'/setpaths.csh >> $PHASER_LOC/phaser_env
echo endif >> $PHASER_LOC/phaser_env
echo '#' >> $PHASER_LOC/phaser_env

# Libtbx environment additions
# ============================

echo "generating libtbx environment additions"
echo " "

set dep_file = "$PHASER_BUILD/dispatcher_include_phaser.sh"

if ( -e $dep_file ) then
  echo 'removing out-of-date file $PHASER_BUILD/dispatcher_include_phaser.sh'
  /bin/rm -f $dep_file
endif

echo 'creating $PHASER_BUILD/dispatcher_include_phaser.sh'
echo " "

echo '#' > $dep_file
echo '# PHASER specific environment additions' >> $dep_file
echo '#' >> $dep_file
echo '# GUI dependencies' >> $dep_file
echo 'if [ "$PHASER_GUI_ENVIRONMENT" = "1" ]; then' >> $dep_file
echo " " 'if [ -z "$LD_LIBRARY_PATH" ]; then' >> $dep_file
echo "   " LD_LIBRARY_PATH='$PHASER'/build/'$PHASER_MTYPE'/python/lib >> $dep_file
echo " " else >> $dep_file
echo "   " LD_LIBRARY_PATH='$PHASER'/build/'$PHASER_MTYPE'/python/lib:'$LD_LIBRARY_PATH' >> $dep_file
echo " " fi >> $dep_file
echo " " export LD_LIBRARY_PATH >> $dep_file
echo '#' >> $dep_file
echo 'fi' >> $dep_file
echo '#' >> $dep_file


# run configure to generate dispatchers:
# ======================================

echo "configuring PHASER components"
echo " "

if ( ! -d "$PHASER_LOC/libtbx" ) then
  cp -rf $CCP4/lib/cctbx/cctbx_sources/libtbx $PHASER_LOC
endif

set ccp4_repositories="--repository ${PHASER_TAR}/source --repository ${CCP4}/lib/cctbx/cctbx_sources"
set ccp4_redirections="ccp4io=${CCP4}"

cd $PHASER_BUILD
python $PHASER_LOC/libtbx/configure.py $ccp4_repositories \
             --command_version_suffix="$CCP4_VERSION" \
             $components $ccp4_redirections |& tee -a $PHASER_LOG/configure.log
cd $PHASER_LOC

# Setup environment to use components for installation tasks:
# ===========================================================

if ( -e $PHASER_BUILD/setpaths.csh ) then
  echo "setting up PHASER execution environment for installation"
  echo " "
  source $PHASER_BUILD/setpaths.csh 
endif

# Install bin directory:
# ======================

if ( -d $PHASER_LOC/bin ) then
  /bin/rm -rf $PHASER_LOC/bin
endif

echo "copying bin directory $PHASER_TAR/bin to"
echo "                      $PHASER_LOC"
echo " "
cp -r $PHASER_TAR/bin $PHASER_LOC

# Install conf directory:
# =======================

if ( -d $PHASER_LOC/conf ) then
  /bin/rm -rf $PHASER_LOC/conf
endif

echo "copying conf directory $PHASER_TAR/conf to"
echo "                       $PHASER_LOC"
echo " "
cp -r $PHASER_TAR/conf $PHASER_LOC

# Installation version information:
# =================================

echo "creating version information"

if ( -e $PHASER_LOC/version ) then
  /bin/rm -f $PHASER_LOC/version
endif
echo "  creating core version file"
echo $PHASER_VERSION > $PHASER_LOC/version 

if ( -e $PHASER_BUILD ) then
  if ( -e $PHASER_BUILD/version ) then
    /bin/rm -f $PHASER_BUILD/version
  endif
  echo "  creating build version file"
  echo $PHASER_VERSION > $PHASER_BUILD/version 
endif

echo " "

# Precompile .py files:
# =====================

echo "precompiling .py files"
echo " "

cd $PHASER_LOC
python $PHASER_LOC/libtbx/libtbx/command_line/py_compile_all.py

# Modify file ownership:
# ======================

set user_group = `groups | awk '{print $1}'`
cd $PHASER_LOC

echo " "
echo "changing ownership of PHASER installation to:"
echo "  user: $USER  group: $user_group"
echo " "

chown -R $USER\:$user_group .

