#!/bin/sh
# hard2soft v1.0  (c) 8.6.2000 by Andreas Ley  (u) 8.6.2000
# Issue commands to change hardlinks to softlinks

usage()
{
	echo "Usage: `basename $0` [-h] [directory [...]]" >&2
	exit 1
}

set -- `getopt h $*` || usage

while :; do
	case $1 in
		-h)     sed '1d;s/^# *//;/^$/q' $0; usage;;
		--)     shift; break;;
	esac
done

test $# -eq 0 && usage

find ${*:-.} -type f ! -links 1 -print | gxargs ls -li | sort -n | \
while read inode perm links owner group size month day year file; do
	if test "${inode}" != "${linkinode}"; then
		linkinode="${inode}"
		linkfile="${file}"
	else
		cnt=1
		while test "`echo \"${linkfile}\" | cut -d/ -f-${cnt}`" = "`echo \"${file}\" | cut -d/ -f-${cnt}`"; do
			cnt="`expr ${cnt} + 1`"
		done
		linkbase="`echo \"${linkfile}\" | cut -d/ -f${cnt}-`"
		base="`echo \"${file}\" | cut -d/ -f${cnt}-`"
		link="`echo "${base}" | sed 's/[^/]*\//..\//g;s/[^/]*$//'`${linkbase}"
		echo "ln -s -f ${link} ${file}"
	fi
done

exit 0
