#!/bin/sh
#
## trash -- an attempt at a trashcan command
## created 05-Aug-1994 jmk
## autodate: 25-Jan-1995
## autotime: 02:47

## ----------------------------------------------------------------
## COPYRIGHT
## `trash' is written and maintained by Jim Knoble
## <jmknoble@mercury.interpath.net>.  `trash' is copyright 1995 by
## Jim Knoble; you may freely distribute and modify it, but the
## original code must bear my name.  Any modifications that you
## introduce must bear your name and must not bear mine.
## ----------------------------------------------------------------

## the idea here is to move copies of things we want to delete
## into a hidden `trash' directory, keeping versions of things
## so that we can undelete them later if we need to.

## this makes pathname patterns which match no files
## expand to '' rather than the pattern.
## we use this in the `--empty' command.
allow_null_glob_expansion=1

## we use a variable to indicate verbose use or not;
## if it's not set, default to verbose.
if [ -z ${TRASH_VERBOSE} ]; then
    export TRASH_VERBOSE="yes"
fi
case ${TRASH_VERBOSE} in
    on | yes | true )
        trash_verbose_cmd="-v"
	;;
    off | no | false )
        trash_verbose_cmd=""
	;;
    * )
        echo "${0}: error: environment variable TRASH_VERBOSE has bad value."
	echo "${0}: value of \`$TRASH_VERBOSE' should be either \`on' or \`off'."
	echo "${0}: defaulting to \`on'"
	trash_verbose_cmd="-v"
	;;
esac

## if we don't have a trash directory defined, make a default
if [ -z ${TRASH_DIR} ]; then
    export TRASH_DIR="$HOME/.trash"
fi

## check that our trashcan is a valid directory...
if [ ! -d $TRASH_DIR ]; then
    echo "${0}: error: trashcan \`$TRASH_DIR' is not a directory."
    echo "${0}: i can only use a trashcan that is a directory."
    echo "${0}: you can set your trashcan with the TRASH_DIR environment variable."
    exit 1
elif [ "$TRASH_DIR" = "." -o "$TRASH_DIR" = "./" -o "$TRASH_DIR" = "/" ]; then
    echo "${0}: error: trashcan \`$TRASH_DIR' is not a valid trash directory."
    echo "${0}: try setting TRASH_DIR to a directory like \`~/.trash'."
    exit 1
else
    ## check for trash commands or files to trash
    trashcmd="$1"
    if [ -z $trashcmd ]; then
        trashcmd="--help"
    fi
    case $trashcmd in
        --help )
	    ## display help
            echo ""
            echo "  usage: ${0} file [file...]"
            echo "      moves <file> to \`trashcan' directory"
            echo "      specified by environment variable \`TRASH_DIR'"
            echo "      (defaults to ~/.trash)."
            echo ""
            echo "  usage: ${0} { --list | --view }"
            echo "      show contents of \`trashcan' directory"
            echo ""
            echo "  usage: ${0} --empty"
            echo "      remove all files from \`trashcan' directory"
            echo ""
	    ;;
	-l | --list | --view )
	    ## list trashcan contents
	    echo "contents of trashcan \`$TRASH_DIR':"
	    cd $TRASH_DIR
	    ## we want to make sure to catch hidden files,
	    ## so we use `-a'.
	    ls -la
	    ;;
	--empty )
            ## remove contents of trashcan
            echo "emptying trashcan \`$TRASH_DIR' ..."
	    ## we want to catch both hidden and normal files...
            for trash_files in $TRASH_DIR/.[^.]* $TRASH_DIR/*; do
	        ## we'll let ourselves know if there were any files
		## in the trashcan
	        trashed_one=1
		## we want to make sure to catch any subdirectories,
		## so we use `-r' to recurse.
                rm -r $trash_verbose_cmd $trash_files
	    done
	    if [ -z $trashed_one ]; then
                echo "${0}: trashcan was already empty."
	    fi
            ;;
        * )
            ## we have files to trash, so move the arguments to the
	    ## trashcan.
	    ## `-f' forces moving (we don't care if we overwrite)
	    ## `-b' backs files up using `-V' version control
            mv -f -b -V numbered $trash_verbose_cmd $* $TRASH_DIR
	    ;;
    esac
fi
