#!/bin/bash

# Copyright (c) 2003 Peter A. Geer
# This program is distributed under the GNU General Public License.
# See the COPYING file in the Help directory for details.

if [ ! -L "$0" ] ; then
	APP_DIR=`dirname "$0"`
	APP_DIR=`cd "$APP_DIR";pwd`
else
	if [ -n `which readlink 2> /dev/null` ] ; then
		APP_DIR=`readlink "$0"`
		APP_DIR=`dirname "$APP_DIR"`
	# Some people won't have Python, or will have an old version.
	elif [ -n `(python -V 2>&1) | grep '\([2-9].[2-9]..\)\|\(3.1..\)'` ] 
	then
		APP_DIR=`python -c \
'import os.path, sys
apprun = os.path.realpath(sys.argv[1])
print os.path.dirname(apprun)' "$0"`
	else
		echo "Trash: Can't get real path to AppRun!  Terminating." 1>&2
		exit 1
	fi
fi

export APP_DIR
APP_NAME=`basename $APP_DIR`  # Should this be the literal 'Trash'?
# EXEC_NAME=`basename "$0"`
export PATH="$APP_DIR":$PATH

# Get choices path
CHOICES_DIR=`choices.sh $APP_NAME`

# Get the UI text.  This constitutes a poor excuse for i18n.
MSG_FILE="$APP_DIR/text/${LANG:0:5}-text.sh"
if [ ! -r $MSG_FILE ] ; then
	MSG_FILE="$APP_DIR/text/C-text.sh"
fi
# Export the file name so that simple-msg.sh can find it.
export MSG_FILE
source "$MSG_FILE"

# Let's make sure libtrash is enabled before we start deleting files.
if [ -z `echo $LD_PRELOAD | grep 'libtrash.so'` ] ; then
	HAS_LIBTRASH=no
else
	HAS_LIBTRASH=yes
fi

# Make sure the trash can is set.
if [ -z $TRASH_CAN ] ; then
	if [ -r "$CHOICES_DIR/trashcan" ] ; then
		TRASH_CAN=`cat $CHOICES_DIR/trashcan`
	else
		TRASH_CAN=Trash
	fi
fi

# Make sure the trash dir isn't the same as the AppDir.
if [[ "$TRASH_CAN" == "$APP_DIR" && "$1" != '--options' ]] ; then
	simple-msg.sh -i "$TRASH_IS_APPDIR"
	exit 1
fi

# Make sure the trash can exists.
if [ ! -d "$HOME/$TRASH_CAN" ] ; then
	mkdir -p "$HOME/$TRASH_CAN"
	if [ ! -d "$HOME/$TRASH_CAN" ] ; then
		simple-msg.sh -a "$TRASH_NOT_SET"
		exit 1
	fi
fi

# Get the file wiping command, if applicable.
WIPE_CMD=`cat "$CHOICES_DIR/wipe_cmd" 2> /dev/null`
WIPE_CMD=${WIPE_CMD:-'shred -u -z -f'}
DEL_WIPE=`cat "$CHOICES_DIR/del_wipe" 2> /dev/null`

# Are we in incinerator mode?
if [[ -r "$CHOICES_DIR/trash_off" ]] ; then
	NO_TRASH=`cat "$CHOICES_DIR/trash_off"`
else
	NO_TRASH=NO
	if  [[ ! -d "$CHOICES_DIR" ]] ; then
		mkdir -p "$CHOICES_DIR"
	fi
	if [ -w "$CHOICES_DIR" ] ; then
		echo $NO_TRASH > "$CHOICES_DIR/trash_off"
	fi
fi

CONFFILE=/etc/libtrash.conf
PERSCONFFILE=$HOME/.libtrash
TRASHDIR=$HOME/$TRASH_CAN

# Find the system root directory for files not in $HOME
if [ -e "$PERSCONFFILE" ] ; then
	SYSTEMROOT=`grep 'TRASH_SYSTEM_ROOT *=' $PERSCONFFILE | sed 's/.*= *//'`
fi
# No personal conf file.
if [ -z "$SYSTEMROOT" ] ; then
	SYSTEMROOT=`grep 'TRASH_SYSTEM_ROOT *=' "$CONFFILE" | sed 's/.*= *//'`
fi

