#!/bin/bash
#                       /usr/local/bin/delete
#  http://crystalfaeries.net/posix/bin/delete
# celeste:crystalfaery DELETE 2017-11-17 22:12:08+00:00
# delete removes target file(s) or director{y,ies} from both localhost and my_twin.
# We implement a subset of the arguments accepted by GNU 'rm'

# CORRESPONDENCE:
# rm -i								   /home/crystalfaeries.net/imgs/animations/159.gif
# ssh -t divservi@box6537.bluehost.com 'rm -i /home1/divservi/public_html/crystalfaeries.net/imgs/animations/159.gif'

# WE SHOULD implement the fast recursive delete of directories:
# blank=$(mktemp -d); rsync --delete "$blank/" "bigdir/"; rmdir "$blank"

# WE SHOULD BETTER HANDLE RELATIVE AND/OR ABSOLUTE PATHS
invoked="$(pwd)"		# where were we invoked?
cd				# since ssh defaults to $HOME on my_twin, default to same on local host
if [ "$invoked" != "$(pwd)" ]
then
	echo "DANGEROUS PRESUMPTION OF PATH IN $0" |& tee -a ~/.delete.log
	# we should really alter the code below instead of throwing this warning
	# we are acting by default from $HOME directory, else via absolute path,
	# but we SHOULD properly handle the CWD of invocation.
	exit	-1	# better to be safe than sorry with undoable deletes
fi

# Initialization
interactive=""			# false by default (do not prompt)
force=""			# false by default (do not forcibly remove)
recursive=""			# false by default (do not recurse)
shred=""			# false by default (do not shred)
prefix=""			# false by default (no options)
target="/tmp/.delete.$$.txt"	# default to false target to protect against insanity and start the loop

