Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / bin / add_rel_link.sh
blobe206268eede90b7a5c2df1f3f2dfca20d66f4b6f
1 #!/bin/sh
2 # This script assumes it is being run by bash
4 # ======= needed functions ======
5 # abspath relpath curentpath
6 abspath ()
8 # treat "./" as a special case
9 if expr "$1" : '\./' >/dev/null; then
10 b=`echo $1 | sed 's@\./@@'`
11 echo $2/$b
12 return 0
14 b=$1
15 a=$2
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@/[^/]*$@@'`
23 done
24 # return the completed absolute path
25 echo $a/$b
28 # relpath abspath curentpath
29 relpath ()
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@$@/@'`
35 while true
37 if [ "$b" = "" ]; then
38 break;
40 a1=`echo $a | sed 's@\([^/]*\)/.*@\1@'`
41 b1=`echo $b | sed 's@\([^/]*\)/.*@\1@'`
42 if [ "$a1" != "$b1" ]; then
43 break;
45 a=`echo $a | sed 's@[^/]*/@@'`
46 b=`echo $b | sed 's@[^/]*/@@'`
47 done
48 # a now has the unique part of the path
49 c=""
50 # c will have the required number of "../"'s
51 while [ "$b" != "" ]
53 c="../$c"
54 b=`echo $b | sed 's@[^/]*/@@'`
55 done
56 # return the completed relative path
57 echo "$c$a"
61 # ====== MAIN ======
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
74 lpath=$2
75 else
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.
79 cur=`/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
88 spath=$1
89 else
90 spath=`relpath $1 $ldir`
92 # use the completed relative path and the given designation path
93 echo ln -s $spath $2
94 ln -s $spath $2
95 else
96 # The source path is already relative, so just use it
97 echo ln -s $1 $2
98 ln -s $1 $2