#!/bin/csh -f
#
# =========================================================================
#
# PHASER main level installation script
#
# Author: Paul Adams, June 2004
#
# Copyright: Lawrence Berkeley National Laboratory, 2004
# =========================================================================
#

setenv PHASER_TAR `pwd`
setenv PHASER_MTYPE    `$PHASER_TAR/bin/machine_type`
setenv PHASER_MVERSION `$PHASER_TAR/bin/machine_version`

source $PHASER_TAR/conf/location
source $PHASER_TAR/conf/version

#
# ==========================================================================
# Parse command line arguments
# ==========================================================================
#

set source=1
set source_options=()
set finalize_options=()
set compiler_tmp=""
set component_tmp=""
set target_tmp=""
set binary_inst=0

foreach arg ( $argv[*] )
  switch ( $arg )
  case --no-compile:
    set source_options = ( $source_options --no-compile )
    breaksw
  case --overwrite:
    set source_options = ( $source_options --overwrite )
    breaksw
  case --noopt:
    set source_options = ( $source_options --noopt )
    breaksw
  case --debug:
    set source_options = ( $source_options --debug )
    breaksw
  case --binary-install:
    set binary_inst=1
    breaksw
  case --force-compile:
    set source_options = ( $source_options --force-compile )
    breaksw
  case --static_libraries:
    set source_options = ( $source_options --static_libraries )
    breaksw
  case --static_exe:
    set source_options = ( $source_options --static_exe )
    breaksw
  case --prefix*:
    set prefix_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$prefix_tmp" == "" ) then
      echo "error: empty prefix path definition"
      exit 1
    else
      setenv PHASER_LOC $prefix_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 source_options = ( $source_options --nproc=$nproc_tmp )
      set finalize_options = ( $finalize_options --nproc=$nproc_tmp )
    endif
    breaksw
  case --compiler*:
    set compiler_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$compiler_tmp" == "" ) then
      echo "error: empty compiler defined"
      exit 1
    else
      set source_options = ( $source_options --compiler=$compiler_tmp )
    endif
    breaksw
  case --component*:
    set component_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$component_tmp" == "" ) then
      echo "error: empty component defined"
      exit 1
    else
      set source_options = ( $source_options --component=$component_tmp )
      set finalize_options = ( $finalize_options --component=$component_tmp )
    endif
    breaksw
  case --target*:
    set target_tmp=`echo $arg | awk 'BEGIN{FS="="}{print $2}'`
    if ( "$target_tmp" == "" ) then
      echo "error: empty target defined"
      exit 1
    else
      set source_options = ( $source_options --target=$target_tmp )
      set finalize_options = ( $finalize_options --target=$target_tmp )
    endif
    breaksw
  case --help:
    echo "Usage: install [options]"
    echo "Options:"
    echo "  --prefix=<path>     Determines PHASER installation path, which"
    echo "                      will be <path>/phaser-[version]"
    echo "  --overwrite         Forces overwriting of installed packages"
    echo "  --static_libraries  Compile static libraries"
    echo "  --static_exe        Compile static executable"
    echo "  --compiler=<compiler>  Use specified compiler"
    echo "  --nproc=number      Number of processors to use in compilation"
    echo "  --no-compile         Do not compile 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: unknown option $arg"
    exit 1
  endsw
end

echo " "
echo "=========================================================================="
echo "                      PHASER Installation"
echo " "
echo "                      version: $PHASER_VERSION"
echo "                      machine type: $PHASER_MTYPE"
echo "                      OS version: $PHASER_MVERSION"
if ( "$compiler_tmp" == "" ) then
  echo "                      compiler: native"
else
  echo "                      compiler: $compiler_tmp"
endif
echo "                      destination: $PHASER_LOC"
echo "=========================================================================="
echo " "

#
# ==========================================================================
# Check for supported architecture
# ==========================================================================
#

if ( "$PHASER_MTYPE" == "unknown-unknown" ) then
  echo " "
  echo "=============================== ERROR ==================================="
  echo "machine type not recognized, installation aborted"
  echo "=============================== ERROR ==================================="
  exit 1
endif

set supported = 0
foreach machine_type ( `cat $PHASER_TAR/conf/supported` )
  if ( "$machine_type" == "$PHASER_MTYPE" ) then
    set supported = 1
  endif
end

if ( ! $supported ) then
  echo " "
  echo "=============================== ERROR ==================================="
  echo "$PHASER_MTYPE is not a supported platform, installation aborted"
  echo "=============================== ERROR ==================================="
  exit 1
endif

if ( ! $binary_inst ) then

#
# ==========================================================================
# Call installation from source
# ==========================================================================
#

echo " "
echo "=========================================================================="
echo "                      Installing from source files"
echo "=========================================================================="
echo " "
echo "source root directory:"
echo "  $PHASER_TAR"
echo " "
$PHASER_TAR/bin/install-from-source --from=$PHASER_TAR --to=$PHASER_LOC \
                                    --arch=$PHASER_MTYPE \
                                    --version=$PHASER_VERSION \
				      $source_options
if ( $status ) then
  exit 1
endif

endif

#
# ==========================================================================
# Call installation finalization
# ==========================================================================
#

echo " "
echo "=========================================================================="
echo "                         Finalizing installation"
echo "=========================================================================="
echo " "
$PHASER_TAR/bin/install-finalize --from=$PHASER_TAR --to=$PHASER_LOC \
                                 --arch=$PHASER_MTYPE \
                                 --version=$PHASER_VERSION \
				 $finalize_options

#
# ==========================================================================
# Finished
# ==========================================================================
#

echo " "
echo "=========================================================================="
echo "                                Finished"
echo "=========================================================================="
echo " "

