2 # This script assumes it is being run by bash
4 # ======= needed functions ======
5 # abspath relpath curentpath
8 # treat "./" as a special case
9 if expr "$1" : '\./' >/dev
/null
; then
10 b
=`echo $1 | sed 's@\./@@'`
16 # for each "../" we remove one directory from the current path
17 # and leading "../" from the relative path
18 # until we have the unique part in b and the abs prefix in a
19 while expr "$b" : '\.\./' >/dev
/null
21 b
=`echo $b | sed 's@\.\./@@'`
22 a
=`echo $a | sed 's@/[^/]*$@@'`
24 # return the completed absolute path
28 # relpath abspath curentpath
31 # take "/" off beginning
32 a
=`echo $1 | sed 's@^/@@'`
33 # take "/" off beginning and add to end
34 b
=`echo $2 | sed 's@^/@@;s@$@/@'`
37 if [ "$b" = "" ]; then
40 a1
=`echo $a | sed 's@\([^/]*\)/.*@\1@'`
41 b1
=`echo $b | sed 's@\([^/]*\)/.*@\1@'`
42 if [ "$a1" != "$b1" ]; then
45 a
=`echo $a | sed 's@[^/]*/@@'`
46 b
=`echo $b | sed 's@[^/]*/@@'`
48 # a now has the unique part of the path
50 # c will have the required number of "../"'s
54 b
=`echo $b | sed 's@[^/]*/@@'`
56 # return the completed relative path
62 # Assume any relative path passed in is relative to the current directory
63 # Given $1 is a path to the source file
64 # Given $2 is a path of the link to be created
65 # Create a link that has the relative path to the source file
66 # That is, $1 converted relative to $2
67 # Check if $1 is absolute or already relative
68 #echo add_rel_link.sh $1 $2
69 if expr "$1" : '\/' >/dev
/null
; then
70 # The source path is absolute, this is the expected case
71 # Check if $2 is absolute or relative
72 if expr "$2" : '\/' >/dev
/null
; then
73 # The link path is already absolute, so just use it
76 # The link path is relative, this is the expected case
77 # WARNING: don't use $PWD here, it won't work right
78 # WARNING: pwd may be a shell alias. Use /bin/pwd.
80 lpath
=`abspath $2 $cur`
82 # take name off the end of the dest
83 ldir
=`echo $lpath | sed 's@/[^/]*$@@'`
85 # If the original path and the ldir do not originate in the same
86 # directory tree, we should just use absolute paths
87 if [ "`echo $1 | cut -d/ -f2`" != "`echo $ldir | cut -d/ -f2`" ]; then
90 spath
=`relpath $1 $ldir`
92 # use the completed relative path and the given designation path
96 # The source path is already relative, so just use it