aboutsummaryrefslogtreecommitdiff
path: root/bin/makelinks
blob: 62474536aaf29b8b4b7d7d7065135f04e1eae1b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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