summaryrefslogtreecommitdiff
path: root/Build/source/build-aux/relpath
blob: f448cfcd84e0e8a69f456aaf2cae7ee19c77d0c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /bin/sh
# relpath - compute the relative path FROM -> TO

# Copyright (C) 2011 Peter Breitenlohner <tex-live@tug.org>
#
# This file is free software; the copyright holder
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.

# First handle the simple case that FROM is a subdirectory of TO

case $2 in
  "$3"/*)
    echo "X$2" | sed 's,^X'$3'/,,; s,[^/][^/]*,..,g'
    exit 0;;
esac

# Now the more general case, mainly for distro builds

err () {
  echo "$*" >&2
  exit 1
}

test $# = 3 || err 'relpath PREF FROM TO
	compute the relative path FROM -> TO
	where FROM and TO are absolute paths, and
	prefixed with PREF are existing directories'

chk () {
  case $2 in
    *' '* | *'/./'* | *'/../'*) err "'$2' contains ' ', '/./', or '/../'";;
    /*) ;;
    *) err "'$2' must be an absolute path";;
  esac
  test -d "$1$2" || err "'$1$2' not a directory"
  # Normalize, remove leading and trailing /
  res=`echo $2 | sed 's,^//*,,; s,//*$,,; s,///*,/,g'`
}

chk "$1" "$2"; from=$res
chk "$1" "$3"; to=$res

test "/$from" = "/$to" && { echo "."; exit 0; }

# Remove common prefix
while test "/$to" != "/"; do
  test "/$from" = "/" && { echo "$to"; exit 0; }
  test `echo $from | sed 's,/.*,,'` = `echo $to | sed 's,/.*,,'` || break
  from=`echo $from | sed 's,[^/]*/*,,'`
  to=`echo $to | sed 's,[^/]*/*,,'`
done

from=`echo $from | sed 's,[^/][^/]*,..,g'`
test "/$to" = "/" || from="$from/$to"

echo "$from"
exit 0