• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# Author: Carlos Santos <unixmania@gmail.com>
4# This file is in public domain.
5#
6
7error() {
8	echo "$@" 1>&2
9	exit 1
10}
11
12src="$1"
13dst="$2"
14
15check_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
24check_path "source" "$src"
25check_path "destination" "$dst"
26
27# strip leading '/'
28src=${src#/*}
29tmp=${dst#/*}
30
31s_prefix=${src%%/*}
32d_prefix=${tmp%%/*}
33
34# strip leading common
35while [ "$s_prefix" = "$d_prefix" ]; do
36	src="${src#$s_prefix/}"
37	tmp="${tmp#$d_prefix/}"
38	s_prefix=${src%%/*}
39	d_prefix=${tmp%%/*}
40done
41
42s_prefix="../"
43while [ -n "$d_prefix" ] && [ "$tmp" != "$d_prefix" ]; do
44	s_prefix="../$s_prefix"
45	tmp="${tmp#$d_prefix/}"
46	d_prefix=${tmp%%/*}
47done
48
49ln -s -f "$s_prefix$src" "$dst"
50