summaryrefslogtreecommitdiffstats
path: root/util/install-symlink.in
blob: 24341b8b44adde13d99d104e8b5908b24c819f6f (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh
#
# install-symlink source destination destdir
#

SYMLINKS=symlinks
LN_S="@LN_S@"
RM="@RM@"
FORCE_RELATIVE=NO
FORCE_ABSOLUTE=NO

while echo $1 | grep -q -- ^- ;
do
	case $1 in
		--relative)
			FORCE_RELATIVE=YES
			;;
		--absolute)
			FORCE_ABSOLUTE=YES
			;;
		--debian)
			FORCE_ABSOLUTE=NO
			FORCE_RELATIVE=NO
			;;
		--symlinks=*)
			SYMLINKS=$(echo $1 | sed -e 's/--symlinks=//')
			;;
		*)
			echo "Unknown option $1"
			exit 1
			;;
	esac					     
	shift;
done


FIX_SYMLINK="$SYMLINKS -c"

SRC="$1"
DEST="$2"
DESTDIR="$3"

if ! echo $SRC | grep -q ^/ ; then
	echo $SRC: Source pathname must be absolute
	exit 1
fi

if ! echo $DEST | grep -q ^/ ; then
	echo $DEST: Destination pathname must be absolute
	exit 1
fi

if ! test -e "$DESTDIR$SRC" ; then
	echo $DESTDIR$SRC: file or directory does not exist
	exit 1
fi

$RM -f "$DESTDIR$DEST"

if test "$LN_S" != "ln -s" ; then
	$LN_S "$DESTDIR$SRC" "$DESTDIR$DEST"
	exit 0
fi

if test $(dirname "$SRC") = $(dirname "$DEST") ; then
	$LN_S "$(basename "$SRC")" "$DESTDIR$DEST"
	exit 0
fi

TOP_SRC=$(echo $SRC | awk -F/ '{print $2}')
TOP_DEST=$(echo $DEST | awk -F/ '{print $2}')

if test $FORCE_RELATIVE = YES ; then
	TOP_SRC=FORCE
	TOP_DEST=FORCE
fi

if test $FORCE_ABSOLUTE = YES ; then
	TOP_SRC=FORCE
	TOP_DEST=FORCE_ABSOLUTE
fi

if test $TOP_SRC != $TOP_DEST ; then
	$LN_S "$SRC" "$DESTDIR$DEST"
else
	$LN_S "$DESTDIR$SRC" "$DESTDIR$DEST"
	$FIX_SYMLINK "$DESTDIR$DEST"
fi