#!/bin/bash
#                       /usr/local/bin/delete
# https://crystalfaeries.net/posix/bin/delete
# celeste:crystalfaery DELETE 2021-02-23 18:46:55+00:00
# delete directories and/or unhardlink then zero target file(s)
# We implement a subset of the arguments accepted by GNU 'rm'

# Initialization
prefix=""				# false by default (no options)
force=""				# false by default (do not forcibly remove)
interactive=""				# false by default (do not prompt)
recursive=""				# false by default (do not recurse)
shred=""				# false by default (do not shred)
blank=$(mktemp -d)			# blank source directory for rsyncs' fast recursive directory delete
target="${blank}/.delete.$$.txt"	# default to nonexistant target to start the loop

# Iterate arguments
until [ "$target" == "" ] ; do
    if [[ $# -le 0 ]]
    then			# we have completed our work
		exit 0		# success exit
    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,
		target="$(echo "$1" | sed 's/^file:\/\/// ; s/^http:\/\/// ; s/^https:\/\/// ; s/^\/crystalfaeries.net\//~\/crystalfaeries.net\// ; s/^crystalfaeries.net\//~\/crystalfaeries.net\// ; s/^\/usr\/local\/bin/crystalfaeries.net\/posix\/bin/')"
		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 backup volumes."
		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 backup volumes."
		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 "$0: SHREDDING IS NOT YET IMPLEMENTED" |& tee -a ~/.delete.log
			exit	-1
		fi

		echo -n "$(pwd)	"	|& tee -a ~/.delete.log
		ls -Flad "$target"	|& tee -a ~/.delete.log

		if [ $interactive ]
		then
			echo -n	"`whoami`@`hostname`: " |& tee -a ~/.delete.log
			echo   "rm $prefix$interactive$force$recursive		$target" >> ~/.delete.log
				rm "$prefix""$interactive""$force""$recursive"	"$target"	# unhardlink!
		else	# NON-interactive
			if [ $recursive ]
			then #recursive delete
				if [ -d "${target}" ]
				then # a directory
					rsync --delete --recursive "$blank/"	"${target}" # FAST RECURSIVE DIRECTORY DELETE
					rmdir					"${target}"
				else # a filespec
					echo   "rm $prefix$interactive$force$recursive		$target" >> ~/.delete.log
						rm "$prefix""$interactive""$force""$recursive"	"$target"	# unhardlink!
				fi
			else #NONrecursive
					echo   "rm $prefix$interactive$force$recursive		$target" >> ~/.delete.log
						rm "$prefix""$interactive""$force""$recursive"	"$target"	# unhardlink!
			fi
		fi

		if [ -e "$target" ]
		then	# user interactively chose NOT to remove target
			echo "`/usr/local/bin/now` keeping: $target" |& tee -a ~/.delete.log
		else	# user removed target - ensure deletion of copies on website and backup media:
			cp /dev/null	"$target"	# cron job removes size 0 files after 3 months.
			echo "`/usr/local/bin/now` deleted: $target" |& tee -a ~/.delete.log
		fi

		;;
	esac
    fi # [[ $# -le 0 ]]
done # until [ "$target" == "" ]
rmdir "$blank"	#cleanup
exit	$?

