File sh.rellink of Package texlive-bin
set +o posix
relpath ()
{
local state=$- ; set +x
local OLDIFS IFS
local -a orgwords
local -a locwords
local -i relp=0
local -i deep=0
local p l
local path=""
OLDIFS="$IFS"; IFS=/
eval orgwords=(${1// /\\\\ }) ; shift
eval locwords=(${1// /\\\\ }) ; shift
IFS="$OLDIFS"
unset OLDIFS
deep=0
relp=0
for l in "${locwords[@]}" ; do
if test "$l" = ".." ; then
((deep++))
continue
elif test $deep -gt 0 ; then
while ((deep-- > 0)) ; do
unset locwords[$((relp+deep))]
(((relp-1)-deep < 0)) || unset locwords[$(((relp-1)-deep))]
done
continue
fi
((relp++))
done
locwords=(${locwords[@]})
deep=0
relp=0
for p in "${orgwords[@]}" ; do
if test "$p" = ".." ; then
((deep++))
continue
elif test $deep -gt 0 ; then
while ((deep-- > 0)) ; do
unset orgwords[$((relp+deep))]
(((relp-1)-deep < 0)) || unset orgwords[$(((relp-1)-deep))]
done
continue
fi
((relp++))
done
orgwords=(${orgwords[@]})
deep=0
relp=0
for p in "${orgwords[@]}" ; do
eval l="\${locwords[$((deep++))]}"
if test "$l" != "$p" -o $relp -ne 0 ; then
((relp++))
path="${path}/$p"
test -n "$l" || continue
if test $relp -eq 1 ; then
path="..${path}"
else
path="../${path}"
fi
fi
done
unset 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
path="../${path}"
unset locwords[$((relp++))]
done
fi
echo "$path"
[[ $state =~ x ]] && set -x
}
rellink ()
{
local state=$- ; set +x
local relpath=$(relpath "${1%/*}" "${2%/*}" 2>/dev/null)
if test -h "$2" ; then
rm -f "$2"
fi
if test -d "$1" ; then
ln -sf "${relpath:+${relpath}/}${1##*/}/" "$2"
else
ln -sf "${relpath:+${relpath}/}${1##*/}" "$2"
fi
[[ $state =~ x ]] && set -x
}