l18n
[cake.git] / scripts / relpath
blob1656e29edf73490a819d3a28f0c878e162b1d81a
1 #!/bin/sh
2 # $0 from to
3 # Find a relative path from "to" to "from"
4 # ie. a path which can be used in "from" to get to "to".
5 # Example: $0 x/y/z a/b -> ../../../a/b
6 # Example: $0 /x/y/z a/b -> /x/y/z
7 # Example: $0 x/../y/z a/b -> ../../a/b
8 # Example: $0 x/y/../z a/b -> ../../a/b
9 # Example: $0 x/./y/z a/b -> ../../../a/b
10 # Example (in x): $0 ../y/z a/b -> ../../x/a/b
12 gawk 'BEGIN {
13 from=ARGV[1];
14 to=ARGV[2];
16 prefix="";
18 gsub (/\/$/,"",from);
20 first=1;
21 cddir=".";
23 if (substr(from,1,1)=="/")
24 print from
25 else
27 while (from!="")
29 if (!match(from,/^[^/]+/))
31 from=substr(from,2);
33 else
35 dir=substr(from,RSTART,RLENGTH);
36 from=substr(from,RSTART+RLENGTH+1);
38 if (dir!="..")
39 first=0;
41 if (dir != "." && dir != "..")
43 prefix=prefix "../";
45 else if (dir=="..")
47 if (first)
49 cmd="cd " cddir " ; pwd";
50 cmd | getline;
51 close (cmd);
52 path=$1;
53 gsub(/.*\//,"",path);
54 to=path "/" to;
55 cddir=cddir "/..";
57 else
58 prefix=substr(prefix,4);
63 print prefix to
65 }' $1 $2