#! /bin/ksh
# junk script
# Korn shell version
# author: Graham Glass
# 9/25/91
#
# Initialize variables
#
fileCount=0       # the number of files specified.
listFlag=0        # 1 if list option (-)used.
purgeFlag=0       # 1 if purge (-p) option used.
fileFlag=0        # 1 if at least one file is specified.
junk=~/.junk      # the name of the junk directory.
#
error ()
{
#
# Display error message and quit
#
cat << ENDOFTEXT
Dear $USER, the usage of junk is as follows:
 junk -p means "purge all files"
 junk -l means "list junked files"
 junk <list of files> to junk them
ENDOFTEXT
exit 1
}
#
# Parse command line
#
for arg in $*
do
 case $arg in
   "-p")
     purgeFlag=1
     ;;
    "-l")
     listFlag=1
     ;;
    -*)
     echo $arg is an illegal option
     ;;
    *)
     fileFlag=1
     fileList[$fileCount]=$arg     # append to list
     let fileCount=fileCount+1
     ;;
  esac
done
#
# Check for too many options
#
let total=$listFlag+$purgeFlag+$fileFlag
if (( total != 1 )) 
then
 error
fi
#
# If junk directory doesn't exist, create it
#
if [[ ! (-d $junk) ]]
then
 'mkdir' $junk               # quoted just in case it's aliased.
fi
#
# Process options
#
if (( listFlag == 1 )) 
then
 'ls' -lgF $junk       # list junk directory.
 exit 0
fi
#
if (( purgeFlag == 1 ))
then
 'rm' $junk/*          # remove files in junk directory.
 exit 0
fi
#
if (( fileFlag == 1 ))
then
 'mv' ${fileList[*]} $junk   # move files to junk dir.
 exit 0
fi
#
exit 0
