#!/bin/bash
####################################################################
#                  mvobsolete                                      #
# This shell script moves the files *_bk?? and *_md?? to the       #
# directory obsolete. The script runs over the whole MCTDH tree    #
# HDM 02/02                                                        #
####################################################################

#-----------------------------------------------------------------------
# FUNCTION findf. Finds all the file of the current directory, which
#                 should be moved. Move these files to obsolete.
#-----------------------------------------------------------------------
findf() {
     pwdf=`pwd`
     dirf=`basename $pwdf`
     unset mvlist
     mvlist=`ls -F *_bk[0-9][0-9] *_mbk[0-9][0-9] *_md[0-9][0-9] *~ 2>/dev/null`
     if [ $dirf = install ]; then
        mvlist=$mvlist' '`ls -F save-* 2>/dev/null`
     fi
     if [ "$mvlist" = " " ]; then unset mvlist ; fi
     if [ -z "$dry" ]; then
        if [ -n "$mvlist" ] ; then
           if [ ! -d obsolete ] ; then mkdir obsolete ; fi
           for j in $mvlist ; do
              if [ -n "${j##*/*}" ] ; then  # ignore directories
                 mv $j obsolete/$j
                 echo "moved: $pwdf/$j"
              fi
           done
        fi
     else
        if [ -n "$mvlist" ] ; then
           echo
           echo "$pwdf"
           echo "$mvlist" | sed -e '/^$/d'
        fi
     fi
}

### --------------- Actual shell script starts here -----------------------###

if [ "$1" = "-h" ] || [ "$2" = "-h" ] ; then
  echo "Purpose: Cleaning the MCTDH-directory from *_bk*, *_md* and *~ files."
  echo "         These files are moved to the directory obsolete."
  echo "Usage  : mvobsolete [ -h -c -d ] [dir] ."
  echo "         If dir is not given, the script runs over all the "\
                 "MCTDH tree."
  echo "         Otherwise, only files of the directory dir are moved."
  echo '         dir is interpreted relative to $MCTDH_DIR='$MCTDH_DIR
  echo 'Options:'
  echo ' -h    : Print this help text.'
  echo ' -c    : Remove all "obsolete" directories.'
  echo ' -d    : Dry run. Just show what mvobsolete would do.'
  echo ' NB: do not use more than one option!  '
  echo ' '
  echo 'Example: mvobsolete source/genoper '
  echo ' '
  exit
fi

if [ "$1" = "-d" ]; then
   export dry=1
   shift
   echo " ---  Dry Run ---"
fi
if [ "$1" = "-c" ]; then
   cd $MCTDH_DIR
   if [ -n "$2" ] ; then cd $2 ; fi
   echo 'Removing the "obsolete" directories from '`pwd`':'
   find . -name obsolete -type d
   echo -n 'Shall I delete them?  (y|n) '
   read answ
   if  [ "$answ" = "y" ]; then
      find . -name obsolete -type d -exec rm -rf {} \; 2>/dev/null
      echo 'Directories "obsolete" removed.'
   else
      echo 'Nothing removed.'
   fi
   exit
fi

if [ -n "$1" ] ; then
  cd $MCTDH_DIR/$1 && findf
  exit
fi


# --- Create list of directories
cd $MCTDH_DIR
dirlist=`find . -type d | egrep -v '(/obsolete|\./bin/[^p].+|\./object)' | sort `

for i in $dirlist ; do
    cd $i && findf
    cd $MCTDH_DIR
done

exit

