diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2017-05-22 21:56:04 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2017-05-22 21:56:04 +0000 |
commit | 82d0f92ab8b84466ae20ab919c1f9c3577b5cecb (patch) | |
tree | 776015ae6c6a386125b2d39e13b62f91b99548f8 /bin/makelinks | |
download | lh-bootstrap-82d0f92ab8b84466ae20ab919c1f9c3577b5cecb.tar.xz |
Initial commit
Diffstat (limited to 'bin/makelinks')
-rwxr-xr-x | bin/makelinks | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/bin/makelinks b/bin/makelinks new file mode 100755 index 0000000..6247453 --- /dev/null +++ b/bin/makelinks @@ -0,0 +1,49 @@ +#!/bin/sh -e +# +# +# "makelinks base linkdir realdir" does a "relative ln", relatively to "base". "linkdir" and "realdir" are relative path to subdirectories under "base". Symlinks are created into linkdir for files in realdir. The symlinks do not contain "base". +# +# + +test "$#" -ge 3 || { echo "makelinks: too few arguments" 1>&2 ; exit 100 ; } + +computerelative() { + source="$1" + target="$2" + + common="$source" + result="" + + while test "${target#$common}" = "$target" ; do + common="$(dirname $common)" + if test -z "$result" ; then + result=".." + else + result="../$result" + fi + done + + if test "$common" = "/" ; then + result="$result/" + fi + + forward="${target#$common}" + + if test -n "$result" -a -n "$forward" ; then + result="$result$forward" + elif test -n "$forward" ; then + result="${forward:1}" + fi + + echo "$result" +} + +base=${1%%/} +linkdir=${2%%/} +realdir=${3%%/} + +targetdir=$(computerelative "$base$linkdir" "$base$realdir") + +for i in $(ls -1 "$base$realdir") ; do + ln -sf "$targetdir/$i" "$base$linkdir/$i" || true +done |