#!/bin/sh

# $Id: linkem,v 1.1 2000/08/25 15:12:28 bame Exp $

# This little script eliminates redundant files with duplicate contents
# within the named directories, inserting hard links so things still
# look the same (modulo ownership/permissions).
#
# Actually the script produces a script to be passed through 'sh' in
# order to accomplish the removal and linking since it pays to inspect
# the steps until you think this script is working.

case "$#:$1" in
    0:*|*:-[h?]*)
	echo "Usage: $0 directories" >&2
	exit 2;;
esac

find $* -type f -print | 
	xargs md5sum |
	sort |
	while read sum fname
	do
	    if [ X$oldsum = X$sum ]
	    then
		set -- $(ls -i $oldfname $fname)
		if [ X$1 != X$3 ]
		then
		    echo "chmod 777 $fname ; rm $fname && ln $oldfname $fname"
		fi
	    else
	    	oldfname=$fname
		oldsum=$sum
	    fi
	done
