2 # relpath - compute the relative path FROM -> TO
4 # Copyright (C) 2011 Peter Breitenlohner <tex-live@tug.org>
6 # This file is free software; the copyright holder
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
10 # First handle the simple case that FROM is a subdirectory of TO
14 echo "X$2" |
sed 's,^X'$3'/,,; s,[^/][^/]*,..,g'
18 # Now the more general case, mainly for distro builds
25 test $# = 3 || err
'relpath PREF FROM TO
26 compute the relative path FROM -> TO
27 where FROM and TO are absolute paths, and
28 prefixed with PREF are existing directories'
32 *' '* |
*'/./'* |
*'/../'*) err
"'$2' contains ' ', '/./', or '/../'";;
34 *) err
"'$2' must be an absolute path";;
36 test -d "$1$2" || err
"'$1$2' not a directory"
37 # Normalize, remove leading and trailing /
38 res
=`echo $2 | sed 's,^//*,,; s,//*$,,; s,///*,/,g'`
41 chk
"$1" "$2"; from
=$res
42 chk
"$1" "$3"; to
=$res
44 test "/$from" = "/$to" && { echo "."; exit 0; }
46 # Remove common prefix
47 while test "/$to" != "/"; do
48 test "/$from" = "/" && { echo "$to"; exit 0; }
49 test `echo $from | sed 's,/.*,,'` = `echo $to | sed 's,/.*,,'` ||
break
50 from
=`echo $from | sed 's,[^/]*/*,,'`
51 to
=`echo $to | sed 's,[^/]*/*,,'`
54 from
=`echo $from | sed 's,[^/][^/]*,..,g'`
55 test "/$to" = "/" || from
="$from/$to"