#!/bin/sh
#########################################################################
##                                                                     ##
##        i use that script to change all hardlinks to symlinks        ##
##                                                                     ##
#########################################################################

if [ $# != 1 ]; then
    cat <<EOF
Usage: double target-directory
This script replaces all hard links in the target-directory
and all subdirectories with symlinks.
EOF
    exit 1
fi

ROOT=$(echo $1 | sed "s/\/*$//")
echo $ROOT

OLDSIZE="0"

for i in $(find $ROOT -links +1 -type f -printf "%s,%h/%f\n" | sort); do
    NEWSIZE=$(echo $i | cut -d, -f1)
    NEWNAME=$(echo $i | cut -d, -f2)
    if [ "$OLDSIZE" = "$NEWSIZE" ]; then
	echo "Linking $NEWNAME->${OLDNAME##*"$ROOT"}"
	rm $NEWNAME
	ln -s ${OLDNAME##*"$ROOT"} $NEWNAME
    fi
    OLDSIZE=$NEWSIZE
    OLDNAME=$NEWNAME
done
    