| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# Author: Carlos Santos <[email protected]> |
| 4 |
# This file is in public domain. |
| 5 |
# |
| 6 |
|
| 7 |
error() { |
| 8 |
echo "$@" 1>&2 |
| 9 |
exit 1 |
| 10 |
} |
| 11 |
|
| 12 |
src="$1" |
| 13 |
dst="$2" |
| 14 |
|
| 15 |
check_path() { |
| 16 |
case "$2" in |
| 17 |
*/../*|*/./*|*/.|*/..) error "$1 path '$2' must be absolute";; |
| 18 |
*/) error "$1 path '$2' must not end with '/'";; |
| 19 |
/?*) ;; |
| 20 |
*) error "$1 path '$2' must start with '/'";; |
| 21 |
esac |
| 22 |
} |
| 23 |
|
| 24 |
check_path "source" "$src" |
| 25 |
check_path "destination" "$dst" |
| 26 |
|
| 27 |
# strip leading '/' |
| 28 |
src=${src#/*} |
| 29 |
tmp=${dst#/*} |
| 30 |
|
| 31 |
s_prefix=${src%%/*} |
| 32 |
d_prefix=${tmp%%/*} |
| 33 |
|
| 34 |
# strip leading common |
| 35 |
while [ "$s_prefix" = "$d_prefix" ]; do |
| 36 |
src="${src#$s_prefix/}" |
| 37 |
tmp="${tmp#$d_prefix/}" |
| 38 |
s_prefix=${src%%/*} |
| 39 |
d_prefix=${tmp%%/*} |
| 40 |
done |
| 41 |
|
| 42 |
s_prefix="../" |
| 43 |
while [ -n "$d_prefix" ] && [ "$tmp" != "$d_prefix" ]; do |
| 44 |
s_prefix="../$s_prefix" |
| 45 |
tmp="${tmp#$d_prefix/}" |
| 46 |
d_prefix=${tmp%%/*} |
| 47 |
done |
| 48 |
|
| 49 |
ln -s -f "$s_prefix$src" "$dst" |