#!/bin/sh

VERSION=3.7.1
macversion=`uname -r | cut -f1 -d.`

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

  myclasspath=".:\$CDF_BASE/cdfjava/classes/cdfjava.jar:\$CDF_BASE/cdfjava/cdfj/cdfj.jar:\$CDF_BASE/cdfjava/cdftools/CDFToolsDriver.jar:\$CDF_BASE/cdfjava/cdfml/cdfml.jar"
  mylibrarypath=".:\$CDF_BASE/cdfjava/lib:\$CDF_BASE/lib"

  exec > $shell_script

  echo "###########################################" 
  echo "#  CDF environment variables declaration"   
  echo "###########################################"
  if [ $whichone -eq 1 ]
   then
    echo "source $definition_file" 
    if [ -n "${CLASSPATH:+x}" ]
     then
      echo "setenv CLASSPATH $myclasspath:\${CLASSPATH}"
     else
      echo "setenv CLASSPATH $myclasspath"
    fi
    if [ $macversion -lt 15 ]
     then
       if [ -n "${DYLD_LIBRARY_PATH:+x}" ]
        then
          echo "setenv DYLD_LIBRARY_PATH $mylibrarypath:\${DYLD_LIBRARY_PATH}" 
        else
          echo "setenv DYLD_LIBRARY_PATH $mylibrarypath"
       fi
    fi
  else
    echo ". $definition_file" 
    if [ -n "${CLASSPATH:+x}" ]
     then
      echo "export CLASSPATH=$myclasspath:\${CLASSPATH}"
     else
      echo "export CLASSPATH=$myclasspath"
    fi
    if [ $macversion -lt 15 ]
     then
       if [ -n "${DYLD_LIBRARY_PATH:+x}" ]
        then
          echo "export DYLD_LIBRARY_PATH=$mylibrarypath:\${DYLD_LIBRARY_PATH}" 
        else
          echo "export DYLD_LIBRARY_PATH=$mylibrarypath"
       fi
    fi
  fi
  exec >&2     # Restore the standard output

  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 -f $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 backslash
       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 -f $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"

chown -f $USER $install_dir
chown -f -R $USER $install_dir/*
HOST_CDF=$install_dir/../cdf
DIRHOME=`dirname $install_dir`
APPDIR="/Applications"
if [[ ${DIRHOME} == "${APPDIR}"* ]] ; then
  MYBASE=`basename $install_dir`
  rm -f $HOST_CDF
  cd $install_dir/..
  ln -s $MYBASE cdf
fi
mkdir ${HOME}/lib
mkdir ${HOME}/Library/Java
mkdir ${HOME}/Library/Java/Extensions
cp $install_dir/lib/libcdf.a ${HOME}/lib
cp $install_dir/lib/libcdf.dylib ${HOME}/lib/libcdf.${VERSION}.dylib
cd ${HOME}/lib
rm -f libcdf.dylib
ln -s libcdf.${VERSION}.dylib libcdf.dylib

cp $install_dir/cdfjava/lib/libcdfNativeLibrary.jnilib ${HOME}/Library/Java/Extensions/libcdfNativeLibrary.${VERSION}.jnilib
cd ${HOME}/Library/Java/Extensions
rm -f libcdfNativeLibrary.jnilib
ln -s libcdfNativeLibrary.${VERSION}.jnilib libcdfNativeLibrary.jnilib

# 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}/.cshrc"
if [ ! -e $shell_file ]; then
   shell_file="${HOME}/.tcshrc"
   if [ ! -e $shell_file ]; then
     shell_file="${HOME}/.cshrc"
     touch $shell_file
     chown -f $USER $shell_file
   fi
fi
definition_file="$install_dir/bin/definitions.C"
create_shell_script $shell_file $definition_file $install_dir 1
replaceC=$shell_file

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

searchB="<Bshell>"
searchC="<Cshell>"
remove_shell_file="$install_dir/remove_env.sh"
ed - $remove_shell_file << editend
  1,\$s:$searchB:$replaceB:g
  1,\$s:$searchC:$replaceC:g
  w
  q
editend
chmod 775 $remove_shell_file

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
