# The original location of this file is: $MCTDH_DIR/install/mctdhrc
# Copy this file to ~/.mctdhrc  (for bash (and ksh ?) users only).
# The .bashrc will then (after MCTDH is installed) source this file.

# MCTDH_KB is read by mcb and mcg.
# If you are working on a german or french keyboard,
# set the environment variable MCTDH_KB to german or french.
# Otherwise unset MCTDH_KB .
# unset MCTDH_KB
# export MCTDH_KB=german
# export MCTDH_KB=french

  if [ -z ${LANG#*.UTF-8} ]; then
    alias mcb="$MCTDH_DIR/bin/mcb_utf"
    alias mcg="$MCTDH_DIR/bin/mcg_utf"
  else
    unalias mcg  > /dev/null  2>&1
    unalias mcb  > /dev/null  2>&1
  fi


# Give the full absolute path of the ELK_TEST comparison data set.
# export MCTDH_ELK=${MCTDH_DIR%/*}/ELK/data

# Give the full absolute path of the MCTDH backup directory.
# export MCTDH_BACKUP=${MCTDH_DIR%/*}/MBACKUP

# Give the full absolute path of the MCTDH addsurf directory.
 export MCTDH_ADDSURF=${MCTDH_DIR%/*}/addsurf


#####------------  DEFINING SEVERAL BASH FUNCTIONS ---------------#####
#####  mctdh_check ,  mctdh_link ,  cdm_setup ,  cdm ,  minstall  #####


# Defining the bash function mctdh_check
mctdh_check(){
  if [ -z "$MCTDH_DIR" ] ; then
     check_ok=1
     echo '============================================================='
     echo '.mctdhrc: WARNING: $MCTDH_DIR is not set!'
     echo '============================================================='
  elif [ ! -d $MCTDH_DIR ] ; then
     check_ok1=1
     echo '============================================================='
     echo ' .mctdhrc: WARNING: The directory:'
     echo ' $MCTDH_DIR = '$MCTDH_DIR
     echo ' does not exist!'
     echo '============================================================='
  else
     check_ok1=0
  fi
  check_ok2=0
  unset PathFile
  if [ -f "$MCTDH_DIR/source/include/paths.inc" ]; then
     PathFile=$MCTDH_DIR/source/include/paths.inc
  elif [ -f "$MCTDH_DIR/source/include/paths.f90" ]; then
     PathFile=$MCTDH_DIR/source/include/paths.f90
  else
     check_ok2=1
     echo '============================================================='
     echo ' .mctdhrc: WARNING: The directory:'
     echo ' $MCTDH_DIR/source/include/paths.(inc|f90)'
     echo ' does not exist!'
     echo '============================================================='
  fi
  if [ -n "$PathFile" ] && [ -z "$(grep $MCTDH_DIR $PathFile)" ] ; then
     check_ok3=1
     echo '============================================================='
     echo ".mctdhrc: WARNING: $MCTDH_DIR is not correctly installed!"
     echo "The file $PathFile is incorrect."
     echo '============================================================='
  else
     check_ok3=0
  fi
  if [ -z "$(grep MCTDH_FFLAGS $MCTDH_DIR/install/compile.cnf)" ] ; then
     check_ok4=1
     echo '============================================================='
     echo ".mctdhrc: WARNING: $MCTDH_DIR is not correctly installed!"
     echo 'The file source/include/compile.cnf is incorrect.'
     echo '============================================================='
  else
     check_ok4=0
  fi
}


# Defining the bash function mctdh_link
mctdh_link(){
   if [ -e $HOME/mctdh ] && ! [ -L $HOME/mctdh ] ; then
      echo '.mctdhrc: There exist a file ~/mctdh ! I cannot make the link.'
   elif [ ! -L $HOME/mctdh ] ; then
      ln -s $MCTDH_DIR $HOME/mctdh
      echo 'Installed link ~/mctdh -> '$MCTDH_DIR
   else
#  Inspect if  ~/mctdh/install/Makefile and $MCTDH_DIR/install/Makefile
#  are the same files. If not, re-install the link.
      i1=`ls -i  $HOME/mctdh/install/Makefile `
      i2=`ls -i  $MCTDH_DIR/install/Makefile `
      i1=`echo $i1 | cut -d" " -f1 `
      i2=`echo $i2 | cut -d" " -f1 `
      i1=${i1:=0} ; i2=${i2:=$i1}
      if [ "$i1" != "$i2" ] ; then
         /bin/rm -f $HOME/mctdh
         ln -s $MCTDH_DIR $HOME/mctdh
         echo 'Re-installed link ~/mctdh -> '$MCTDH_DIR
      fi
   fi
}



# Defining the bash-function cdm_setup.
# This will be used by the bash-function cdm.
cdm_setup() {
 # Find all dirs under MCTDH, except obsolete, source.* or source_*
 # alld=`find $MCTDH_DIR -type d | egrep -v 'obsolete$' | \
 #       egrep -v "${MS}\." | egrep -v "${MS}_" | egrep -v ".svn"`
   alld=`find $MCTDH_DIR -type d \! \
         \( -name 'obsolete' -prune -o \
            -path "${MS}.*" -prune -o \
            -path "${MS}_*" -prune -o \
            -name ".svn" -prune -o \
            -name ".hg" -prune \)`
   alls=`echo "$alld" | grep  $MS`    # Source Dirs only.
   allr=`echo "$alld" | grep -v $MS`  # Except source dirs.
   alld=`echo -e "${alls}\n$allr"`    # Putting source-files before all others.
}


# Defining the bash-function cdm.
cdm() {
   # initialise list of dirs on first call of cdm
   ms=`echo "$alld" | head -n 1`
   if [ "$ms" != "$MS" ] ; then
      cdm_setup
   fi
   if [ "$1" = "-h" ] ; then
      echo '-------------------------------------------------------------'
      echo '"cdm" is equivalent to "cd $MCTDH_DIR" .'
      echo '"cdm <dir>" will cd to the directory <dir>, if this directory'
      echo 'is below $MCTDH_DIR . Abbreviations are allowed.'
      echo 'I.e. "cdm ana" will cd to $MCTDH_DIR/source/analyse'
      echo 'and  "cdm gu"  will cd to $MCTDH_DIR/doc/guide .'
      echo 'Use the -c option to clear the alld list.'
      echo 'This may be necessary if a directory is not found.'
      echo '-------------------------------------------------------------'
      return
    elif [ "$1" = -c ] ; then
      unset alld
      cdm_setup
      return
    elif [ -z "$1" ] ; then
       cd $MCTDH_DIR
    elif [ -d $MCTDH_DIR/$1 ] ; then
       cd $MCTDH_DIR/$1
    else
       unset aa
       bb=${1%/}
       aa=`echo "$alld" | egrep $MCTDH_DIR.*/$bb$`
       aa=`echo $aa  | cut -d" " -f1`
       if [ -z "$aa" ] ; then
          aa=`echo "$alld" | egrep $MCTDH_DIR.*/$bb`
          aa=`echo $aa  | cut -d" " -f1`
          if [ -z "$aa" ] ; then
             echo "No such directory under $MCTDH_DIR : $bb"
          else
            cd $aa
          fi
       else
          cd $aa
       fi
    fi
}


#  Defining the bash-function minstall.
minstall()  {
   if [ "$1" = "-h" ] ; then
      echo '-----------------------------------------------------------'
      echo 'Typing: "minstall <path-of-second-MCTDH-directory>"'
      echo 'will make the second-MCTDH-directory active.'
      echo 'If the option -c is given, the PATH will be cleared'
      echo 'from previous mctdh entries.'
      echo 'Option -C clears the PATH and the environment variables'
      echo 'from all MCTDH entries (and does nothing else).'
      echo 'If the option -l is given, the following link will be set:'
      echo '   $HOME/mctdh -> path-of-second-MCTDH-directory'
      echo '-----------------------------------------------------------'
      return
   fi
   unset linkRC
   unset clearRC
   if [ "$1" = "-C" ] ; then
      newpathRC=`echo $PATH | tr ':' '\n' | sed -e '/.*mctdh.*bin.*/d' |\
                  tr '\n' ':' `
      PATH=$newpathRC
      envlist=`env | grep MCTDH_ | cut -d= -f1`
      unset $envlist
      return
   fi
   if [ "$1" = "-l" ] ; then
      linkRC=1
      shift
   fi
   if [ "$1" = "-c" ] ; then
      clearRC=1
      newpathRC=`echo $PATH | tr ':' '\n' | sed -e '/.*mctdh.*bin.*/d' |\
                  tr '\n' ':' `
      shift
   fi
   if [ "$1" = "-l" ] ; then
      linkRC=1
      shift
   fi

   in=${1:-.}  # If no argument is given, use . (current dir).

   if [ ! -d $in ] ; then
      echo "The directory $in does not exist!"
      return
   fi
   opath=$(pwd)
   cd $in
   mpath=$(pwd)
   mpath=${mpath%/}
   if [ ! -f $mpath/install/mctdh.profile ] ; then
      echo "$mpath does not seem to be a MCTDH directory!"
      cd $opath
      return
   fi
   if [ -n "$clearRC" ]; then PATH=$newpathRC ; fi
   export MCTDH_DIR=$mpath
   . $MCTDH_DIR/install/mctdh.profile
   export MM=$MCTDH_DIR
   export MS=$MCTDH_DIR/source
   cdm_setup
   if [ -n "$linkRC" ] ; then mctdh_link ; fi
   echo "New MCTDH-Directory is: $MCTDH_DIR"
   cd $opath
   return
}



######---------   HERE STARTS THE SCRIPT  ----------########


mctdh_check

if [ "$check_ok1" = "0" ] && [ "$check_ok2" = "0" ] && \
   [ "$check_ok3" = "0" ] && [ "$check_ok4" = "0" ] ; then

# These environment variables and the alias are very convenient.
   export MM=$MCTDH_DIR
   export MS=$MCTDH_DIR/source
   alias brc='. $HOME/.bashrc'

# Generate a link ~/mctdh -> $MCTDH_DIR
   mctdh_link

fi

#####---------------  END OF MCTDHRC  --------------########

