#!/bin/bash
####################################################################
#                  MFIND                                           #
# This shell script lists all files of the mctdh-directory that    #
# are newer than one day.                                          #
# HDM 04/00                                                        #
####################################################################

#-----------------------------------------------------------------------
#  Definitions :
#-----------------------------------------------------------------------

time='-mtime -1'
swch=0
unset cdir
unset  dir
present=`pwd`
minus40='----------------------------------------'

purp="Purpose: Find recently modified files of the MCTDH directory."
usage="Usage: mfind [ -h ] [ -d | -m | -n | -g | -p ] [ -f -c -t -l ] [dir]"
help1='-h : print this help text.'
help2='-d days : Set the argument of -mtime to "days".'
help3='-m min  : Set the argument of -mmin to "min".'
hlp3a='-n file : Set the argument of -newer to "file".'
hlp3b='-g : Set timing to -newer $MCTDH_DIR/doc/guide/guide.ps'
hlp3c='-p : Set timing to -newer $MCTDH_DIR/mctdhall.prj'
hlp3d='-P : Set timing to -newer $MCTDH_DIR/ProjectHeader'
help4='Use either -n -m -g -p or -d option. Default is -d -1 (i.e. 1 day old).'
help5='-i pat  : Search for files whose name matches egrep-pattern "pat".'
hlp5a='-e pat  : Exclude files whose name matches egrep-pattern "pat".'
help6='-c cdir : Copy the files found to directory cdir. cdir should not exist.'
hlp6a='-t cdir : Like -c, but also tar and gzip the directory cdir.'
help7='-l : Print a "ls -ltr" list, sorted according to modification date.'

#-----------------------------------------------------------------------
#  Read the options (if any).
#-----------------------------------------------------------------------

while getopts ":hi:e:m:n:c:t:d:Ppgl" opt; do
    case $opt in
    h  ) echo $minus40$minus40
         echo $purp
         echo $usage
         echo ' '$help1
	 echo ' '$help2
	 echo ' '$help3
	 echo ' '$hlp3a
	 echo ' '$hlp3b
	 echo ' '$hlp3c
	 echo '      '$help4
	 echo ' '$help5
	 echo ' '$hlp5a
	 echo ' '$help6
	 echo ' '$hlp6a
	 echo ' '$help7
	 echo ' '
         echo 'If "dir" is given, search only in dir and its subdirectories.'
         echo 'If  no "dir" is given, $MCTDH_DIR is assumed.'
         echo ' '
	 echo 'Example:'
	 echo '"mfind -m -90 -t ~/changes" will copy'\
	       'files not older than 90 minutes to the'
         echo 'directory ~/changes. It also'\
	       'will produce the tar-file /home/changes.tgz.'
	 echo 'Note: The directory /home/changes does not need to exist.'
	 echo ' '
	 exit ;;
    i  ) pat=$OPTARG ;;
    e  ) epat=$OPTARG ;;
    d  ) time='-mtime '$OPTARG ;;
    m  ) time='-mmin  '$OPTARG ;;
    n  ) time='-newer '$OPTARG ;;
    c  ) cdir=$OPTARG ;;
    t  ) cdir=$OPTARG
         tar=1 ;;
    g  ) time="-newer $MCTDH_DIR/doc/guide/guide.ps" ;;
    p  ) time="-newer $MCTDH_DIR/mctdhall.prj" ;;
    P  ) time="-newer $MCTDH_DIR/ProjectHeader" ;;
    l  ) swch=1 ;;
    \? ) echo ' Unkown option!'
         echo ' -h provides a help text.'
	 echo $usage
	 exit 1 ;;
  esac
done
shift $(($OPTIND - 1))
if [ -n "$1" ] ; then
   cdtest=`cd "$1" 2>&1`
   if [ -n "$cdtest" ] ; then
      cd "$MCTDH_DIR"
      cdtest=`cd "$1" 2>&1`
      if [ -n "$cdtest" ] ; then
         cd source
         cdtest=`cd "$1" 2>&1`
         if [ -z "$cdtest" ] ; then cd "$1" ; fi
      else
         cd "$1"
      fi
   else
      cd "$1"
   fi
else
   cd "$MCTDH_DIR"
fi
dir=`pwd`

if [ -n "$cdtest" ] ; then
   echo "Directory $1 was not found!"
   exit
fi

echo $minus40$minus40
echo 'Searching through directory: '$dir
echo 'for files with modification time: '$time' .'
if [ -n "$pat" ] ; then
   echo "Include files with pattern $pat"
fi
if [ -n "$epat" ] ; then
   echo "Exnclude files with pattern $epat"
fi
echo ' '

if [ -n "$cdir" ] ; then
    cd $present
    if [ ! -d $cdir ] ; then mkdir -p $cdir ; fi
    cd $cdir
    cdir=`pwd`
fi

cd $dir
dir1=${dir##$MCTDH_DIR}
if [ "$dir" != "$dir1" ] ; then
   cd "$MCTDH_DIR"
   dir1='.'$dir1
else
   dir1='./'
fi

list=`find $dir1 $time -type f  -name '*[!~]' -print | egrep -v  \
'(MCTDH_client|\.svn|\./object/.*|\./run.*/.*|\./test.*/.*|\./tutorial/.*|\./bin/[^p].+/.*|.*/paths\.inc|.*\.ps|.*\.dvi|.*\.aux|.*/i686/.*|.*/alpha/.*|\./source/tags|\./source/TAGS|\./source/TYPE_DAT.*|\./source/SFILES|\./source/SUBR|.*\.tgz|\./install/compile.cnf$|\./install/platform.cnf$|\./install/platform\.cnf\.csh$|\./doc/.*\.log|\./doc/guide/guide\.ps|\./doc/guide/guide\.pdf|\./doc/guide/guide\.i.*|\./doc/guide/guide\.l.*|\./doc/guide/guide\.b.*|\./doc/guide/guide\.toc|\./CompDir*|\./.*mctdhall.*|.*\.[0-9][0-9]?$|.*/ProjectHeader|.*_bk[0-9][0-9]?$|.*_md[0-9][0-9]?$|.*/obsolete/.*|.*/Makefile_.*|\./source\..*/.*|\./source_.*/.*)' | sort `

if [ -n "$pat" ] ; then
   list=`echo "$list" | egrep "$pat"`
fi
if [ -n "$epat" ] ; then
   list=`echo "$list" | egrep -v "$epat"`
fi
echo "$list"
echo ' '
number=`echo "$list" | wc -l`
echo ' '$number' files found.'


if [ -n "$cdir" ] ; then
   echo ' '
   echo 'Copying the files found to: '$cdir'.'
   for i in $list
     do
	j=${i#./}
	dirj=${j%/*}
	if [ $dirj = $j ] ; then unset dirj ; fi
	file=${j#$dirj/}
	dirj=$cdir/$dirj
	if [ ! -d $dirj ] ; then mkdir -p $dirj ; fi
	cp $i $dirj/$file
     done
     if [ -n "$tar" ] ; then
	cd "$cdir"
	name=$(basename $cdir)
	cd ..
	dname=$(pwd)
	echo 'Creating tar-file: '$dname/$name.tgz
	tar -czf $name.tgz $name
    fi
fi

if [ "$swch" = 1 ] && [ $number -gt 0 ] ; then
  echo ' '
  ls -ltr $list | cut -c 1-11,32-
fi

echo $minus40$minus40
exit

