# Iterate arguments
until [ "$target" == "" ] ; do
    if [[ $# -le 0 ]]
    then			# we have completed our work
		exit 0		# success exit (we SHOULD track any errors but do not yet)
    else			# we have more arguments so claim the next argument as our target
				# accept a file URL syntax prefix,
				# an http: or https: prefix for our own website content,
				# and a /home file path with leading garbage we ignore:
		target=$(echo "$1" | sed 's/^file:\/\/// ; s/^http:\/\//\/home\// ; s/^https:\/\//\/home\// ; s/^.*\/home\//\/home\//')
		webserverpath="`echo ${target} | sed 's/\/home\/audio\/www/\/home1\/divservi\/public_html\/crystalfaeries.net\/audio/ ; s/\/home\/www\/audio/\/home1\/divservi\/public_html\/crystalfaeries.net\/audio/ ; s/\/home\/video\/www/\/home1\/divservi\/public_html\/crystalfaeries.net\/video/ ; s/\/home\/www\/video/\/home1\/divservi\/public_html\/crystalfaeries.net\/video/ ; s/\/home\/crystalfaeries.net/\/home1\/divservi\/public_html\/crystalfaeries.net/'`"
		shift		# consume   that argument (future use of "$1" refers to NEXT argument)
	case $target in
	-f)
		prefix="-"
		force="f"	# We shall forcibly remove
		;;
	--force)
		prefix="-"
		force="f"	# We shall forcibly remove
		;;
	-h)
		echo	"$0 deletes both for "`whoami`@`hostname`" (local host) and on my_twin `/usr/local/bin/my_twin`."
		echo	"$0 accepts "-h"     for this useage information."
		echo	"$0 accepts "--help" for this useage information."
		echo	"$0 accepts "-v"         for version information."
		echo	"$0 accepts "--version"  for version information."
		echo	"$0 accepts "-i" to activate interactive prompting."
		echo	"$0 accepts "-I" to activate interactive prompting."
		echo	"$0 accepts "-f"          to activate forced      removal."
		echo	"$0 accepts "--force"     to activate forced      removal."
		echo	"$0 accepts "-r"          to activate recursive   removal."
		echo	"$0 accepts "-R"          to activate recursive   removal."
		echo	"$0 accepts "--recursive" to activate recursive   removal."
		echo	"$0 accepts "-s"          to activate shredding   removal."
		echo	"$0 accepts "-S"          to activate shredding   removal."
		echo	"$0 accepts "--shred"     to activate shredding   removal."
		exit	  0
		;;
	--help)
		echo	"$0 deletes both for "`whoami`@`hostname`" (local host) and on my_twin `/usr/local/bin/my_twin`."
		echo	"$0 accepts "-h"     for this useage information."
		echo	"$0 accepts "--help" for this useage information."
		echo	"$0 accepts "-v"         for version information."
		echo	"$0 accepts "--version"  for version information."
		echo	"$0 accepts "-i" to activate interactive prompting."
		echo	"$0 accepts "-I" to activate interactive prompting."
		echo	"$0 accepts "-f"          to activate forced      removal."
		echo	"$0 accepts "--force"     to activate forced      removal."
		echo	"$0 accepts "-r"          to activate recursive   removal."
		echo	"$0 accepts "-R"          to activate recursive   removal."
		echo	"$0 accepts "--recursive" to activate recursive   removal."
		echo	"$0 accepts "-s"          to activate shredding   removal."
		echo	"$0 accepts "-S"          to activate shredding   removal."
		echo	"$0 accepts "--shred"     to activate shredding   removal."
		exit	  0
		;;
	-i)
		prefix="-"
		interactive="i"	# We shall interactively prompt for authorization to remove
		;;
	-I)
		prefix="-"
		interactive="I"	# We shall interactively prompt for authorization to remove
		;;
	-r)
		prefix="-"
		recursive="r"	# We shall recurse
		;;
	-R)
		prefix="-"
		recursive="r"	# We shall recurse
		;;
	--recursive)
		prefix="-"
		recursive="r"	# We shall recurse
		;;
	-s)
		shred="s"	# We shall shred
		;;
	-S)
		shred="s"	# We shall shred
		;;
	--shred)
		shred="s"	# We shall shred
		;;
	-v)
		echo	"$0 `grep 'celeste:crystalfaery' $0 | sed 's/^# celeste:crystalfaery //' | head -n 1`"
		exit	  0
		;;
	--version)
		echo	"$0 `grep 'celeste:crystalfaery' $0 | sed 's/^# celeste:crystalfaery //' | head -n 1`"
		exit	  0
		;;
	*)
		if [ "$shred" == "s" ]
		then
			echo "SHREDDING IS NOT YET IMPLEMENTED BY $0" |& tee -a ~/.delete.log
			exit	-1
		fi
		ls -Flad "$target" |& tee -a ~/.delete.log
		if [ $interactive ]
		then
			echo -n	"`whoami`@`hostname`: " |& tee -a ~/.delete.log
		fi
		echo   "rm $prefix$interactive$force$recursive	$target" >> ~/.delete.log
			rm "$prefix""$interactive""$force""$recursive"	"$target"
		if [ -e "$target" ]
		then
			echo "`/usr/local/bin/now` keeping: $target" |& tee -a ~/.delete.log
		else
			echo "`/usr/local/bin/now` deleted: $target" |& tee -a ~/.delete.log
		fi
		if [ $interactive ]
		then
			echo -n	"`/usr/local/bin/my_twin`: " |& tee -a ~/.delete.log
		fi
		echo   "ssh `/usr/local/bin/my_twin` rm $prefix$interactive$force$recursive $webserverpath" >>	  ~/.delete.log
			ssh `/usr/local/bin/my_twin` rm "$prefix""$interactive""$force""$recursive" "$webserverpath"
			sleep 10	# bluehost dishonors rapid connect requests
			ssh `/usr/local/bin/my_twin` "if [ -e $webserverpath ] ; then echo `/usr/local/bin/now` keeping: $webserverpath |& tee -a ~/.delete.log; else echo `/usr/local/bin/now` deleted: $webserverpath |& tee -a ~/.delete.log; fi"
			sleep 10	# bluehost dishonors rapid connect requests
		;;
	esac
    fi # [[ $# -le 0 ]]
done # until [ "$target" == "" ]
exit	$?



syntax highlighted by Code2HTML, v. 0.9.1