#!/bin/sh
########################################################################
#
#  $MCTDH_DIR/install/bashpath
#
#  You must run this shell script, if your bash is not found under
#  /bin/bash
#  In this case goto $MCTDH_DIR/install and type: ./bashpath
#  The new path is to be supplied as an argument to bashpath.
#  E.g.:  bashpath /usr/local/bin/bash
########################################################################

minus40='----------------------------------------'

vz=`echo "$1" | cut -c 1`

if [ "$1" = "-h" ] ||  [ "$2" = "-h" ] ; then
    echo $minus40$minus40
    echo 'bashpath changes the #!PATH command of all shell-scripts.'
    echo 'You must run this shell script, if your bash is not found under'\
         '/bin/bash'
    echo 'The new path is to be supplied as argument to bashpath.'
    echo "E.g.: bashpath /usr/local/bin/bash"
    echo 'Options:'
    echo '   -h  : Print this help text.'
    echo '   -n  : No-action. Do not change the files, but list'\
         'actions taken otherwise.'
    echo $minus40$minus40
    exit
elif [ "$1" = "-n" ] ; then
    noaction=1
    shift 1
    echo "Option -n set. No action is taken, files left unchanged."
elif [ "$vz" = - ] ; then
    echo "Unknown option: $1 . Try -h"	
    exit
fi

newpath="$1"
echo $minus40$minus40
echo "This is bashpath!    newpath = $newpath"
echo $minus40$minus40

sl=`echo "$1" | cut -c 1`
if [ "$sl" != "/" ] ; then
      echo "INPUT ERROR!"
      echo "The argument newpath = $newpath"
      echo "must be an absolute path!"
      echo "Try option -h"
      exit
fi
baseb=`basename "$newpath"`
if [ "$baseb" != "bash" ] ; then
      echo "INPUT ERROR!"
      echo "The argument newpath = $newpath"
      echo 'must end with the string "bash"!'
      echo "Try option -h"
      exit
fi


$newpath --version
if [  $? != 0 ] && [ -z "$noaction" ] ; then
   echo "There is no bash-shell under newpath = $newpath"
   echo -n "Continue anyway? (y/n) : "
   read answ
   if [ ! "$answ" = y ] ; then exit ; fi
fi

nnewp='#!'$newpath
bashp='^#!.*/bash'
cmd="s?${bashp}?${nnewp}?"
count=0

PWD=`pwd`
bpwd=`basename "$PWD"`
echo $minus40$minus40
echo "working directory: $PWD"
if [ $bpwd != "install" ] ; then
     echo 'You must start bashpath while being in $MCTDH_DIR/install'
     exit
fi

for i in `ls` ; do
    j=0
    unset oldpath
    oldpath=`egrep "$bashp" $i 2> /dev/null `
    if [ -n "$oldpath" ] && [ "$oldpath" != "$nnewp" ] && \
                          [ "$i" != bashpath ] ; then
        scrpt=`echo "changing: $i                     " | cut -c 1-30`
        echo "$scrpt $oldpath  ->  $nnewp"
        if [ -z "$noaction" ] ; then
           sed "$cmd" $i > buffer_bashpath
           cat buffer_bashpath > $i
        fi
        count=`echo "$count + 1" | bc`
    fi
done
rm -f buffer_bashpath

echo $minus40$minus40

cd ../bin
echo 'working directory: '`pwd`

for i in `ls` ; do
    j=0
    unset oldpath
    oldpath=`egrep "$bashp" $i 2> /dev/null `
    if [ -n "$oldpath" ] && [ "$oldpath" != "$nnewp" ] && \
                          [ "$i" != bashpath ] ; then
        scrpt=`echo "changing: $i                     " | cut -c 1-30`
        echo "$scrpt $oldpath  ->  $nnewp"
        if [ -z "$noaction" ] ; then
           sed "$cmd" $i > buffer_bashpath
           cat buffer_bashpath > $i
        fi
        count=`echo "$count + 1" | bc`
    fi
done
rm -f buffer_bashpath
echo $minus40$minus40
echo "Done bashpath.  newpath = $newpath"
if [ -z "$noaction" ] ; then
   echo "$count files updated."
else
   echo "$count files found."
   echo "No action taken, files are left unchanged."
fi
echo $minus40$minus40
exit


