#!/bin/tcsh -f

# mk_setenv.csh
#
# Jan. 6, 2009  David Lawrence
#
# Generate a script for setting up the environment for using the Hall-D
# software based on the current environment.
# 

# Set the output file name and make sure it doesn't already exist.
# We won't overwrite an existing file.
set fname="setenv_"$BMS_OSNAME".csh"
if ( -e $fname ) then
	echo " "
	echo "File "'"'$fname'"'" already exists!"
	echo "Move it out of the way and try again."
	echo " "
	exit
endif

echo "Generating file $fname ..."

# Header
touch $fname
echo "#\!/bin/tcsh" >> $fname
echo " " >> $fname
echo "# This file was generated by the script "'"'"mk_setenv.csh"'"' >> $fname
echo "#" >> $fname
echo -n "# Generation date: " >> $fname
date >> $fname
echo -n "# User: " >> $fname
whoami >> $fname
echo -n "# Host: " >> $fname
hostname >> $fname
echo -n "# Platform: ">>$fname
uname -a >> $fname
echo "# BMS_OSNAME: $BMS_OSNAME" >> $fname
echo " " >> $fname

# (DY)LD_LIBRARY_PATH
# On BSD systems, DYLD_LIBRARY_PATH is used while on most others
# (including Linux) LD_LIBRARY_PATH is used. Here, we set the LDLPV
# variable to name of the variable to set. We determine this by whether
# or not DYLD_LIBRARY_PATH is set. At the same time, we make sure the
# variable is at least defined (empty if needed) so the rest of the
# generated script can just add to it without checking for existence.
set LDLPV="LD_LIBRARY_PATH"
if ( $?DYLD_LIBRARY_PATH ) then
	set LDLPV="DYLD_LIBRARY_PATH"
endif
echo "# Make sure $LDLPV is set" >> $fname
echo "if ( "'!'" "'$''?'"$LDLPV ) then" >> $fname
echo "   setenv $LDLPV" >> $fname 
echo "endif" >> $fname
echo " " >> $fname

# CLANG-LLVM C++ compiler
# If the CLANGROOT environment variable is set, check if there is
# a setenv.csh script in there that we can source.
if ( $?CLANGROOT ) then
  if ( -e $CLANGROOT/setenv.csh ) then
    echo "# CLANG-LLVM C++ compiler" >> $fname
    echo "if ( -e $CLANGROOT/setenv.csh ) then" >> $fname
    echo "  source $CLANGROOT/setenv.csh" >> $fname
    echo "endif" >> $fname
    echo " " >> $fname
  endif
endif


# HALLD
echo "# HALLD" >> $fname
echo "setenv HALLD_HOME $HALLD_HOME" >> $fname
echo "setenv HDDS_HOME $HDDS_HOME" >> $fname
echo "setenv BMS_OSNAME $BMS_OSNAME" >> $fname
echo "setenv PATH "'$'"{HALLD_HOME}/bin/"'$'"{BMS_OSNAME}:"'$'"PATH" >> $fname
echo " " >> $fname

# JANA
echo "# JANA" >> $fname
echo "setenv JANA_HOME $JANA_HOME" >> $fname
echo "setenv JANA_CALIB_URL $JANA_CALIB_URL" >> $fname
echo "setenv JANA_GEOMETRY_URL xmlfile://"'$'"{HDDS_HOME}/main_HDDS.xml" >> $fname
echo "setenv JANA_PLUGIN_PATH "'$'"{JANA_HOME}/lib" >> $fname
echo "setenv PATH "'$'"{JANA_HOME}/bin:"'$'"PATH" >> $fname
echo " " >> $fname

# CCDB
echo "# CCDB" >> $fname
if ( $?CCDB_HOME ) then
	echo "setenv CCDB_HOME $CCDB_HOME" >> $fname
	echo "if ( -e "'$'"CCDB_HOME/environment.csh ) then" >> $fname
	echo "  source "'$'"CCDB_HOME/environment.csh" >> $fname
	echo "endif" >> $fname
endif
if ( $?CCDB_CONNECTION ) then
	echo "setenv CCDB_CONNECTION $CCDB_CONNECTION" >> $fname
endif
echo " " >> $fname

# ROOT
echo "# ROOT" >> $fname
echo "setenv ROOTSYS $ROOTSYS" >> $fname
echo "setenv $LDLPV "'$'"{ROOTSYS}/lib:"'$'"$LDLPV" >> $fname
echo "setenv PATH "'$'"{ROOTSYS}/bin:"'$'"PATH" >> $fname
echo " " >> $fname

# CERNLIB
echo "# CERNLIB" >> $fname
if ( $?CERN ) then
echo "setenv CERN $CERN" >> $fname
echo "setenv CERN_LEVEL $CERN_LEVEL" >> $fname
echo "setenv $LDLPV "'$'"{CERN}/"'$'"{CERN_LEVEL}/lib:"'$'"$LDLPV" >> $fname
echo "setenv PATH "'$'"{CERN}/"'$'"{CERN_LEVEL}/bin:"'$'"PATH" >> $fname
echo " " >> $fname
endif

# Java
if ( $?JAVAROOT ) then
echo "# Java" >> $fname
echo "setenv JAVAROOT $JAVAROOT" >> $fname
echo " " >> $fname
endif

# Xerces
echo "# Xerces" >> $fname
echo "setenv XERCESCROOT $XERCESCROOT" >> $fname
echo "setenv $LDLPV "'$'"{XERCESCROOT}/lib:"'$'"$LDLPV" >> $fname
echo " " >> $fname

# Here, we want to check if a plain old "setenv.csh" exists. If not,
# generate one. It's purpose will be to source the platform specific
# version, but so someone does not need to have the ugly platform
# specifier in thier .cshrc file etc.
set fname="setenv.csh"
if ( ! -e $fname ) then
	echo "Generating file $fname ..."
	
	touch $fname
	# Header
	echo "#\!/bin/tcsh" >> $fname
	echo " " >> $fname
	echo "# This file was generated by the script "'"'"mk_setenv.csh"'"' >> $fname
	echo "#" >> $fname
	echo -n "# Generation date: " >> $fname
	date >> $fname
	echo -n "# User: " >> $fname
	whoami >> $fname
	echo -n "# Host: " >> $fname
	hostname >> $fname
	echo -n "# Platform: ">> $fname
	uname -a >> $fname
	echo "# BMS_OSNAME: $BMS_OSNAME" >> $fname
	echo " " >> $fname
	
	echo "if ( ! "'$''?'"BMS_OSNAME ) then" >> $fname
	echo "   setenv BMS_OSNAME "'`'"/group/halld/Software/scripts/osrelease.pl"'`' >> $fname
	echo "endif" >> $fname
	echo " " >> $fname

	echo "if ( -e $PWD/setenv_"'$'"{BMS_OSNAME}.csh ) then" >> $fname
	echo "    # Try prepending path of cwd used in generating this file" >> $fname
	echo "    source $PWD/setenv_"'$'"{BMS_OSNAME}.csh" >> $fname
	echo "else if ( -e setenv_"'$'"{BMS_OSNAME}.csh ) then" >> $fname
	echo "    source setenv_"'$'"{BMS_OSNAME}.csh" >> $fname
	echo "endif " >> $fname
	echo " " >> $fname
endif

echo "Done."
