#! /bin/sh
# Filename:    ctools-setup(.in)
# Description: Writes an initialization script for ctools software
#              which is custom fit to the user and the particular
#              software installation. Note that this script is
#              designed to be invoked by ctools-init.csh/ctools-init.sh,
#              and should not normally be invoked directly by user.
# Author/Date: Juergen Knoedlseder, IRAP, July 16, 2011
#-------------------------------------------------------------------------------
#
# Set fixed parameters
# ====================
this_script=`echo $0 | sed 's:.*/::'`
ctools_info="$this_script:"
ctools_error="$ctools_info ERROR --"

#
# Extract argument
# ================
flavor=$1
if [ "x$flavor" != xcsh -a "x$flavor" != xsh ]; then
  echo "$ctools_info usage $this_script csh|sh" >&2
  exit 3
fi

#
# Set parameters
# ==============
prefix="/opt/anaconda1anaconda2anaconda3"
exec_prefix="${prefix}"
ctools_bin="${exec_prefix}/bin"
ctools_lib="${exec_prefix}/lib"
uname_system=`uname`
python_dir="${prefix}/lib/python3.6/site-packages"
python_exec_dir="${exec_prefix}/lib/python3.6/site-packages"
pkgconfig_dir="${exec_prefix}/lib/pkgconfig"
man_dir="${prefix}/share/doc/ctools/man"
caldb_dir="${prefix}/share/caldb"

#
# Make pfiles available (this is needed for an installation where the
# ctools reside in a directory different from GammaLib, as the
# system does not know in this case where the syspfiles directory is
# ===================================================================
if [ -d $prefix/syspfiles/. ]; then

  # make sure local parameter file directory exists
  if [ ! -w $HOME/pfiles/. ]; then
    mkdir $HOME/pfiles
    if [ $? -ne 0 ]; then
      echo "$ctools_error cannot create local pfiles directory $HOME/pfiles" >&2
      exit 3
    fi
  fi

  # use PFILES (if set) to set initial values for locpfiles and syspfiles
  if [ "x$PFILES" != x ]; then
    syspfiles=`echo $PFILES | sed "s%.*;%%"`
    locpfiles=`echo $PFILES | sed "s%;*$syspfiles$%%"`
  fi

  # move $HOME/pfiles to front of locpfiles and weed out redundancies
  if [ "x$locpfiles" = x ]; then
    locpfiles="$HOME/pfiles"
  else
    locpfiles=`echo ":$locpfiles:" | \
      sed "s%:$HOME/pfiles:%:%g" | sed "s%::*$%%"`
    locpfiles="$HOME/pfiles$locpfiles"
  fi

  # move $prefix/syspfiles to front of syspfiles and weed out redundancies
  if [ "x$syspfiles" = x ]; then
    syspfiles="$prefix/syspfiles"
  else
    syspfiles=`echo ":$syspfiles:" | \
      sed "s%:$prefix/syspfiles:%:%g" | sed "s%::*$%%"`
    syspfiles="$prefix/syspfiles$syspfiles"
  fi

  # Set PFILES and PFCLOBBER environment variable
  PFILES="$locpfiles;$syspfiles"
  PFCLOBBER=1
fi

#
# Set PYTHONPATH
# ==============
if [ -z "${PYTHONPATH}" ]; then
  PYTHONPATH=$python_dir:$python_exec_dir
else
  PYTHONPATH=$python_dir:$python_exec_dir:$PYTHONPATH
fi

#
# Set PKG_CONFIG_PATH
# ===================
if [ -z "${PKG_CONFIG_PATH}" ]; then
  PKG_CONFIG_PATH=$pkgconfig_dir
else
  PKG_CONFIG_PATH=$pkgconfig_dir:$PKG_CONFIG_PATH
fi

#
# Set PATH
# ========
if [ -z "${PATH}" ]; then
  PATH=$ctools_bin
else
  PATH=$ctools_bin:$PATH
fi

#
# Set MANPATH
# ==============
if [ -z "${MANPATH}" ]; then
  if `which manpath > /dev/null 2>&1` ; then
    default_manpath=`manpath`
  else
    default_manpath=`man -w 2> /dev/null`
  fi
fi
if [ -z "${MANPATH}" ]; then
  MANPATH=$man_dir:${default_manpath}; export MANPATH
else
  MANPATH=$man_dir:$MANPATH; export MANPATH
fi

#
# Set CALDB (if not yet set)
# ==========================
if [ -z "${CALDB}" ]; then
  CALDB=$caldb_dir
fi

#
# List of variables needed in initialization script
# =================================================
varlist="PATH"
varlist="$varlist PYTHONPATH PKG_CONFIG_PATH MANPATH"
varlist="$varlist PFILES PFCLOBBER CALDB"

#
# Set LD_LIBRARY_PATH
# ===================
case $uname_system in
  Darwin*)
    ;;
  *)
    if [ -z "${LD_LIBRARY_PATH}" ]; then
      LD_LIBRARY_PATH=$ctools_lib
    else
      LD_LIBRARY_PATH=$ctools_lib:$LD_LIBRARY_PATH
    fi
    varlist="$varlist LD_LIBRARY_PATH"
    ;;
esac

#
# Save values in output initialization shell scripts
# ==================================================
# Name of temporary config script
HOST_NAME=`hostname`
ctools_config="$HOME/ctools-config-$HOST_NAME"

# Bourne Shell version, write only for sh flavor initialization
if [ $flavor = sh ]; then
  rm -f $ctools_config$$.sh
  touch $ctools_config$$.sh
  for var in $varlist; do
    eval "if [ \"x\$$var\" != x ]; then
            echo \"\$var=\\\"\$$var\\\"; export \$var\" >> $ctools_config$$.sh;
          fi"
  done
  echo "$ctools_config$$.sh"
# C Shell version, write only for csh flavor initialization
elif [ $flavor = csh ]; then
  rm -f $ctools_config$$.csh
  touch $ctools_config$$.csh
  for var in $varlist; do
    eval "if [ \"x\$$var\" != x ]; then
            echo setenv \$var \\\"\$$var\\\" >> $ctools_config$$.csh;
          fi"
  done
  echo "$ctools_config$$.csh"
fi
