#!/bin/bash
# mr - move to rubbish
# mr @files
# moves each of @files to ~/rubbish/${basename}_`nano=1 dt0`
f=0

. opts

writable_check=1
if [ "$f" = 1 ]; then
	writable_check=0
fi

if [ -z "$RUBBISH" ]; then
	M=`mntpoint "$1"`
	if [ "$M" = "$(mntpoint "$HOME")" -o "$M" = / ]; then
		RUBBISH="$HOME/.rubbish"
	else
		RUBBISH="$M/.rubbish"
	fi
fi
(umask 0700; mkdir -p "$RUBBISH"; chmod 0700 "$RUBBISH")
status=0
for A; do
	if [ "$writable_check" = 1 ]; then
		unwritable=`find "$@" -type f ! -writable`
		if [ -n "$unwritable" ]; then
			echo >&2 "$unwritable"
			confirm "Remove unwritable files, or abort?" || exit 1
		fi
	fi
	N=`basename -- "$A"`
	while true; do
		B="$RUBBISH/${N}_`nano=1 dt0`_$$"
		[ -e "$B" ] || break  # XXX not entirely secure, should use >| to creat or something?
	done
	mv -v -i -- "$A" "$B" || status=1
	[ -n "$mr_echo" ] && echo "$DEST"
done
exit $status
