#!/bin/bash

## ------------------- install_mctdh -------------------------------- ##
##                                                  GW and HDM 01/00  ##
########################################################################
#  This script installs the MCTDH program package.                     #
#  It sets the MCTDH_DIR environment variable and writes it to the     #
#  configuration file ~/.bashrc or ~/.profile or ~./cshrc.             #
#  It sets up the include file ../include/paths.inc and then           #
#  compiles all programs and latex-compiles the documentation.         #
########################################################################
##                                                                    ##
########################################################################
#  Here one may set a string that can be ignored from all paths.       #
#  I.e. $ignore/path and  $substi/path are both valid absolute         #
#  path-names, pointing to the same file.                              #
#  See the on-line documentation for further explanation.              #
#  Set "ignore" and "substi" according to your demands and remove      #
#  the "#" to set them into action. NB this is effective only for      #
#  default installation.                                               #
#                                                                      #
#   ignore="/afs/urz.uni-heidelberg.de/usr"                            #
#   substi="/u"                                                        #
#                                                                      #
########################################################################

minu40='----------------------------------------'
minu20='--------------------'
minu10='----------'
star40='****************************************'
star20='********************'
force=0
echo ' '
echo "$star40$star40"
echo "$star20    -------  INSTALL MCTDH  -------    $star20"'*'
echo "$star40$star40"
echo ' '
echo $(date)

