#!/bin/bash
####################################################################
#                  LB                                              #
# This shell script lists the content of the backup directory.     #
# HDM 02/04                                                        #
####################################################################

minus40='----------------------------------------'
echo "$minus40$minus40"
mctdh=`basename $MCTDH_DIR`

while getopts ":ham" opt; do
    case $opt in
    h  ) echo "Purpose: List the content of the MCTDH Backup directory."
         echo "Usage: lb [ -h | -a | -m ]  [dir] ."
         echo 'If dir is not given, $MCTDH_DIR is assumed.'
         echo 'Only those files, which belong to the backup of dir are listed.'
         echo '-h : print this help text.'
         echo '-a : all files will be listed (dir is ignored).'
         echo '-m : list the memory consumption.'
         echo "$minus40$minus40"
         exit ;;
    a  ) all=1 ;;
    m  ) pm=1  ;;
    \? ) echo ' Unkown option!'
         echo $usage
         echo ' -h provides a help text.'
         exit 1 ;;
  esac
done
shift $(($OPTIND - 1))

if [ -n "$1"   ]; then mctdh=$1    ; fi
if [ -n "$all" ]; then unset mctdh ; fi

if [ -z "$MCTDH_BACKUP" ]; then
   echo "Environment variable MCTDH_BACKUP is unset!"
   exit 1
elif [ ! -d $MCTDH_BACKUP ]; then
   echo "The directory $MCTDH_BACKUP does not exist!"
   exit 1
fi

echo "MCTDH BACKUP DIRECTORY is: $MCTDH_BACKUP"
cd $MCTDH_BACKUP
ls -ldhtrF ${mctdh}* | cut -c 1-14,37-
if [ -n "$pm" ]; then
   mem=`du -cs ${mctdh}* | tail -1 | cut -f1`
   mmb=$(($mem/1024)) ; mmk=$(($mem%1024))
   echo  "Memory consumption: ${mmb}M ${mmk}K"
fi
echo "MCTDH BACKUP DIRECTORY is: $MCTDH_BACKUP"
echo "$minus40$minus40"
exit

