#!/usr/bin/ksh
#
# Name:    
#   handleListOfSourceids inputFile
#   See the code section below about Usage.
#
# Purpose: This is a sample shell script that handles a file
#          that has the list of the sourceIds which have
#          SCCS files that can be removed from the $CMVC/vc tree 
#          because these files are not longer referenced in the 
#          proper places in the CMVC database.
#
#          This script will NOT perform the removal of the
#          files. The CMVC administrator will have to do it.
#
# Output:  
#
#  a) A file with individual entries with the size of the fully 
#     qualified SCCS file or directory that can be removed.
#     This file will have a total of the amount of space that
#     these entries occupy in the file system.
#     The file name is the inputFile name with a suffix of .out
#
#  b) A shell script file with individual entries that have the
#     specific command to remove the SCCS file or directory.
#     The file name is the inputFile name with a prefix of
#     'remove' and with a suffix of '.ksh'
#
# Warnings:
# * Stop the CMVC family before running this script.
# * Ensure that you have a recent backup the CMVC family file 
#   system and the CMVC database before executing this command.
#
# Notes:   
# * This script needs to be run from the CMVC family user id.
#
#########################################################################

# Process the proper number of input parameters
# and display Usage, if necessary

if [ "$#" -eq 0 ]
then
   print -u2 'Usage:  '
   print -u2 'handleListOfSourceIds inputFile '
   print -u2 'Where: inputFile should contain the list of sourceIds of'
   print -u2 '       SCCS files or directories that are not referenced'
   print -u2 '       anymore in the CMVC family and which can be removed.'
   print -u2 'Example:'
   print -u2 ' handleListOfSourceids list1'
   print -u2 '  * The list of fully qualified file names to be removed '
   print -u2 '    with their sizes are shown in the file: list1.out'
   print -u2 '  * The shell script that contains the actual remove commands'
   print -u2 '    is called: remove.list1.ksh' 
   return 1
fi

inputFile=$1


# Processing the default values for the sourceIds, if not specified.
if [ ! -f $inputFile ]
then
   print 'The input file does not exist: ' $inputFile
   print 'Exiting now'.
   return 1
else
   outputFile=`echo $inputFile.out`
   print "List of fully qualified file names and " > $outputFile
   print "their file size in bytes." >> $outputFile
   print " " >> $outputFile

   scriptFile=`echo remove.$inputFile.ksh`
   print "#!/usr/bin/ksh" > $scriptFile
   print " " >> $scriptFile
fi

# The variable counter counts the number of unreferenced SCCS files that
# are found in the given range of sourceIds.
let "counter = 0"

# The variable totalSize keeps the total of the amount of bytes
# that each file or directory occupy.
let "totalSize = 0"

# Print header with information

print 'handleListOfSourceIds: Starting.'
print 'This script identifies the fully qualified names of the '
print 'unreferenced SCCS files and directories which reside in '
print 'the directory:       ' $HOME/vc
print 'for the CMVC Family: ' $LOGNAME
print ''
print 'These files are listed in the output file: ' $outputFile
print 'The script that has the remove commands is: ' $scriptFile
print ''

# Process all the entries from the inputFile

while read sourceId
do
   print "Handling sourceId: $sourceId"

   lengthSourceId=`echo "$sourceId" | wc -m | awk '{ print $1 }'`
   case "$lengthSourceId" in
   2)
     sourceId=`echo "0000$sourceId"`
   ;;
   3)
     sourceId=`echo "0000$sourceId"`
   ;;
   4)
     sourceId=`echo "000$sourceId"`
   ;;
   5)
     sourceId=`echo "00$sourceId"`
   ;;
   6)
     sourceId=`echo "0$sourceId"`
   ;;
   esac

   d1=`echo "$sourceId" | cut -c 1`
   d2=`echo "$sourceId" | cut -c 2`
   d3=`echo "$sourceId" | cut -c 3`
   d4=`echo "$sourceId" | cut -c 4`
   XX=`echo "$sourceId" | cut -c 5-6`
   sFile=`echo "\$HOME/vc/$d1/$d2/$d3/$d4/s.$XX"`
   bFile=`echo "\$HOME/vc/$d1/$d2/$d3/$d4/b.$XX/"`
   if [ -f $sFile ]
   then
      let "fileSize = `ls -l "$sFile" | awk '{ print $5 }'`"
      found="yes"
      print "$sFile = $fileSize" >> $outputFile
      print "rm $sFile" >> $scriptFile
   else
      if [ -d $bFile ]
      then
         blocks=`du -k -s "$bFile" | awk '{ print $1 }'`
         let "fileSize = $blocks * 1024"
         found="yes"
         print "$bFile = $fileSize" >> $outputFile
         print "rm $bFile*" >> $scriptFile
         print "rmdir $bFile" >> $scriptFile
      else
         print "ERROR: Cannot find the file associated with sourceId: $sourceId"
         let "fileSize = 0"
         found="no"
      fi
   fi
   let "counter = counter + 1"
   let "totalSize = $totalSize + $fileSize"

done < $inputFile

print "" >> $outputFile
print "The total byte count is: $totalSize" >> $outputFile

print "" >> $scriptFile
print "# end of script " >> $scriptFile

print ''
print 'These files are listed in the output file: ' $outputFile
print 'The script that has the remove commands is: ' $scriptFile
print ''
print ' '
print 'End of script'

return 0

# end of file
