#!/bin/ksh
#      *************************************************************
#      *                                                           *
#      * Script to Examine User's Private Program Development Tree *
#      *                                                           *
#      *************************************************************
#
#                  COPYRIGHT (c) 1994, 1997, 2003  BY
#         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
# 
# THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
# ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
# INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
# COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
# OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
# TRANSFERED.

TMP=/tmp/$$_tmp

trap "rm -f ${TMP} ; exit" INT EXIT
rm -f ${TMP}

# ------------------------------
# Process command line options
#
HELP="false"
ROOT="${HOME}/sigmet"
PURGE="false"

while [ ! "$1" = "" ] ; do
  ARG="$1" ; shift

  if [ "${ARG}" = "-help" -o "${ARG}" = "-?" ] ; then
    HELP="true" ;

  elif [ "${ARG}" = "-purge" ] ; then
    PURGE="true"

  elif [ "${ARG}" != "${ARG#-}" ] ; then
    echo "Unrecognized option: ${ARG}"
    HELP="true" ;

  else
    ROOT="/home/${ARG}/sigmet"
  fi
done

if [ "${HELP}" = "true" ] ; then
  echo "Command Line Options:"
  echo "   <User> : Work with tree for specified user"
  echo "   -purge : Clean all files from the tree"
  exit
fi

# Check that the root directory is valid, and go there.
#
if [ ! -d "${ROOT}" ] ; then
  echo "Illegal directory: ${ROOT}" ; exit
fi
cd ${ROOT} ;

if [ "${PURGE}" = "true" ] ; then

  # Purge files from the tree
  #
  echo "Files resident in ${ROOT}..."
  find . -type f -print | sort > ${TMP} ; cat ${TMP} | nl

  echo ""
  read "REPLY?Delete these files ? [no]"
  if [ "${REPLY}" = "yes" ] ; then
    echo "Deleting files...\c"
    cat ${TMP} | xargs rm -f
    echo " Done."
  fi

else
  # Display usage information.  First make a list of all of the files,
  # and then display them in different categories.
  #
  find . -type f -print | \
    egrep -v "/\.dde|\.o$|/P.*\.f|~$|/topLevelDir$" | \
    sort > ${TMP}

  echo "IRIS Source rooted at ${ROOT}..."
  egrep -v "\.a$" ${TMP} | nl

  echo ""
  echo "IRIS Libraries rooted at ${ROOT}..."
  egrep    "\.a$" ${TMP} | nl

  # Overall usage could be displayed with 'du -s *', but those totals
  # would include the sizes of the directory entries themselves, which
  # we really don't want to see.  So brute force just the files sizes.
  #
  echo ""
  echo "    Overall Usage"
  echo "Blocks  Files  Directory"
  echo "------  -----  ---------"
  for TOPITEM in * ./ ; do
    if [ -d ${TOPITEM} ] ; then
      BLOCKS="0" ; FILES="-1"
      for VAL in `find ${TOPITEM} -type f | xargs du -s | \
        grep -v '^[0-9]*[	 ]*.$' | sed 's/[	 ].*$//'` 0 ; do
        let BLOCKS="((BLOCKS+VAL))" ; let FILES="((FILES+1))"
      done
      if [ "${BLOCKS}" != "0" -o "${TOPITEM}" = "./" ] ; then
        TYPEDBLOCKS="${BLOCKS}" ; typeset -R6 TYPEDBLOCKS
        TYPEDFILES="${FILES}"   ; typeset -R6 TYPEDFILES
        echo "${TYPEDBLOCKS} ${TYPEDFILES}  ${TOPITEM}"
      fi
    fi
  done

fi
