+# func_relativize DIR1 DIR2
+# computes a relative pathname RELDIR such that DIR1/RELDIR = DIR2.
+# Input:
+# - DIR1 relative pathname, relative to the current directory
+# - DIR2 relative pathname, relative to the current directory
+# Output:
+# - reldir relative pathname of DIR2, relative to DIR1
+func_relativize ()
+{
+ dir0=`pwd`
+ dir1="$1"
+ dir2="$2"
+ sed_first='s,^\([^/]*\)/.*$,\1,'
+ sed_rest='s,^[^/]*/*,,'
+ sed_last='s,^.*/\([^/]*\)$,\1,'
+ sed_butlast='s,/*[^/]*$,,'
+ while test -n "$dir1"; do
+ first=`echo "$dir1" | sed -e "$sed_first"`
+ if test "$first" != "."; then
+ if test "$first" = ".."; then
+ dir2=`echo "$dir0" | sed -e "$sed_last"`/"$dir2"
+ dir0=`echo "$dir0" | sed -e "$sed_butlast"`
+ else
+ first2=`echo "$dir2" | sed -e "$sed_first"`
+ if test "$first2" = "$first"; then
+ dir2=`echo "$dir2" | sed -e "$sed_rest"`
+ else
+ dir2="../$dir2"
+ fi
+ dir0="$dir0"/"$first"
+ fi
+ fi
+ dir1=`echo "$dir1" | sed -e "$sed_rest"`
+ done
+ reldir="$dir2"
+}
+
+# func_relconcat DIR1 DIR2
+# computes a relative pathname DIR1/DIR2, with obvious simplifications.
+# Input:
+# - DIR1 relative pathname, relative to the current directory
+# - DIR2 relative pathname, relative to DIR1
+# Output:
+# - relconcat DIR1/DIR2, relative to the current directory
+func_relconcat ()
+{
+ dir1="$1"
+ dir2="$2"
+ sed_first='s,^\([^/]*\)/.*$,\1,'
+ sed_rest='s,^[^/]*/*,,'
+ sed_last='s,^.*/\([^/]*\)$,\1,'
+ sed_butlast='s,/*[^/]*$,,'
+ while true; do
+ first=`echo "$dir2" | sed -e "$sed_first"`
+ if test "$first" = "."; then
+ dir2=`echo "$dir2" | sed -e "$sed_rest"`
+ if test -z "$dir2"; then
+ relconcat="$dir1"
+ break
+ fi
+ else
+ last=`echo "$dir1" | sed -e "$sed_last"`
+ while test "$last" = "."; do
+ dir1=`echo "$dir1" | sed -e "$sed_butlast"`
+ last=`echo "$dir1" | sed -e "$sed_last"`
+ done
+ if test -z "$dir1"; then
+ relconcat="$dir2"
+ break
+ fi
+ if test "$first" = ".."; then
+ if test "$last" = ".."; then
+ relconcat="$dir1/$dir2"
+ break
+ fi
+ dir1=`echo "$dir1" | sed -e "$sed_butlast"`
+ dir2=`echo "$dir2" | sed -e "$sed_rest"`
+ if test -z "$dir1"; then
+ relconcat="$dir2"
+ break
+ fi
+ if test -z "$dir2"; then
+ relconcat="$dir1"
+ break
+ fi
+ else
+ relconcat="$dir1/$dir2"
+ break
+ fi
+ fi
+ done
+}
+
+# func_ln SRC DEST
+# Like ln -s, except that SRC is given relative to the current directory (or
+# absolute), not given relative to the directory of DEST.
+func_ln ()
+{
+ case "$1" in
+ /*)
+ ln -s "$1" "$2" ;;
+ *) # SRC is relative.
+ case "$2" in
+ /*)
+ ln -s "`pwd`/$1" "$2" ;;
+ *) # DEST is relative too.
+ ln_destdir=`echo "$2" | sed -e 's,[^/]*$,,'`
+ test -n "$ln_destdir" || ln_destdir="."
+ func_relativize "$ln_destdir" "$1"
+ ln -s "$reldir" "$2"
+ ;;
+ esac
+ ;;
+ esac
+}
+