#-----------------------------------------------------------------------
# SETTING UP ENVIRONMENT VARIABLE MCTDH_DIR.
# The script thus may be called form any directory.
#-----------------------------------------------------------------------
bname=`basename $0`
path=${0%$bname} ; path=${path%/}
if [ -n "${path%.}" ] ; then cd $path ; fi
path=$(pwd)
path=${path%/}
path1=${path#"$ignore"}
if [ "$path1" != "$path" ] ; then  path="$substi$path1" ; fi
export MCTDH_DIR=${path%/*}

#-----------------------------------------------------------------------
# HELP
#-----------------------------------------------------------------------
if [ "$1" = "-h" ] || [ "$2" = "-h" ] ; then
    echo ' Purpose: Installation of the MCTDH package.'
    echo '          Some paths and environment variables will be set.'
    echo '          The file paths.inc will be written to'\
                    '$MCTDH_DIR/source/include/.'
    echo ' '
    echo ' Usage  : install_mctdh [ -f  -r  -u  -h ] '
    echo ' Options:  '
    echo '    -h  : print this help text.'
    echo '    -f  : enforce (re)-installing paths.inc and ignore/substi.'
    echo '    -r  : Display the README file. (Type "q" to quit reading).'
    echo '    -u  : Add a new user to an already installed package.'
    echo '          The script writes information to configuration file,'
    echo '          (e.g. .bashrc), but does nothing else. '
    echo ' '
    echo 'The MCTDH directory of this installation is: '
    echo "$MCTDH_DIR"
    echo $minu40$minu40
    exit
elif [ "$1" = "-r" ] || [ "$2" = "-r" ] ; then
    less README
    exit
elif [ "$1" = "-f" ] ; then
    force=1
    shift
elif [ "$1" = "-u" ] ; then
    user=1
    shift
elif [ -n "$1" ] ; then
    echo "Unknown option: $1"
    exit
fi

if [ "$1" = "-f" ] ; then
    force=1
elif [ "$1" = "-u" ] ; then
    user=1
elif [ -n "$1" ] ; then
    echo "Unknown option: $1"
    exit
fi


if [ -n "$user" ] ; then
   echo ' '
   echo "$minu40$minu40"
   echo ' *****  NEW USER INSTALLATION  *****'
   echo "$minu40$minu40"
   echo ' '
fi

echo 'This install script can be stopped any time with "Ctrl C". '
echo 'If you think you have made a wrong choice, just run it again! '
echo "You may wish to read the $MCTDH_DIR/install/README file first,"
echo 'before continuing. To this end simply type "./install_mctdh -r" .'
echo 'See also  "./install_mctdh -h" .'
echo ' '
#-----------------------------------------------------------------------
# Determine where we are.
# DEFAULT or HEIDELBERG installation.
#-----------------------------------------------------------------------
plt=`ls platform.cnf.* | cut -c 14- | egrep -v '(hd|def|csh|priv|template)$'`
if [ -n "$plt" ] ; then
    echo "#### Installation with manual selection of platform.cnf ####"
    unset ort
elif [ -f ./platform*.priv ] ; then
    echo "#### Installation with private platform.cnf ####"
    ort=priv
elif [ -d /home/dieter/MCTDH ] && \
     [ -d /home/mctdh/PRCS  ] ; then
    ort=hd
    unset ignore
    unset substi
    echo "####  Heidelberg TC-Cluster Installation ####"
else
    ort=def
    echo "####  DEFAULT Installation ####"
fi
echo ' '
#If you do not want an automatic choice, unset ort here.
# unset ort

echo 'The MCTDH directory of this installation is: '
echo "$MCTDH_DIR"

mdir="${MCTDH_DIR#../}" ; mdir="${mdir#./}"
mdir="${mdir#..}" ; mdir="${mdir#.}"

if [ -z "$mdir" ] || [ "$mdir" = "${mdir#/}" ] ; then
echo ' '
echo '#########################################################################'
echo '#### WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING'
echo '#### $MCTDH_DIR = '"$MCTDH_DIR"
echo '#### Is the path of the MCTDH directory determined correctly?'
echo '#### $MCTDH_DIR should contain the full absolute path!'
echo '#### If incorrect, cancel this installation and re-start it by calling'
echo '#### install_mctdh with its full absolute path.'
echo '#########################################################################'
echo ' '
echo 'Type "continue" if you really want to continue.'\
     ' Any other input will exit.'
read dummy
if [ ! "$dummy" = "continue" ] ; then exit ; fi
fi


plength=`echo "$MCTDH_DIR" | wc -c`

if [ $plength -gt 140 ] ; then
echo ' '
echo '#########################################################################'
echo '#### WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING'
echo '#### $MCTDH_DIR = '"$MCTDH_DIR"
echo '#### The path of the MCTDH-Directory is '$plength' characters long.'
echo '#### A path with more than 140 characters may lead to problems.'
echo '#### A path with more than 166 characters will lead to problems!'
echo '#### You should move the MCTDH-Directory to a location with a'
echo '#### shorter absolute path name. (One may set a link.)'
echo '#### Avoid excessive long paths (>160 char) for name-directories too.'
echo '#########################################################################'
echo ' '
echo 'Type "continue" if you really want to continue.'\
     'Any other input will exit.'
read dummy
if [ ! "$dummy" = "continue" ] ; then exit ; fi
fi

if [ -d $HOME/mctdh ] && [ ! -L $HOME/mctdh ] ; then
echo ' '
echo '#########################################################################'
echo '#### WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING'
echo '#### There exists a file or directory $HOME/mctdh.'
echo '#### You should rename this file. Otherwise the convenient link.'
echo '#### $HOME/mctdh -> $MCTDH_DIR  cannot be installed.'
echo '#########################################################################'
echo ' '
echo 'Type "continue" if you really want to continue.'\
     ' Any other input will exit.'
read dummy
if [ ! "$dummy" = "continue" ] ; then exit ; fi
fi

#-----------------------------------------------------------------------
# write information to install/MCTDH_client
#-----------------------------------------------------------------------
gmuf=$MCTDH_DIR/install/MCTDH_client
configfile=install/mctdh.profile
echo '# If you are working under bash you may simply source this' > $gmuf
echo '# file and you will be able to run the present package.' >> $gmuf
echo '#'  >> $gmuf
echo "#*MCTDH*A***********$star40$star20" >> $gmuf
echo '# Following lines written by install_mctdh.  '"$(date)" >> $gmuf
echo 'export MCTDH_DIR='$MCTDH_DIR  >> $gmuf
echo ". $MCTDH_DIR/$configfile"     >> $gmuf
echo "if [ -f ~/.mctdhrc ] ; then . ~/.mctdhrc" >> $gmuf
echo "else . $MCTDH_DIR/install/mctdhrc ; fi" >> $gmuf
echo "#*MCTDH*B***********$star40$star20" >> $gmuf
chmod a+r $gmuf

#-----------------------------------------------------------------------
# write information to .profile, .bashrc or .cshrc to include paths
# These sources install/mctdh.profile to set path correctly.
#-----------------------------------------------------------------------
echo ' '
echo "$minu40$minu40"
echo ' '
echo 'For your convenience I will write some statements to your' \
     'configuration files '
echo '(i.e. to  ~/.profile or ~/.bashrc or ~/.cshrc or ~/.kshrc).'
echo "Your old ~/.XXX configuration file will be saved to ~/.save-XXX-$$."
echo ' '
echo 'Choose: b) write the commands to my ~/.bashrc file'\
     '(for bash users).'
echo 'Or    : c) rather write them to my ~/.cshrc file'\
     '(for csh and tcsh users).'
echo 'Or    : k) rather write them to my ~/.kshrc file'\
     '(kcsh users).'
echo 'Or    : p) please, rather write them to my ~/.profile file.'
echo '          (alternative for ksh and bash users (not recommended)).'
echo 'Or    : y) yes, write the commands to my ~/.bashrc file.'\
      ' (same as b))'.
echo 'Or    : n) no, do not touch my configuration files.'
unset ians
until [ $ians ] ; do
    echo -n '(b|c|k|p|y|n) ? : '
    read  ans
    if  [ "$ans" = "y" ] || [ "$ans" = "yes" ] || [ "$ans" = "b" ] ; then
       ians=b
    elif [ "$ans" = "c" ] ; then
       ians=c
    elif [ "$ans" = "k" ] ; then
       ians=k
    elif [ "$ans" = "p" ] ; then
       ians=p
    elif [ "$ans" = "n" ] || [ "$ans" = "no" ]; then
       ians=n
    else
       echo ' What? '$ans' ?  Please answer "b", "c", "k", "p", or "y" or "n"'
    fi
done

if [ "$ians" = b ] ; then
    file=$HOME/.bashrc
    configfile=install/mctdh.profile
    if [ -f $file ] ; then /bin/cp  $file $HOME/.save-bashrc-$$ ; fi
    echo 'Writing to "~/.bashrc".'
    rcfile="~/.bashrc"
    lsh=bash
elif [ "$ians" = c ] ; then
    file=$HOME/.cshrc
    configfile=install/mctdh.profile.csh
    if [ -f $file ] ; then /bin/cp  $file $HOME/.save-cshrc-$$ ; fi
    echo 'Writing to "~/.cshrc".'
    rcfile="~/.cshrc"
    lsh=csh
elif [ "$ians" = k ] ; then
    file=$HOME/.kshrc
    configfile=install/mctdh.profile
    if [ -f $file ] ; then /bin/cp  $file $HOME/.save-profile-$$ ; fi
    echo 'Writing to "~/.kshrc".'
    rcfile="~/.kshrc"
    lsh=ksh
elif [ "$ians" = p ] ; then
    file=$HOME/.profile
    configfile=install/mctdh.profile
    if [ -f $file ] ; then /bin/cp  $file $HOME/.save-profile-$$ ; fi
    echo 'Writing to "~/.profile".'
    rcfile="~/.profile"
    lsh=prf
elif [ "$ians" = n ] ; then
    unset file
    echo 'No writing to any "~/.*" configuration file.'
    lsh=no
fi

if [ -n "$file" ] ; then
    unset ians
    ll=1
    if [ -f $file ] ; then
       grep '^# Following lines written by install' $file > /dev/null
       ll=$?
    fi
    if [ $ll -eq 0 ] ; then
        echo ' '
        echo 'There are already lines written by install_mctdh to your'
        echo '"'$file'" file. They read:'
        echo ' '
        sed -n -e '/^\#\*MCTDH\*A\*/,/^\#\*MCTDH\*B\*/p' $file
        echo ' '
        echo 'Shall I remove them? (if uncertain, choose y)'
        until [ $ians ] ; do
            echo -n '(y|n) ? : '
            read  ans
        if  [ "$ans" = "y" ]||[ "$ans" = "yes" ]; then
            ians=0
        elif [ "$ans" = "n" ]||[ "$ans" = "no" ]; then
            ians=1
        else
            echo ' What? '$ans' ?  Please answer "y" or "n"'
        fi
        done
    fi

    if [ "$ians" = 0 ] ; then
        sed -e '/^\#\*MCTDH\*A\*/,/^\#\*MCTDH\*B\*/d' $file > tmpf$$
        sed -e '/^\#\*MCTDH\*A\*/,/^\#\*MCTDH\*B\*/d' tmpf$$ >| $file
        /bin/rm  tmpf$$
        echo 'Lines removed which were written by a previous install_mctdh.'
        echo ' '
    fi


#------------------------------------------------------------------
#   Write to configuration file
#------------------------------------------------------------------

    echo "#*MCTDH*A***********$star40$star20" >> $file
    echo '# Following lines written by install_mctdh.  '"$(date)" >> $file
    if [ "$lsh" = bash ] ; then
       echo 'export MCTDH_DIR='$MCTDH_DIR >> $file
       echo '. $MCTDH_DIR/'$configfile    >> $file
       echo -n 'if [ -f ~/.mctdhrc ] && [ -t 0 ] ;' >> $file
       echo    ' then . ~/.mctdhrc ; fi'  >> $file
    elif [ "$lsh" = csh ] ; then
       echo 'setenv MCTDH_DIR '$MCTDH_DIR   >> $file
       echo "source $MCTDH_DIR/$configfile" >> $file
    elif [ "$lsh" = ksh ] || [ "$lsh" = prf ]; then
       echo 'MCTDH_DIR='$MCTDH_DIR >> $file
       echo 'export MCTDH_DIR' >> $file
       echo ". $MCTDH_DIR/$configfile" >> $file
       echo 'if [ -f ~/.mctdhrc ] ; then . ~/.mctdhrc ; fi' >> $file
    fi
    echo "#*MCTDH*B***********$star40$star20" >> $file

    echo ' '
    echo "$minu40$minu40"
    echo ' '
    echo 'Here you see the (tail of) your '$file' file:'
    echo ' '
    tail $file
    echo ' '
fi

#-----------------------------------------------------------------------
# Copy install/mctdhrc to $HOME/.mctdhrc
#-----------------------------------------------------------------------
cd $MCTDH_DIR/install
if [ "$lsh" != csh ] ; then
   echo ' '
   echo "$minu40$minu40"
   echo ' '
   echo ' The file mctdhrc may be copied to $HOME/.mctdhrc.'
   echo ' The .bashrc will then source .mctdhrc.'
   echo ' Doing so, the bash-functions cdm and minstall will be available'
   echo ' and a link $HOME/mctdh -> $MCTDH_DIR will be installed.'
   unset ians
   until [ $ians ] ; do
      echo -n ' Shall I copy mctdhrc to $HOME/.mctdhrc (y/n) ? : '
      read ans
      if [ "$ans" = "y" ] || [ "$ans" = "yes" ] ; then
         ians=1
         if [ -f $HOME/.mctdhrc ] ; then
            cp -f $HOME/.mctdhrc  $HOME/.save-mctdhrc-$$
            echo ' Saved old ~/.mctdhrc  to  ~/.save-mctdhrc-'$$
         fi
         cp -f mctdhrc $HOME/.mctdhrc
         echo ' mctdhrc copied to ~/.mctdhrc'
      elif [ "$ans" = "n" ] || [ "$ans" = "no" ] ; then
         echo " mctdhrc is not copied."
         ians=2
      else
         echo ' Please type y or n !'
      fi
done
fi
echo ' '
echo "$minu40$minu40"

if [ -n "$user" ] ; then
   echo ' '
   echo ' *****  END OF NEW USER INSTALLATION  *****'
   echo "$minu40$minu40"
   exit
fi

#-----------------------------------------------------------------------
# select file defining MCTDH_PLATFORM variable
#-----------------------------------------------------------------------
old1=csh ; old2=csh ; old3=csh ; old4=csh ; old5=csh ; unset ending
platforms=`ls platform.cnf.*`
for i in $platforms ; do
  ii=${i##*.}        # Remove everything and including the last "."
  ii=${ii%%*~}       # Remove file~
  ii=${ii%%*_bk*}    # Remove file_bk*
  ii=${ii%%*_md*}    # Remove file_md*
  ii=${ii%%*template*} # Remove *template*
  if [ -z "$ii" ] || [ "$ii" = "$old1" ] || [ "$ii" = "$old2" ] || \
     [ "$ii" = "$old3" ] || [ "$ii" = "$old4" ] || \
     [ "$ii" = "$old5" ] || [ "$ii" = "csh" ]  ; then
     :
  else
     ending="$ending  $ii"
     old5=$old4 ; old4=$old3 ; old3=$old2 ; old2=$old1 ; old1=$ii
     if [ "$ii" != "priv" ] && [ "$ii" != "def" ] && [ "$ii" != "hd" ]
        then unset ort ; fi
  fi
done

if [ -z "$ort" ] ; then            # select platform.cnf by hand

echo ' '
echo ' The MCTDH_PLATFORM variable is used to choose the relevant platform'
echo ' and compiler. It is also used to set the directory names for the'
echo ' executables and object files. Default values are provided in '
echo ' install/platform.cnf.def. Specific values can be set by '
echo ' selecting a different file. (e.g. hd is for Heidelberg people).'
echo ' Choose a file from : '$ending
echo ' If you enter "n" or "no" I will do nothing! '
echo ' Entering "y" or "yes" is equivalent to "def". '
unset ians
until [ $ians ] ; do
   echo -n '( y n '$ending' ) ? : '
   read platfile
   if [ "$platfile" = "y" ] || [ "$platfile" = "yes" ] ; then
        platfile=def
   fi
   if [ "$platfile" = "n" ] || [ "$platfile" = "no" ] ; then
        echo ' platform.cnf not written!'
        ians=1
   elif [ ! -f "platform.cnf.$platfile" ] ; then
       echo " platform.cnf.$platfile does not exist! Try again."\
            'Choose from '$ending' .'
   else
       echo ' '
       platfile1="platform.cnf.$platfile"
       platfile2="platform.cnf.csh.$platfile"
       if [ -f platform.cnf ] ; then
          mv platform.cnf save-pltfrm.cnf-$$
          echo "platform.cnf moved to save-pltfrm-$$"
       fi
       if [ -f platform.cnf.csh ] ; then
          mv platform.cnf.csh save-pltfrm.cnf.csh-$$
          echo "platform.cnf.csh moved to save-pltfrm.cnf.csh-$$"
       fi
       if [ -f $platfile1 ] ;then
          echo "Linking $platfile1 to platform.cnf"
          ln -sf  $platfile1 platform.cnf
       else
          echo "#########################################################"
          echo "####  WARNING: $platfile1 does not exist! ####"
          echo "#########################################################"
       fi
       if [ -f $platfile2 ] ; then
          echo "Linking $platfile2 to platform.cnf.csh"
          ln -sf  $platfile2 platform.cnf.csh
       else
          echo "####  WARNING: $platfile2 does not exist! ####"
          echo "(This is OK, if you do not intend to work under C-Shell)"
       fi
       echo ' '
       ians=1
   fi
done

else      # Automatic selection of priv/def/hd installation

   platfile=$ort
   echo ' '
   platfile1="platform.cnf.$platfile"
   platfile2="platform.cnf.csh.$platfile"
   if [ -f platform.cnf ] ; then
     mv platform.cnf save-pltfrm.cnf-$$
     echo "platform.cnf moved to save-pltfrm-$$"
   fi
   if [ -f platform.cnf.csh ] ; then
     mv platform.cnf.csh save-pltfrm.cnf.csh-$$
     echo "platform.cnf.csh moved to save-pltfrm.cnf.csh-$$"
   fi
   echo "Linking $platfile1 to platform.cnf"
     ln -sf  $platfile1 platform.cnf
   if [ -f $platfile2 ] ; then
      echo "Linking $platfile2 to platform.cnf.csh"
     ln -sf  $platfile2 platform.cnf.csh
   else
      echo "No $platfile2 file."
   fi
   echo ' '
fi                  # End of hand/automatic priv/def/hd flow control.

echo "$minu40$minu40"
echo ' '

#-----------------------------------------------------------------------
# search for compile.cnf files.
#-----------------------------------------------------------------------
unset old1 ; unset old2 ; unset old3 ; unset old4 ; unset ending
bele=1
comps=`ls compile.cnf_*`
for i in $comps ; do
  ii=${i#*_}         # Remove everything and including the first "_"
  ii=${ii%%*~}       # Remove file~
  ii=${ii%%*_bk*}    # Remove file_bk*
  ii=${ii%%*_md*}    # Remove file_md*
  ii=${ii%%*template*} # Remove *template*
  if [ -z "$ii" ] || [ "$ii" = "$old1" ] || [ "$ii" = "$old2" ] || \
     [ "$ii" = "$old3" ] || [ "$ii" = "$old4" ] ; then
     :
  else
     ending="$ending  $ii"
     old4=$old3 ; old3=$old2 ; old2=$old1 ; old1=$ii
     if [ "$ii" != "be" ] && [ "$ii" != "le" ] && [ "$ii" != "benp" ] \
        && [ "$ii" != "lenp" ] ; then unset bele ; fi
  fi
done


echo ' In UNIX there are two variants of how to place the bytes in a word.'
echo ' These variants are called LITTLE ENDIANS (le) and BIG ENDIANS (be).'
echo ' You will be able to read binary files created on different machines'
echo ' as long as they all use the same format, i.e. all are either le or be.'
echo ' If you use gfortran, you should choose the le format.'
echo ' Moreover, shared memory parallelization is possible. However,'
echo ' your compiler or system may not support this (missing libs etc.)'
echo ' In this case one should choose lenp or benp (np = no parallelization).'
echo
echo ' Do you want the le or be format for the compilers? Choose from:'
echo

if [ -z "$bele" ] ; then
  echo ' Hmm, there is an extra compile.cnf_* file!'
  echo ' Choose a compile.cnf file from : '$ending
  echo ' If you enter "n" or "no" I will do nothing! '
  echo ' Entering "y" or "yes" is equivalent to "le". '
  unset ians
  unset compfile
  until [ $ians ] ; do
     echo -n '( y n '$ending' ) ? : '
     read compfile
     if [ "$compfile" = "y" ] || [ "$compfile" = "yes" ] ; then
        ians=0
        compfile=le
     elif [ "$compfile" = "n" ] || [ "$compfile" = "no" ] ; then
        echo ' compile.cnf not written!'
        ians=1
     elif [ ! -f "compile.cnf_$compfile" ] ; then
         echo " compile.cnf_$compfile does not exist! Try again."\
            'Choose from '$ending' .'
         unset compfile
     else
         ians=0
     fi
  done
     if [ "$ians" = "0" ] ; then
         echo ' '
         if [ -f compile.cnf ] ; then
            mv compile.cnf save-compile.cnf-$$
            echo "compile.cnf moved to save-compile-$$"
         fi
         ln -sf compile.cnf_$compfile compile.cnf
         echo "compile.cnf_$compfile  linked to  compile.cnf"
     fi
else
  echo ' "y" or "le" if you want the LITTLE ENDIANS format and parallelization.'
  echo ' "n" or "be" if you want the BIG ENDIANS format and parallelization.'
  echo ' "lenp" if you want the LITTLE ENDIANS format without parallelization.'
  echo ' "benp" if you want the BIG ENDIANS format without parallelization.'
  echo ' (If you do not know what to do, choose "y".)'
  unset ians
  until [ $ians ] ; do
    echo -n '(y|n|le|be|lenp|benp) ? : '
    read  ans
    if [ -f compile.cnf ] ; then
       mv compile.cnf save-compile.cnf-$$
       echo "compile.cnf allready exist! Saved to save-compile-$$"
    fi
    if  [ "$ans" = "y" ] || [ "$ans" = "yes" ] || [ "$ans" = "le" ]; then
       ians=0
       ln -sf compile.cnf_le compile.cnf
       echo "compile.cnf_le  linked to  compile.cnf"
       echo ' '
       echo ' LITTLE ENDIANS format is used (if possible).'
       echo ' The usage of parallelization features is enabled.'
    elif [ "$ans" = "n" ] || [ "$ans" = "no" ] || [ "$ans" = "be" ]; then
       ians=1
       ln -sf compile.cnf_be compile.cnf
       echo "compile.cnf_be  linked to  compile.cnf"
       echo ' '
       echo ' BIG ENDIANS format is used (if possible).'
       echo ' The usage of parallelization features is enabled.'
    elif [ "$ans" = "lenp" ]; then
       ians=1
       ln -sf compile.cnf_lenp compile.cnf
       echo "compile.cnf_lenp  linked to  compile.cnf"
       echo ' '
       echo ' LITTLE ENDIANS format is used (if possible).'
       echo ' The usage of parallelization features is disabled.'
    elif [ "$ans" = "benp" ]; then
       ians=1
       ln -sf compile.cnf_benp compile.cnf
       echo "compile.cnf_benp  linked to  compile.cnf"
       echo ' '
       echo ' BIG ENDIANS format is used (if possible).'
       echo ' The usage of parallelization features is disabled.'
    else
       echo " What? $ans ?"
       echo ' Please answer "y" or "le" or "be" or "lenp" or "benp"'
    fi
  done
fi

#--------------------------------------------------------------------
# set path names in include files
# $MCTDH_DIR/source/include/paths.inc is generated.
#--------------------------------------------------------------------
incpath="$MCTDH_DIR/source/include"

echo ' '
if [ -f $incpath/paths.inc ] ; then
    ians=1
    echo "The file $incpath/paths.inc already exists!"
    grep $MCTDH_DIR $incpath/paths.inc > /dev/null
    if [ ! "$?" = 0 ] ; then ians=0 ; fi
    lines=`wc -l $incpath/paths.inc | cut -c 1-3`
    if [ $lines -lt 22 ]; then ians=0 ; fi # re-install paths.inc if too small
else
    ians=0
fi

mctdh1=`echo $MCTDH_DIR | cut -c 1-62`
mctdh2=`echo $MCTDH_DIR | cut -c 63-122`
mctdh3=`echo $MCTDH_DIR | cut -c 123- `

# Forran 77/90 compatibility: padding with spaces
# and putting ampersand in column 73

mctdh1=$(printf '%-66s&' "'$mctdh1'")
if [ -n "$mctdh2" ] ; then
    mctdh2=$(printf '%-64s&' "'$mctdh2'")
fi
opdatadef='      parameter   (opdatadef='
opdatadef=$(printf '%-73s&' "$opdatadef")


if [ "$ians" = 0 ] || [ "$force" = 1 ] ; then
   /bin/rm -f $incpath/paths.inc >& /dev/null
   echo "!    -*- fortran -*-"                         > $incpath/paths.inc
   sed -n -e '/^!##A1##/,/(oppathdef=[ ]*&$/p' $bname >> $incpath/paths.inc
   echo "     & ${mctdh1}"                            >> $incpath/paths.inc
   if [ -n "$mctdh2" ] ; then
       echo "     & //${mctdh2}"                      >> $incpath/paths.inc
   fi
   echo "     & //'$mctdh3/operators')"               >> $incpath/paths.inc

   echo "$opdatadef"                                  >> $incpath/paths.inc
   echo "     & ${mctdh1}"                            >> $incpath/paths.inc
   if [ -n "$mctdh2" ] ; then
       echo "     & //${mctdh2}"                      >> $incpath/paths.inc
   fi
   echo "     & //'$mctdh3/source/surfaces')"         >> $incpath/paths.inc
   echo ' '
   echo 'Installed : '$incpath/paths.inc
fi


#-----------------------------------------------------------------------
# Edit the abspath routines (einglib.f) to handle "ignore".
#-----------------------------------------------------------------------
grep "      ignore= '$ignore '"  \
    $MCTDH_DIR/source/lib/utilities/einglib.f > /dev/null
found="$?"
grep "      substi= '$substi '"  \
    $MCTDH_DIR/source/lib/utilities/einglib.f > /dev/null

if [ "$?" != 0 ] || [ "$found" != 0 ] || [ "$force" = 1 ] ; then
# Write an sed script-file.
    echo '/^C.....Initialize (=SED=)/{' > sedfile
    echo 'n'                           >> sedfile
    echo 'N'                           >> sedfile
    echo 'c\'                          >> sedfile
    echo "\      ignore= '$ignore '"\\ >> sedfile
    echo "\      substi= '$substi '"   >> sedfile
    echo '}'                           >> sedfile

   sed -f sedfile $MCTDH_DIR/source/lib/utilities/einglib.f > eingl.f
   cp -f eingl.f $MCTDH_DIR/source/lib/utilities/einglib.f
   echo 'Installed "ignore" : '$MCTDH_DIR/source/lib/utilities/einglib.f
else
   echo '"ignore" already exist in '$MCTDH_DIR/source/lib/utilities/einglib.f
fi
rm -f eingl.f sedfile



#-----------------------------------------------------------------------
# compile documentation
#-----------------------------------------------------------------------
echo ' '
echo "$minu40$minu40"
echo ' '
echo 'Some of the documentation is stored as Latex files. These need '
echo 'to be compiled before they can be read.'
echo 'Would you like to compile the Latex documentation now ?'
echo ' '
echo 'Choose: y) yes '
echo 'Or    : n) no '
unset ians
until [ $ians ] ; do
    echo -n '(y|n) ? : '
    read  ans
    if  [ "$ans" = "y" ] || [ "$ans" = "yes" ]; then
       ians=0
    elif [ "$ans" = "n" ] || [ "$ans" = "no" ]; then
       ians=1
    fi
done

if [ "$ians" = 0 ] ; then
   ( xterm -geometry 80x15-0+0 -e $MCTDH_DIR/install/dcompile
     cd $MCTDH_DIR/doc ; chmod -R a+r *  ) &
fi

#-----------------------------------------------------------------------
# GNU or INTEL compiler for Linux
#-----------------------------------------------------------------------
tp=`type ifort 2>/dev/null`
if [ `uname -s` = Linux ] && [ -n "$tp" ]; then
   echo ' '
   echo "$minu40$minu40"
   echo ' '
   echo 'There exist a Intel IFORT compiler on your Linux system.'
   echo 'You may choose between GNU compiler or Intel IFORT compiler.'
   echo 'Type "y" or "GNU" for GNU compilers, or "IF" for IFORT compilers.'
   pltfm=`uname -m`
   unset ians
   until [ $ians ]; do
       echo -n '(y|GNU|IF) ? : '
       read  ans
       if  [ "$ans" = "y" ] || [ "$ans" = "yes" ] || [ "$ans" = "GNU"  ]; then
          ians=0
          rm -f  $MCTDH_DIR/install/MCTDH_IFORT_$pltfm
       elif [ "$ans" = "IF" ] || [ "$ans" = "if" ]; then
          ians=1
          touch $MCTDH_DIR/install/MCTDH_IFORT_$pltfm
       else
          echo ' What? '$ans' ?  Please answer "y" or "GNU" or "IF" '
       fi
   done
else
   rm -f  $MCTDH_DIR/install/MCTDH_IFORT_$pltfm
fi

#-----------------------------------------------------------------------
# compile programs
#-----------------------------------------------------------------------
echo ' '
echo "$minu40$minu40"
echo ' '
echo 'The parameters chosen for the compilation of the programs are :'
echo ' '
$MCTDH_DIR/bin/compile config

echo ' '
echo 'Would you like to compile the programs now ?'
echo ' '
echo 'Choose: y) yes'
echo 'Or    : n) no'
unset ians
until [ $ians ] ; do
    echo -n '(y|n) ? : '
    read  ans
    if  [ "$ans" = "y" ] || [ "$ans" = "yes" ]; then
       ians=0
    elif [ "$ans" = "n" ] || [ "$ans" = "no" ]; then
       ians=1
    else
       echo ' What? '$ans' ?  Please answer "y" or "n"'
    fi
done

if [ "$ians" = 0 ] ; then
   echo ' '
   echo 'Compilation of all the MCTDH source code.'
   echo 'This may take a while (3 .... 30 min, depending on your hardware).'
   $MCTDH_DIR/bin/compile -a -Q all
   if [ "$?" = 1 ] ; then
     echo '##########'
     echo '##########  The compilation (partially) failed!'
     echo '##########  I try again! This time without suppressing the output.'
     echo '##########'
     $MCTDH_DIR/bin/compile  all
   fi
fi

#-----------------------------------------------------------------------
# finish installation
#-----------------------------------------------------------------------
echo "$minu40$minu40"
echo ' '
if [ -n "$file" ] ; then
   echo 'You should now execute '$file' i.e.: '
   if [ "$lsh" = ksh ] || [ "$lsh" = prf ] ; then
      echo ' . '$file
   elif [ "$lsh" = bash ] || [ "$lsh" = csh ] ; then
      echo ' source '$file
   fi
   echo 'in order to make the changes to your configuration file effective.'
fi

if [ "$lsh" = bash ] || [ "$lsh" = ksh ]; then
  echo
  echo "Make sure that your $rcfile is executed on login."
  echo "You may need to add a line like"
  echo " if [ -f $rcfile ] ; then . $rcfile ; fi "
  echo 'to your ~/.profile file.'
fi

dat=$(echo " $(date)              " | cut -c 1-36)
echo ' '
echo 'The command "menv" ("'$MCTDH_DIR'/bin/menv")'
echo 'shows your current MCTDH environment variables and your PATH.'
echo ' '
echo "Add the line  file:${HOME}/mctdh/doc/index.html "
echo 'as bookmark to your browser. This gives you easy access to the '\
     'documentation.'
echo '(works only if ~/.mctdhrc is installed).'
echo ' '
echo "$star40$star40"
echo "$star20  Finished Installation (install_mctdh) $star20"
echo "$star20  $dat  $star20"
echo "$star40$star40"
echo ' '

exit


### ------  DO NOT REMOVE THE FOLLOWING LINES ! ----- ###

!##A1###################################################################
!
! PATHS.INC
!
! include file containing paths needed in program
!
! paths.inc is created by the install_mctdh script.
! It supplies the path to the operator directory (i.e. ~.op files)
! and to the source/opfuncs directory to the mctdh program.
!
!#######################################################################
!-----------------------------------------------------------------------
! opdefpath: default path for operator files (~.op)
! opdatadef: path to operator library, needed for data files
!            (e.g. in nocl1sch.F)
!-----------------------------------------------------------------------

      character*200  oppathdef, opdatadef

      parameter   (oppathdef=                                            &

### ------  DO NOT REMOVE THE ABOVE LINES ! ----- ###


