#!/bin/bash

# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************

#
if [ $# = 0 ]
then
    cat << EOT
______________________________________________________________
                                                 2000-10-18/vk
  Usage:
          $0 show
          $0 clean

  This script searches for orphaned dot files in all Metview
  subdirectories, i.e. for hidden files where the corresponding
  visible Metview file does not exist any more.

  If called with parameter 'show' (or with anything else
  than 'clean') then just shows you all such files that are
  considered to be orphaned dot files.

  If called with parameter 'clean' then, for each file, asks
  whether to move it into Metview Wastebasket, or to keep it.

  Script does its best not to clean dotted configuration files.

  Warning: this script has been tested only on SGI IRIX 6.5!

  Recommendation:
     1) Always run '$0 show' first, and check the output!
     2) Do not run '$0 clean' while Metview is running!

  Prerequisite:
     $HOME/metview/Wastebasket has to exist!
______________________________________________________________
EOT

    exit
fi

MV=$HOME/metview

#-- show or clean? --
clean=0
[ x$1 = xclean ] && clean=1
total=0
found=0

#-- Wastebasket available? --
W=$HOME/metview/Wastebasket
if [ $clean = 1 -a ! -d $W ]
then
    echo " >>> Wastebasket file $W not found!"
    exit
fi

#-- give user feedback (1) --
echo
echo " Browsing through your Metview directory $MV..."
echo

#-- create a file with names of all dot files; ignore known dotted config files:
#--  .MainWindowResources
#--  ..* (double dotted files)
#--  .Temporary (this is for double safety, visible file is a broken link)
TMP=$$.tmp
find $MV -name '.*' -exec ls {} \; | grep -v .Temporary | grep -v .MainWindowResources | grep -v '\.\.' | grep -v Wastebasket > $TMP

#-- give user feedback (2) --
if [ $clean = 1 ]
then
    echo " Cleaning/moving into $W:"
    echo
else
    echo " Following orphaned dot files (if any) found:"
fi

#-- process the file containing dotted file names --
while read f
do
    total=$(($total+1))

    #-- build visible file name --
    dot=`basename "$f"`
    dir=`dirname "$f"`
    vis=$dir/`echo "$dot" | cut -b 2-`

    #-- skip existing files and symlinks --
    if [ ! -r "$vis" -a ! -h "$vis" ]
    then
       #-- dot file exists, but not the visible file --
       ls -o "$f"
       found=$(($found+1))

       if [ $clean = 1 ]
       then
           echo "Enter 'y' or 'Y' to move to $W:"
           read ans < /dev/tty
           if [ "$ans" = "y" -o "$ans" = "Y" ]
           then
               echo "Clean $f"
               mv "$f" $W/.
               echo
           fi
       fi
    fi
done < $TMP

echo "--- $found orphaned files among $total processed dot files"
[ $clean != 1 -a $found != 0 ] && echo "--- To clean them do: $0 clean"
rm $TMP
