#/bin/sh -f

# usage: truepath <path>
#
# This program prints to stdout the real path of a linked file.

# Check the arg count

if [ "$#" != "1" ]; then
	echo "usage: truepath <path>"
	exit 1
fi


path="$1"


# Is the path absolute?

if [ `echo $path | sed -e "s,^/.*,YES,g"` = "YES" ]; then
	dir=`dirname $path`
else
	dir="`pwd`/`dirname $path`"
fi

prog="`basename $path`"
cd $dir
dir="`pwd`"

while :; do
        file="$dir/$prog"
        if [ `ls -ld $file | wc -w` != `ls -ld / | wc -w` ]; then
                file="`ls -l $file | awk '{ print $11 }'`"
                dir="`dirname $file`"
                prog="`basename $file`"
                cd $dir
                dir="`pwd`"
        else
                break;
        fi
done

echo $file
