#!/bin/sh
#
#	myrm	- creates a trashroom
#
#	  usage: myrm [ options ]
#
#	description: 
#		creates a directory called trashroom where files to be deleted
#		are kept until myrm gets a -t operator
#


# -----------------------------------------------------------------
# 0: Functions
# -----------------------------------------------------------------

emptyTrash()
{
    #
    #  delete all files in trashroom
    #
    
    for ITEM in $TRASHLIST ; do
        rm $TRASH/$ITEM                     # delete item
    done
}


pickTroughTrash()
{
    #
    #  pick trough files in trashroom
    #
    
    for ITEM in $TRASHLIST ; do
        echo "file: $ITEM   (t)hrow out or (r)ecover?"
        read VAR
 
        if   [ $VAR = t ] ||
             [ $VAR = T ] ; then
            rm $TRASH/$ITEM                 # delete item
        elif [ $VAR = r ] ||
             [ $VAR = R ] ; then
            recover $ITEM                   # recover
        fi  
    done
}


doFile()
{
    #
    #  check for, recover or move file
    #

    if [ $RETRIEVE = TRUE ] ; then
        recover $1
    else
        movetoTrash $1
    fi
}


movetoTrash()
{
    #
    #  move file to trash
    #
    
    MOVEFILE=FALSE
    
    if [ -e $1 ] ; then                     # check for file
        
        MOVEFILE=TRUE                       # yes, found file

        if [ -e $TRASH/$1 ] ; then          # check for file in trash
        
            echo "There already is a file called $1 in the trash"
            echo "O.K. to replace it?"
            read VAR
            if [ $VAR = y   ] || 
               [ $VAR = Y   ] ; then
                :                           # yes, replace
            else
                MOVEFILE=FALSE              # no, don't replace
            fi
        fi
        
    else
        echo "Could not find file: $1"
    fi
    
    if [ $MOVEFILE = TRUE ] ; then
        cp $1 $TRASH                        # copy file to trash
        rm $1                               # delete file
    fi
}


recover()
{
    #
    #  move file from trash
    #
    
    MOVEFILE=FALSE
    
    if [ -e $TRASH/$1 ] ; then              # check for file in trash
        
        MOVEFILE=TRUE                       # yes, found file

        if [ -e $1 ] ; then                 # check for file in folder
        
            echo "There already is a file called $1 in this folder"
            echo "O.K. to replace it?"
            read VAR
            if [ $VAR = y   ] || 
               [ $VAR = Y   ] ; then
                :                           # yes, replace
            else
                MOVEFILE=FALSE              # no, don't replace
            fi
        fi

    else
        echo "Could not find file: $1 in trash"
    fi

    if [ $MOVEFILE = TRUE ] ; then
        cp $TRASH/$1 .                      # copy file to folder
        rm $TRASH/$1                        # delete file in trash
    fi
}


# -----------------------------------------------------------------
# 1: Initialize globals
# -----------------------------------------------------------------

    RETRIEVE=FALSE                          # retrieve off
    TRASH=$HOME/trashroom                   # path of trashroom
    TRASHLIST=`ls $TRASH`                   # list of trash items
    
# -----------------------------------------------------------------
# 2: Test for and create trash room
# -----------------------------------------------------------------
    
    if [ -e $TRASH ] ; then
        :                                   # trashroom exists
    else
        mkdir $TRASH                        # create trashroom
    fi
    

# -----------------------------------------------------------------
# 3: Loop through arguments list
# -----------------------------------------------------------------
    
    while [ $# -gt 0 ] ; do			        # while argc>0
    
		case $1 in
			-t)	 emptyTrash       ;;        # empty trash
			-l)	 ls $TRASH        ;;        # list trash
            -L)	 ls -l $TRASH     ;;        # list trash long
            -p)  pickTroughTrash  ;;        # pick through trash
            -g)  RETRIEVE=TRUE    ;;        # retrieve on
			*)	 doFile $1        ;;        # deal with file
		esac

		shift				                # next arg
    done
