#!/bin/bash -e
# vmlnback: move files to a directory and link back

# check that the idiot user (i.e. me) didn't using opts after the files
# really don't use any options with this script
for arg; do
	if [ "$arg" == "--" ]; then
		break
	fi
	if [ "${arg:0:1}" = "-" ]; then
		echo "vmlnback: don't use options such as -iv etc" >&2
		exit 1
	fi
done

dest="`p "$1"`"
shift

for A; do
	A=${A%/}  # gets rid of only one trailing slash
	DEST_DIR=""
	if [ -d "$dest" ]; then
		DEST_DIR=1
	fi
	echo -n "mv   "
	mv -v -i -- "$A" "$dest" || . fail "can't mv $A $dest"
	echo -n "ln   "
	if [ -n "$DEST_DIR" ]; then
		ln -v -s -- "$dest"/"`basename -- "$A"`" "$A" || . fail "can't ln -s back"
	else
		ln -v -s -- "$dest" "$A" || . fail "can't ln -s back"
	fi
done
