#! /bin/sh

function create_shell_script
{
shell_file=$1
definition_file=$2
install_dir=$3

if [ $shell_file = "/bin/tcsh" -o $SHELL = "/bin/csh" ]
then
    shell_script="$install_dir/define_cdf.csh"
else
    shell_script="$install_dir/define_cdf.sh"
fi

echo ${CLASSPATH=undefined} > /dev/null
echo ${DYLD_LIBRARY_PATH=undefined} > /dev/null

trap 'rm -f $shell_script; exit' 1 2 3 15 
exec > $shell_script

echo "###########################################" 
echo "#  CDF environment variables declaration"   
echo "###########################################"
if [ $SHELL = "/bin/tcsh" -o $SHELL = "/bin/csh" ]
then
   echo "source $definition_file" 
   if [ $CLASSPATH = "undefined" ]
   then
      echo "setenv CLASSPATH .:\${CDF_BASE}/cdfjava/classes/cdfjava.jar:\${CDF_BASE}/cdfjava/cdftools/CDFToolsDriver.jar:\${CDF_BASE}/cdfjava/cdfml/cdfml.jar" 
   else
      echo "setenv CLASSPATH \${CDF_BASE}/cdfjava/classes/cdfjava.jar:\${CDF_BASE}/cdfjava/cdftools/CDFToolsDriver.jar:\${CDF_BASE}/cdfjava/cdfml/cdfml.jar:\${CLASSPATH}:.:" 
   fi
   if [ $DYLD_LIBRARY_PATH = "undefined" ]
   then
      echo "setenv DYLD_LIBRARY_PATH .:\${CDF_BASE}/cdfjava/lib:\${CDF_LIB}" 
   else
      echo "setenv DYLD_LIBRARY_PATH \${CDF_BASE}/cdfjava/lib:\${CDF_LIB}:\${DYLD_LIBRARY_PATH}:.:" 
   fi

else
    echo ". $definition_file" 
    if [ $CLASSPATH = "undefined" ]
    then
       echo "export CLASSPATH; CLASSPATH=.:\${CDF_BASE}/cdfjava/classes/cdfjava.jar:\${CDF_BASE}/cdfjava/cdftools/CDFToolsDriver.jar:\${CDF_BASE}/cdfjava/cdfml/cdfml.jar" 
    else
       echo "export CLASSPATH; CLASSPATH=\${CDF_BASE}/cdfjava/classes/cdfjava.jar:\${CDF_BASE}/cdfjava/cdftools/CDFToolsDriver.jar:\${CDF_BASE}/cdfjava/cdfml/cdfml.jar:\${CLASSPATH}:.:" 
    fi
    if [ $DYLD_LIBRARY_PATH = "undefined" ]
    then
       echo "export DYLD_LIBRARY_PATH; DYLD_LIBRARY_PATH=.:\${CDF_BASE}/cdfjava/lib:\${CDF_LIB}" 
    else
       echo "export DYLD_LIBRARY_PATH; DYLD_LIBRARY_PATH=\${CDF_BASE}/cdfjava/lib:\${CDF_LIB}:\${DYLD_LIBRARY_PATH}:.:" 
    fi
fi

exec >&2     # Restore the standard output

if [ ! -x $shell_script ]
then
   chmod +x $shell_script
fi

modify_shell_file="yes"
if [ $modify_shell_file = "yes" ]
then
    # If the default shell file (.cshrc or .profile) doesn't exist, create one.
    if [ ! -s $shell_file ]
    then
       touch $shell_file
    else
       # Before removing the existing CDF environment variables definition
       # lines, replace '\' with '@@^' since the 'read' command removes '\'.
       search='\\'
       replace='@@^'
       ed - $shell_file << end
       1,\$s/$search/$replace/g
       w
       q
end
       remove_cdf_env_vars $shell_file

       # Restore the backshash
       ed - $shell_file << end1
       1,\$s/$replace/$search/g
       w
       q
end1
    fi
    # Put the shell script at the end of the shell file
    ed - $shell_file << insert
       \$r $shell_script
       w
       q
insert
    # rm -f $shell_script
    chmod 775 $shell_file
fi
}


function remove_cdf_env_vars {
input_file=$1
output_file="cshrc$$"
while read rec
do
   if [ "$rec" = "###########################################" ]
   then
      read rec1
      if [ "$rec1" = "#  CDF environment variables declaration" ]
      then
          for i in `jot 4`
          do
              read rec
          done      
      else
          echo "$rec" 1>&3
          echo "$rec1" 1>&3
      fi
   else
      echo "$rec" 1>&3
   fi
done <$input_file 3>"$output_file"
rm -f $input_file
mv $output_file $input_file
}


############################################################################
#  $2 = the name of the installation directory provided Mac OS X Installer
############################################################################
install_dir="$2/cdf31-dist"    

defaults write "${HOME}/.MacOSX/environment" CDF_BASE $install_dir
defaults write "${HOME}/.MacOSX/environment" DYLD_LIBRARY_PATH "$install_dir/lib:$install_dir/cdfjava/lib"

if [ $SHELL = "/bin/tcsh" -o $SHELL = "/bin/csh" ]
then
   definition_file="$install_dir/bin/definitions.C"
   # In tcsh, .tcshrc is read first over .cshrc.
   # If .tcshrc doesn't exist, use the .cshrc file if exists.
   # If .cshrc doesn't exist, use the .tcshrc file.
   shell_file="$HOME/.tcshrc"
   if [ ! -e $shell_file ]  
   then
      shell_file="$HOME/.cshrc"
      if [ ! -e $shell_file ]
      then
         shell_file="$HOME/.tcshrc"
         touch $shell_file
      fi
   fi
   create_shell_script $shell_file $definition_file $install_dir

elif [ $SHELL = "/bin/bash" -o $SHELL = "/bin/sh" ]
then
   shell_file="$HOME/.profile"
   definition_file="$install_dir/bin/definitions.B"
   create_shell_script $shell_file $definition_file $install_dir
fi

modify_definition_files="yes"
if [ $modify_definition_files = "yes" ]
then
   search="<cdf_dir>/cdf-dist"
   replace="$install_dir"
   for file in `ls $install_dir/bin/definitions.*`
   do
       echo "Modifying the definition file `basename $file` .."
       ed - $file << editend
       1,\$s:$search:$replace:g
       w
       q
editend
   done
fi


exit 0
