#! /bin/ksh
##--------------------------------------------------------------------------
## List DCE Cell directories via the CDS Control Program `cdscp'.
##                                   CDS = Cell Directory Services.
##
## Usage: cdsList [-od] [dir]
##
##                       dir    defaults to /.:/subsys/pd.
##
##                 -o           List objects also.
##                 -d pattern   Delete objects matching egrep pattern.
##                 -D           Delete directories.
##
## e.g.:  cdsList		# List all directories in the PD cell.
##        cdsList -o		# List all directories *and* objects.
##        cdsList -o -d user	# Delete all objects     matching `user'.
##        cdsList -o -d host	# Delete all objects     matching `host'.
##        cdsList -D -d user	# Delete all directories matching `user'. 
##        cdsList -oDd "user|host"	# Delete all objects and directories
##					# matching `user' or `host'.
##        cdsList -o /.:/subsys/pd/servers/srvr_objs/mySpooler
##					# List objs in my spooler.
##        cdsList -o /.:/		# List all cell objects.
##
## Note: 1. Lines ending in '/' are directories,
##          otherwise they are objects.
##       2. When deleting, the output format is:
##              return_code: name_of_object_deleted
##       3. The `user' and `host' paradigms only work if the directory
##          or object name has the username or the hostname embedded
##          in it.
##
## Author: HP, JJB, Jeff Bralley, Aug95.
## $Header: cdsList,v 34.7 95/09/19 13:39:16 ssa Exp $
##--------------------------------------------------------------------------
#
typeset thisF=$_
typeset bn=$(basename $0)
typeset prefix=/.:/subsys/pd
typeset oOpt=False
typeset dOpt=""
typeset DOpt=False
#
#---------------------------------------------------------------------------
# Help requested?
#
Help(){ grep '^##' $thisF; }
case $1 in -h|-help|'?'|--h|--help) Help; exit;; esac	# Help requested?
#
#---------------------------------------------------------------------------
# Get command line options.
#
while getopts "od:D" opt; do
    case $opt in
        o) oOpt=True;;
        d) dOpt="$OPTARG";;
        D) DOpt=True;;
        \?) Help; exit;;
    esac
done
shift OPTIND-1
[ $# -gt 1 ] && echo "Too many parameters. \$1=starting_dir" && Help && exit
#
#---------------------------------------------------------------------------
# Bogus switch combos?
#
if [[ $DOpt = True && "$dOpt" = "" ]]; then
    echo "-D switch set without -d switch.  Delete *all* not allowed."
    Help
    exit 1
fi
#
#---------------------------------------------------------------------------
# Start stopwatch.
#
((t0=$SECONDS))
#
#---------------------------------------------------------------------------
# CDS recursive list function.
#
cdsList(){
    cdscp list directory $1/* |\
    grep '^[a-zA-Z]' |\
    while read d; do
        D=$1/$d
        [ "$dOpt" = "" ] && echo $D
        if [ $oOpt = True ]; then
            cdscp list object $1/$d/* |\
            grep '^[a-zA-Z]' |\
            while read o; do
                object=$1/$d/$o
                if [ "$dOpt" = "" ]; then	# Delete object?
                    echo $object		#    No.
                else				#    Yes.
                    echo $object | egrep "$dOpt" 1>/dev/null 2>&1 # Match?
                    if [ $? -eq 0 ]; then
                        cdscp delete object $object
                        echo $?: $object
                    fi
                fi
            done
        fi
        cdsList $1/$d		###### Recursive descent #####
        if [ $DOpt = True ]; then			# Delete directory?
            echo $D | egrep "$dOpt" 1>/dev/null 2>&1
            if [ $? -eq 0 ]; then
                cdscp delete directory $D
                echo $?: $D/
            fi
        fi
    done
}
#
#---------------------------------------------------------------------------
# Main recursion entry point.
#
cdsList ${1:-$prefix}
#
#---------------------------------------------------------------------------
# Stop stopwatch.
#
((et=$SECONDS-t0))
((hrs=et/3600));((et=et-hrs*3600));((mins=et/60));((secs=et-mins*60))
printf "\nDuration: %02d:%02d:%02d\n\n" $hrs $mins $secs
#
#---------------------------------------------------------------------------
# Graceful exit.
#
exit 0
