File sh.rellink of Package lifelines

set +o posix
relpath ()
{
    local OLDIFS IFS
    local orgwords locwords
    local newpath=""
    local relp deep p l

    OLDIFS="$IFS"; IFS=/
    eval declare -a orgwords=(${1// /\\\\ })
    eval declare -a locwords=(${2// /\\\\ })
    IFS="$OLDIFS"

    declare -i relp=0
    declare -i deep=0

    for p in "${orgwords[@]}" ; do
	eval l="\${locwords[$((deep++))]}"
	if test "$l" != "$p" -o $relp -ne 0 ; then
	    : $((relp++))
	    newpath="${newpath}/$p"
	    test -n "$l" || continue
		if test $relp -eq 1 ; then
		    newpath="..${newpath}"
		else
		    newpath="../${newpath}"
		fi
	 fi
    done
    unset OLDIFS orgwords p l

    if test $deep -lt ${#locwords[@]} ; then
	relp=0
	while test $relp -lt $deep; do
	    unset locwords[$((relp++))]
	done
	while test ${#locwords[@]} -gt 0  ; do
	    newpath="../${newpath}"
	    unset locwords[$((relp++))]
	done
    fi

    echo "$newpath"
}

rellink ()
{
    local relpath=$(relpath "${1%/*}" "${2%/*}" 2>/dev/null)
    if test -d "$1" ; then
	ln -sf "$relpath/${1##*/}/" "$2"
    else
	ln -sf "$relpath/${1##*/}"  "$2"
    fi
}