#!/bin/sh

create_shell_script () {
  shell_file=$1
  definition_file=$2
  install_dir=$3
  whichone=$4

  shell_script="${HOME}/define_cdf.sh"

  trap 'rm -f $shell_script; exit' 1 2 3 15 
  if [ -s $shell_script ];
  then
    rm -f $shell_script
  fi

  exec > $shell_script

  echo "###########################################" 
  echo "#  CDF environment variables declaration"   
  echo "###########################################"
  if [ $whichone = 1 ]
  then
    echo "source $definition_file" 
    if [ -n "${CLASSPATH:+x}" ]
    then
      echo "setenv CLASSPATH .:\${CDF_BASE}/cdfjava/classes/cdfjava.jar:\${CDF_BASE}/cdfjava/cdftools/CDFToolsDriver.jar:\${CDF_BASE}/cdfjava/cdfml/cdfml.jar:\${CLASSPATH}" 
    else
      echo "setenv CLASSPATH .:\${CDF_BASE}/cdfjava/classes/cdfjava.jar:\${CDF_BASE}/cdfjava/cdftools/CDFToolsDriver.jar:\${CDF_BASE}/cdfjava/cdfml/cdfml.jar" 
    fi
    if [ -n "${DYLD_LIBRARY_PATH:+x}" ]
    then
      echo "setenv DYLD_LIBRARY_PATH .:\${CDF_BASE}/cdfjava/lib:\${CDF_LIB}:\${DYLD_LIBRARY_PATH}" 
    else
      echo "setenv DYLD_LIBRARY_PATH .:\${CDF_BASE}/cdfjava/lib:\${CDF_LIB}" 
    fi
  else
    echo ". $definition_file" 
    if [ -n "${CLASSPATH:+x}" ]
    then
      echo "export CLASSPATH=.:\${CDF_BASE}/cdfjava/classes/cdfjava.jar:\${CDF_BASE}/cdfjava/cdftools/CDFToolsDriver.jar:\${CDF_BASE}/cdfjava/cdfml/cdfml.jar:\${CLASSPATH}" 
    else
      echo "export CLASSPATH=.:\${CDF_BASE}/cdfjava/classes/cdfjava.jar:\${CDF_BASE}/cdfjava/cdftools/CDFToolsDriver.jar:\${CDF_BASE}/cdfjava/cdfml/cdfml.jar" 
    fi
    if [ -n "${DYLD_LIBRARY_PATH:+x}" ]
    then
      echo "export DYLD_LIBRARY_PATH=.:\${CDF_BASE}/cdfjava/lib:\${CDF_LIB}:\${DYLD_LIBRARY_PATH}" 
    else
      echo "export DYLD_LIBRARY_PATH=.:\${CDF_BASE}/cdfjava/lib:\${CDF_LIB}" 
    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
       chown $USER $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
    chown $USER $shell_file
  fi

}


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"

defaults write "${HOME}/.MacOSX/environment" CDF_BASE "$install_dir"
defaults write "${HOME}/.MacOSX/environment" DYLD_LIBRARY_PATH "$install_dir/lib:$install_dir/cdfjava/lib"
defaults write "${HOME}/.MacOSX/environment" CLASSPATH "$install_dir/cdfjava/classes/cdfjava.jar:$install_dir/cdfjava/cdftools/CDFToolsDriver.jar:$install_dir/cdfjava/cdfml/cdfml.jar"
chown -R $USER ${HOME}/.MacOSX

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
     chown $USER $shell_file
   fi
fi
create_shell_script $shell_file $definition_file $install_dir 1

shell_file="$HOME/.profile"
if [ ! -e $shell_file ];
then
  touch $shell_file
  chown $USER $shell_file
fi
definition_file="$install_dir/bin/definitions.B"
create_shell_script $shell_file $definition_file $install_dir 2

modify_definition_files="yes"
if [ $modify_definition_files = "yes" ];
then
   search="<cdf_install_dir>"
   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
