#!/bin/bash
#                      /usr/local/bin/unhardlink
# http://crystalfaeries.net/posix/bin/unhardlink
# celeste:crystalfaery 2015-03-07 22:12:35+00:00
# NOTE: we really SHOULD verify we have hardlinks not symlinks to play with
# Added option to shred and DELETE file (secure remove)
# Original source code is from:
# http://serverfault.com/questions/386514/break-all-hardlinks-within-a-folder
let	helplines=8				# how many lines above this one?

set	-e
temp="$(mktemp -d ./unhardlink-XXXXXXXX)"	# create  our temporary directory on entry
trap "rm -rf $temp; exit $?" 0 1 2 15		# cleanup our temporary directory on exit
[ -d "$temp" ] || exit -1			# abort if no temporary directory available
						# parse arguments
case	"$1" in
-h)
	head -n	$helplines	$0
	exit	0		# successfully provided help
	;;
--help)
	head -n	$helplines	$0
	exit	0		# successfully provided help
	;;
-v)
	head -n	$helplines	$0
	exit	0		# successfully provided version info
	;;
--version)
	head -n	$helplines	$0
	exit	0		# successfully provided version info
	;;
-d)
	shift	# consume the -d	argument
	for i in "$@"
	do
		cp	-ip	"$i" "$temp/tempcopy" && mv "$temp/tempcopy" "$i"	|| exit $?
		shred -f -u	"$i"							|| exit $?
	done
	;;
--delete)
	shift	# consume the --delete	argument
	for i in "$@"
	do
		cp	-ip	"$i" "$temp/tempcopy" && mv "$temp/tempcopy" "$i"	|| exit $?
		shred -f -u	"$i"							|| exit $?
	done
	;;
*)
	for i in "$@"
	do
		cp	-ip	"$i" "$temp/tempcopy" && mv "$temp/tempcopy" "$i"	|| exit $?
	done
	;;
esac


syntax highlighted by Code2HTML, v. 0.9.1