# No arguments, just open the trash
if [ $# == 0 ] ; then
	exec rox $TRASHDIR
fi

case $1 in
	--empty)
		CHANGE_ICON=`cat "$CHOICES_DIR/change_icon" 2> /dev/null`
		CHANGE_ICON=${CHANGE_ICON:-0}
		EMPTY_WIPE=`cat "$CHOICES_DIR/empty_wipe" 2> /dev/null`
		
		# Try to find and wipe all the regular files under TRASHDIR,
		# then just delete the entire directory wholesale.
		export TRASH_OFF=YES 
		if [[ "$EMPTY_WIPE" == 1 ]] ; then
			find "$TRASHDIR" -type f -exec $WIPE_CMD {} \;
		fi
		rm -rf "$TRASHDIR"
		
		# Remake the directory and turn libtrash back on.
		mkdir -p $TRASHDIR
		if [ -n $SYSTEMROOT ] ; then
			mkdir -p $TRASHDIR/$SYSTEMROOT
		fi
		export TRASH_OFF=NO
		
		if [[ $CHANGE_ICON == 1 && $NO_TRASH == NO ]] ; then
			cp -f "$APP_DIR/Empty.png" "$APP_DIR/.DirIcon"
			rox -x "$APP_DIR"
		fi
		exit 0
	;;
	--open)
		rox "$TRASHDIR"
		rox -x "$TRASHDIR"
		exit 0
	;;
	--trash-on)
		NO_TRASH=NO
		CHANGE_ICON=`cat "$CHOICES_DIR/change_icon" 2> /dev/null`
		CHANGE_ICON=${CHANGE_ICON:-0}
		if [ -w "$CHOICES_DIR/trash_off" ] ; then
			echo $NO_TRASH > "$CHOICES_DIR/trash_off"
			if [ -w "$APP_DIR" ] ; then
				if [[ ($CHANGE_ICON == 1) && (`ls -a "$TRASHDIR" | wc -l` -gt 3) ]] ; then
					cp -f "$APP_DIR/Full.png" "$APP_DIR/.DirIcon"
				else 
					cp -f "$APP_DIR/Empty.png" "$APP_DIR/.DirIcon"
				fi
				rox -x "$APP_DIR"
			else
				echo $ICON_ERROR >&2
			fi
		else
			echo $CHOICES_ERROR >&2
		fi
		exit 0
	;;
	--trash-off)
		NO_TRASH=YES
		if [ -w "$CHOICES_DIR/trash_off" ] ; then
			echo $NO_TRASH > "$CHOICES_DIR/trash_off"
			if [ -w "$APP_DIR" ] ; then
				cp -f $APP_DIR/Burning.png $APP_DIR/.DirIcon
				rox -x $APP_DIR
			else
				echo $ICON_ERROR >&2
			fi
		else
			echo $CHOICES_ERROR >&2
		fi
		exit 0
	;;
	--wipe-on) 
		echo 1 > "$CHOICES_DIR/del_wipe" 
		exit 0
	;;
	--wipe-off) 
		echo 0 > "$CHOICES_DIR/del_wipe" 
		exit 0
	;;
	--size)
		TRASH_SIZE=`du -sh $TRASHDIR | cut -f 1`
		MSG_TEXT="$SIZE_LABEL: $TRASH_SIZE"
		RES=2
		if [[ -n `pymessage --version 2> /dev/null` ]] ; then
			MSG_TEXT="$SIZE_LABEL: <b>$TRASH_SIZE</b>"
			pymessage -m -t "$SIZE_TITLE" -d "$CANCEL" -b "$OPEN:0,$EMPTY:delete:1,$CANCEL:cancel:2" "$MSG_TEXT"
			RES=$?
		elif [[ -n `gxmessage --version 2> /dev/null` ]] ; then
			gxmessage -b "$OPEN:0,$EMPTY:1,$CANCEL:2" -default "$CANCEL" "$MSG_TEXT"
			RES=$?
		elif [[ -n `gmessage --version 2> /dev/null` ]] ; then
			gmessage -b "$OPEN:0,$EMPTY:1,$CANCEL:2" -default "$CANCEL" "$MSG_TEXT"
			RES=$?
		else
			xmessage -b "$OPEN:0,$EMPTY:1,$CANCEL:2" -default "$CANCEL" "$MSG_TEXT"
			RES=$?
		fi
		case $RES in
			0) rox "$TRASHDIR" 
				rox -x "$TRASHDIR"
				exit 0
				;;
			1) exec "$APP_DIR/AppRun" --empty ;;
			*)	exit 0;;
		esac
	;;
	--options)
		if [[ -z `pymessage --version 2> /dev/null` ]] ; then
			simple-msg.sh -a "$PYMESSAGE_ERROR"
			rox "$CHOICES_DIR"
			exit 0
		fi

		CHANGE_ICON=`cat "$CHOICES_DIR/change_icon" 2> /dev/null`
		CHANGE_ICON=${CHANGE_ICON:-0}

		# NO_TRASH is a YES or NO, so we have to converto to 1/0
		if [ $NO_TRASH == YES ] ; then
			BURN=1
		else
			BURN=0
		fi
		USE_PROMPT=`cat $CHOICES_DIR/use_prompt 2> /dev/null`
		VALS=`pymessage -b "$SAVE:save:0,$CANCEL:cancel:1" -d "$SAVE" \
			-t "$OPTIONS_TITLE" \
			-e "$TRASHDIR_LABEL:trashcan:$TRASH_CAN" \
			-e "$SECURE_CMD:wipecmd:$WIPE_CMD" \
			-k "$INCINERATOR_LABEL:incinerator:$BURN" \
			-k "$INCINERATOR_PROMPT:prompt:$USE_PROMPT" \
			-k "$ICON_CHANGE:chicon:$CHANGE_ICON" \
			-k "$SECURE_INCINERATOR:wipeon:$DEL_WIPE" \
			-k "$SECURE_EMPTY:wipeemp:$EMPTY_WIPE" \
			$OPTIONS_TITLE`
		if [[ $? == 1 ]] ; then
			exit 0
		fi
		parser.sh prompt "$VALS" > "$CHOICES_DIR/use_prompt"
		parser.sh trashcan "$VALS" > "$CHOICES_DIR/trashcan"
		parser.sh wipecmd "$VALS" > "$CHOICES_DIR/wipe_cmd"
		parser.sh wipeon "$VALS" > "$CHOICES_DIR/del_wipe"
		parser.sh wipeemp "$VALS" > "$CHOICES_DIR/empty_wipe"
		CHANGE_ICON=`parser.sh chicon "$VALS"`
		echo $CHANGE_ICON > "$CHOICES_DIR/change_icon"
		BURN=`parser.sh incinerator "$VALS"`
		# Check BURN to keep the icon from flashing.
		if [[ $BURN != 1 ]] ; then
			# Check if there's more than ., .., and SYSTEM_ROOT in there.
			if [[ ($CHANGE_ICON == 1) && (`ls -a "$TRASHDIR" | wc -l` -ge 3) ]] ; then
				cp -f "$APP_DIR/Full.png" "$APP_DIR/.DirIcon"
			else
				cp -f "$APP_DIR/Empty.png" "$APP_DIR/.DirIcon"
			fi
			rox -x "$APP_DIR"
		fi
		if [ $BURN == 1 ] ; then
			"$APP_DIR/AppRun" --trash-off
		else
			"$APP_DIR/AppRun" --trash-on
		fi
	;;
	*)
		#This is the case where we have files to delete.
		
		USE_PROMPT=`cat "$CHOICES_DIR/use_prompt" 2> /dev/null`
		CHANGE_ICON=`cat "$CHOICES_DIR/change_icon" 2> /dev/null`
		CHANGE_ICON=${CHANGE_ICON:-0}

		# Prompt for confirmation.
		if [[ $USE_PROMPT == 1 && $NO_TRASH == YES ]] ; then
			MSG_TEXT="$DELETE_WARNING: 
