#!/bin/bash
#
# Issue assorted Linux system commands that reference root-only access
#  directories or files.
#
# Description: Issue to run system commands to privilege directories
#
# Change Activity:
#   10/10/2006 ferrants    xxxxxx - initial version
#
#STARTUSAGE
#
# Usage:
#   saveupCommandUtil -c list -d <file(s)>
#                     -c find -d <directory>
#
# Where:
#   -c   The operation to be performed.  Must be one of:
#        . list - issues the 'ls -1' command against the specified files. 
#                 Meant to deliver stdOut in a specific format
#        . find - issues the 'find' command with args to specified
#                 directory.
#
#   -d   The target directory
#   -f   The input list of files(s) to query. May be wildcarded
#
# Return Codes:
# * - system command return codes
#
#ENDUSAGE
#****************************************************************************

function showUsage() {
   # Print out the prologue comments as usage info
   sed -e '/STARTUSAGE/,/ENDUSAGE/ s/^#//' -e '1,/STARTUSAGE/ d' -e '/ENDUSAGE/,$ d' "$0"
}

# Initialize variables that hold option values 
giveUsage=0
cmd=
directory=
files=

# Parse the options
while getopts 'c:f:d:?' optname; do
   case "$optname" in
      c) cmd="$OPTARG";;
      d) directory="$OPTARG";;
      f) files="$OPTARG";;
      \?) giveUsage=1; break;;
   esac
   noopt=0
done

if [ "$giveUsage" -eq 1 ]; then
   showUsage
   exit 99
fi
      
if [ -z "$cmd" ]; then
   echo "Missing required argument command argument"
   showUsage
   exit 99
fi

#
# Handle the various requests
#

if [ "$cmd" == "list" ]; then
   # This request will echo the 'ls -1' cmd to stdout
   if [ -z "$files" ]; then
      echo "Missing required -f argument"
      showUsage
      exit 99
   fi
   
   # issue the 'ls' command...
   ls -1 $files
   lsRC=$?
   exit $lsRC 
   
elif [ "$cmd" == "find" ]; then
   # verify checksum values
   if [ -z "$directory" ]; then
      echo "Missing required -d argument"
      showUsage
      exit 99
   fi
   
   # issue the 'find' command...
   find $directory ! -type b ! -type c ! -type p ! -type s -type f
   findRC=$?
   exit $findRC 

else
   echo "Unknown command verb: $cmd"
   showUsage
   rc=99
fi
