#!/bin/bash
#
# This script creates links to /home/mctdh/addsurf.
# To change this default path, edit line 10-25 of this script.
#
# HDM 09/02
#-----------------------------------------------------------------------

minus40='----------------------------------------'
echo "$minus40$minus40"

# --- Find the path of the addsurf directory
if [ -n "$MCTDH_ADDSURF" ] && [ -d $MCTDH_ADDSURF ]; then
   addsrf=$MCTDH_ADDSURF                   # Set in .mctdhrc
elif [ -d /home/mctdh/addsurf ] && [ -d /home/dieter ] ; then
   addsrf=/home/mctdh/addsurf        # Heidelberg default location
else
   addsrf=${MCTDH_DIR%/*}/addsurf    # Default.
fi

# --- If addsurf does not exist try addsurf3a
if [ -z "$addsrf" ] || [ ! -d $addsrf ]; then
   if [ -n "$MCTDH_ADDSURF" ] && [ -d ${MCTDH_ADDSURF}3a ]; then
      addsrf=${MCTDH_ADDSURF}3a                 # Set in .mctdhrc
   else
      addsrf=${MCTDH_DIR%/*}/addsurf3a    # Default.
   fi
fi

echo "The path of the addsurf directory is: $addsrf"

if [ ! -d $addsrf ]; then
   echo "Could not determine the correct path of the addsruf directory."
   echo "Edit your .mctdhrc (line 20). Exit."
   echo "$minus40$minus40"
   exit
fi

#-----------------------------------------------------------------------
# FUNCTION  outf. Print content of 'surfaces'.
#-----------------------------------------------------------------------
outf() {
      echo "These are the links in "`pwd`' :'
      echo ' '
      ls -F | tr ' ' '\n' | fgrep '@' | tr '@' ' ' |\
      tr '\n' ' ' | awk \
      '{printf("%13s%13s%13s%13s%13s%13s\n",$1,$2,$3,$4,$5,$6)}
       {printf("%13s%13s%13s%13s%13s%13s\n",$7,$8,$9,$10,$11,$12)}
       {printf("%13s%13s%13s%13s%13s%13s\n",$13,$14,$15,$16,$17,$18)}
       {printf("%13s%13s%13s%13s%13s%13s\n",$19,$20,$21,$22,$23,$24)}'
      echo "$minus40$minus40"
      echo ' '
      echo "These are the files in "`pwd`' :'
      echo ' '
      ls -F | tr ' ' '\n' | fgrep -v '@' | fgrep -v README \
            | fgrep -v obsolete | fgrep -v DEF_ | fgrep -v DUMMY \
            | tr '\n' ' ' | awk \
      '{printf("%13s%13s%13s%13s%13s%13s\n",$1,$2,$3,$4,$5,$6)}
       {printf("%13s%13s%13s%13s%13s%13s\n",$7,$8,$9,$10,$11,$12)}
       {printf("%13s%13s%13s%13s%13s%13s\n",$13,$14,$15,$16,$17,$18)}
       {printf("%13s%13s%13s%13s%13s%13s\n",$19,$20,$21,$22,$23,$24)}'
      echo "$minus40$minus40"
}

#-----------------------------------------------------------------------
#  Read the options (if any).
#-----------------------------------------------------------------------
while getopts ":hacsf:" opt; do
   case $opt in
   h ) help=1 ;;
   f ) add=$OPTARG ;;
   s ) show=1 ;;
   a ) ask=1 ;;
   c ) clear=1 ;;
   \?) echo 'Unkown option!'
       echo 'Usage: mklinks [ -h -a -c -f path ]'
       exit ;;
   esac
done
shift $(($OPTIND - 1))


#-----------------------------------------------------------------------
#  Write help text.
#-----------------------------------------------------------------------
if [ "$help" = "1" ] ; then
   echo 'Purpose: Create links from $MCTDH_DIR/source/surfaces/'\
        ' to addsurf'
   echo 'Usage: mklinks [ -h -a -c -s -f path ] [surf-files]'
   echo '   -h      :  Print this help text.'
   echo '   -f path :  Use "path" as addsurf directory.'
   echo "   -a      :  Ask before installing a link."
   echo "   -c      :  Clear $MCTDH_DIR/source/surfaces/ from all links."
   echo "   -s      :  Show content of $MCTDH_DIR/source/surfaces/ ."
   echo "The addsurf-path is: $addsrf"
   echo ' '
   echo 'Examples:'
   echo ' "mklinks -a" will make all links, but ask for permition for each file.'
   echo ' "mklinks no hcn"  will make links to:  hcn.F hcn_data'\
           'nocl1_data nocl1sch.F'
   echo "$minus40$minus40"
   exit
fi

if [ ! -d $addsrf ] ; then
   echo "The directory $addsrf does not exist!"
   echo "$minus40$minus40"
   exit
fi

#-----------------------------------------------------------------------
#  Show content of 'surfaces'. (Option -s).
#-----------------------------------------------------------------------
if [ "$show" = "1" ] ; then
    cd $MCTDH_DIR/source/surfaces
    outf
    exit
fi

#-----------------------------------------------------------------------
#  Generate surfs, the list of all (relevant) files on addsurf.
#-----------------------------------------------------------------------
cd $addsrf
for i in $@ ; do arglist="$arglist"${i}'*  ' ; done

surfs=`ls $arglist | fgrep -v addsurf. | fgrep -v lsth.f | fgrep -v _bk \
 | egrep -v '.*~$' | fgrep -v _md | fgrep -v README | fgrep -v obsolete `
cd $MCTDH_DIR/source/surfaces
echo ' '
echo "Current directory: "`pwd`
echo ' '

#-----------------------------------------------------------------------
#  Remove links, if -c option (clear) is set.
#-----------------------------------------------------------------------
if [ "$clear" = "1" ] ; then
    ssurfs=`ls -F | fgrep @ | tr '@' ' ' `
    echo $ssurfs
    for surf in $ssurfs ; do
    if [ -L $surf ] ; then rm -f $surf ; fi
    done
    echo ' '
    echo "All links (listed above) removed from "`pwd`"/ !"
    echo ' '
    outf
    exit
fi

if [ -z "$ask" ] ; then
   for surf in $surfs ; do
       echo "ln -s $addsrf/$surf $surf"
   done

   echo ' '
   echo 'Shall I create these links? (y/n).'
   read dummy
   if [ ! "$dummy" = "y" ] ; then
      echo 'No links installed.'
      outf
      exit
   fi
fi

#-----------------------------------------------------------------------
#  Create links.
#-----------------------------------------------------------------------
for surf in $surfs ; do
     if [ -f $surf ] && [ ! -L $surf ] ; then
        echo 'The file exist'
        ls -l $surf | cut -c 43-
        echo "No link created for $surf"
     elif [ -L $surf ] ; then
        echo 'The link exist'
        ls -l $surf | cut -c 43-
        echo 'Shall I overwrite? (y/n)'
        read dummy
        if [ ! "$dummy" = "y" ] ; then
           rm -f $surf
           ln -s $addsrf/$surf $surf
        fi
     else
        if [ "$ask" = "1" ] ; then
	   echo "Shall I make : ln -s $addsrf/$surf $surf ? (y/n)"
           read dummy
           if [ "$dummy" = "y" ] ; then
              ln -s $addsrf/$surf $surf
	      echo "Link created!"
	   else
	      echo "Link NOT created."
           fi
	else
           ln -s $addsrf/$surf $surf
	fi
     fi
done

echo ' '
outf
echo "Done! Good bye!"

exit