$@"
			simple-msg.sh "$MSG_TEXT"
			RES=$?
			if [[ $RES != 0 ]] ; then
				exit 1
			fi
		fi
	
		# Get the command to use when deleting.
		if [[ "$DEL_WIPE" == 1 ]] ; then
			DEL_CMD="$WIPE_CMD"
		else
			DEL_CMD='rm -rf'
		fi
	
		while [ $# != 0 ] ; do
			# If it's already in the trash, permanently delete it.  
			# Change icon if needed.
			if [[ `echo "$1" | grep "$TRASHDIR"`x != x || "$NO_TRASH" == YES ]]
			then
				if [[ "$DEL_WIPE" == 1 && -d "$1" ]] ; then
					export TRASH_OFF=YES 
					find "$1" -type f -exec $WIPE_CMD {} \;
					TRASH_OFF=YES rm -rf "$1"
					export TRASH_OFF=NO
				else 
					TRASH_OFF=YES $DEL_CMD "$1" 
				fi
				if [[ ($CHANGE_ICON == 1) && (`ls -a "$TRASHDIR" | wc -l` -eq 3) && ("$NO_TRASH" != YES) ]] ; then
					cp -f "$APP_DIR/Empty.png" "$APP_DIR/.DirIcon"
					rox -x "$APP_DIR"
				fi
			# If we have libtrash and the trash is on, then do an rm.
			# Change the icon, if appropriate.
			elif [[ $HAS_LIBTRASH == yes ]] ; then
				TRASH_OFF=NO rm -rf "$1"
				if [[ $CHANGE_ICON == 1 ]] ; then
					cp -f "$APP_DIR/Full.png" "$APP_DIR/.DirIcon"
					rox -x "$APP_DIR"
				fi
			# If we don't have libtrash, then get a unique name and move
			# the file to it.  Change the icon if indicated.
			else
				TARGET_NAME=`basename "$1"`
				TARGET_NAME=`getname.sh $TRASHDIR "$TARGET_NAME"`
				TARGET_PATH="$TRASHDIR/$TARGET_NAME"
				mv -f "$1" "$TARGET_PATH"
				if [[ $CHANGE_ICON == 1 ]] ; then
					cp -f "$APP_DIR/Full.png" "$APP_DIR/.DirIcon"
					rox -x "$APP_DIR"
				fi
			fi
			FILE_DIR=`dirname "$1"`
			FILE_DIR=`(cd $FILE_DIR;pwd)`
			rox -x "$FILE_DIR"
			shift
		done
	;;
esac
