#!/bin/csh -f
#
# =========================================================================
#
# PHASER source installation script
#
# Authors: Paul Adams, 2003, 2004, 2005 and Michael Hohn 2004. 
#
# Copyright: Lawrence Berkeley National Laboratory, 2003
#
# =========================================================================
#

#* argument parsing 
set compile = 1
set nproc = 1
set overwrite = 0
set noopt = 0
set debug = 0
set force_compile=0
set static_flag=" "
set compiler_flag=" "
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 --no-compile:
    set compile=0
    breaksw
  case --overwrite:
    set overwrite=1
    breaksw
  case --noopt:
    set noopt=1
    breaksw
  case --debug:
    set debug=1
    breaksw
  case --force-compile:
    set force_compile=1
    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 --compiler*:
    set compiler_flag_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$compiler_flag_tmp" == "" ) then
      echo "error: empty compiler defined"
      exit 1
    else
      set compiler_flag = ( --compiler=$compiler_flag_tmp )
    endif
    breaksw
  case --static_libraries:
    set static_flag = ( $static_flag --static_libraries )
    breaksw
  case --static_exe:
    set static_flag = ( $static_flag --static_exe )
    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 --help:
    echo "Usage: install-from-source --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 "  --overwrite         Forces overwriting of installed packages"
    echo "  --nproc=number      Number of processors to use in compilation"
    echo "  --no-compile        Do not compile PHASER source, even if present"
    echo "  --noopt             Turn off optimization during compilation"
    echo "  --debug             Turn on debugging during compilation"
    echo "  --force-compile     Force compilation of source"
    echo "  --help              Print this help message"
    exit 0
  default:
    echo "install-from-source: unknown option $arg"
    exit 1
  endsw
end

if ( ( ! $?PHASER_TAR )   || \
     ( ! $?PHASER_LOC )   || \
     ( ! $?PHASER_MTYPE ) || \
     ( ! $?PHASER_VERSION ) ) then
  echo "install-from-source: missing command line parameters"
  echo "Usage: install-from-source --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-source/$PHASER_MTYPE/$PHASER_HOST/tmp
setenv PHASER_LOG $PHASER_TAR/build-source/$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-source/$PHASER_MTYPE/$PHASER_HOST/tmp"
echo "PHASER installation log directory set to: "
echo "  build-source/$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-source/$PHASER_MTYPE/$PHASER_HOST/tmp"
  echo " "
  mkdir -p $PHASER_TMP
endif

if ( ! -d "$PHASER_LOG" ) then
  echo "making directory: "
  echo "  build-source/$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-source/$PHASER_MTYPE/$PHASER_HOST/tmp"
  echo " "
  touch $PHASER_TMP/.install_status
endif

#
# ==========================================================================
# Install PHASER from source for this platform
# ==========================================================================
#

#
# ==========================================================================
# Python must be compiled and installed
# For CCP4 we assume this is done elsewhere
# ==========================================================================
#

# cctbx bundle including boost:
# =============================

echo "checking cctbx sources"
echo " "

# set up redirection for cctbx and ccp4 source
set ccp4_repositories="--repository ${CCP4}/lib/cctbx/cctbx_sources"
set ccp4_redirections="ccp4io=${CCP4}"
if ( ( ! `grep ^cctbx $PHASER_TMP/.install_status | awk '{print $2}'` ) || \
     ( ! -e $PHASER_LOC/libtbx/configure.py ) ) then

  echo "updating cctbx sources for the phaser build"
  echo " "
  cd $PHASER_TAR
  /bin/rm -f $PHASER_LOG/cctbx.log

  # but these need to be copied explicitly
  cp -rf $CCP4/lib/cctbx/cctbx_sources/scons $PHASER_LOC
  cp -rf $CCP4/lib/cctbx/cctbx_sources/libtbx $PHASER_LOC

# make soft links to build dirs from previous cctbx build
  foreach cctbx_build_dir ( "boost" "boost_adaptbx" "ccp4io" "ccp4io_adaptbx" "cctbx" "include" "iotbx" "libtbx" "mmtbx" "scitbx" "scons" )
    test -d $CCP4/lib/cctbx/cctbx_build/$cctbx_build_dir && ln -s $CCP4/lib/cctbx/cctbx_build/$cctbx_build_dir $PHASER_BUILD/$cctbx_build_dir
  end

  if ( ! `grep ^cctbx $PHASER_TMP/.install_status | awk '{print $2}'` ) then
    echo cctbx 1 >> $PHASER_TMP/.install_status
  endif

endif

#* Phaser:
# =======

echo "checking phaser sources"
echo " "

# set up redirection for phaser source
set ccp4_repositories="--repository ${PHASER_TAR}/source $ccp4_repositories"

if ( ! `grep ^phaser $PHASER_TMP/.install_status | awk '{print $2}'` ) then
  cd $PHASER_TAR
  /bin/rm -f $PHASER_LOG/phaser.log

  if ( ! $status ) then
    echo phaser 1 >> $PHASER_TMP/.install_status
  endif
endif

#* Phaser compilation:
# ===================

echo "checking for compiled PHASER components"
echo " "

if ( $compile ) then
  if ( ( ! `grep ^compile_phaser $PHASER_TMP/.install_status | awk '{print $2}'` ) || $force_compile ) then
    cd $PHASER_BUILD

    if ( $debug ) then
      echo "configuring components with debugging"
      echo " "
      python $PHASER_LOC/libtbx/configure.py $static_flag $compiler_flag \
             $ccp4_repositories --build=debug \
             --command_version_suffix="$CCP4_VERSION" \
             $components $ccp4_redirections |& tee $PHASER_LOG/compile.log
    else if ( $noopt ) then
      echo "configuring components with no optimization"
      echo " "
      python $PHASER_LOC/libtbx/configure.py $static_flag $compiler_flag \
             $ccp4_repositories --build=quick \
             --command_version_suffix="$CCP4_VERSION" \
             $components $ccp4_redirections |& tee $PHASER_LOG/compile.log
    else
      echo "configuring components with optimization"
      echo " "
      python $PHASER_LOC/libtbx/configure.py $static_flag $compiler_flag \
             $ccp4_repositories --build=release \
             --command_version_suffix="$CCP4_VERSION" \
             $components $ccp4_redirections |& tee $PHASER_LOG/compile.log
    endif

    echo "setting up environment"
    echo " "
    source setpaths.csh

    echo "compiling PHASER components - this might take an hour"
    echo " "
    libtbx.scons -j $nproc $targets |& tee -a $PHASER_LOG/compile.log

    if ( ! $status ) then
      if ( ! `grep ^compile_phaser $PHASER_TMP/.install_status | awk '{print $2}'` ) then
        echo compile_phaser 1 >> $PHASER_TMP/.install_status
      endif
    else
      echo "compilation of PHASER components failed"
      echo " "
      exit 1
    endif
  endif
endif

