1#!/bin/sh
2#
3# install-symlink source destination destdir
4#
5
6SYMLINKS=symlinks
7LN_S="@LN_S@"
8RM="@RM@"
9FORCE_RELATIVE=NO
10FORCE_ABSOLUTE=NO
11
12while echo $1 | grep -q -- ^- ;
13do
14	case $1 in
15		--relative)
16			FORCE_RELATIVE=YES
17			;;
18		--absolute)
19			FORCE_ABSOLUTE=YES
20			;;
21		--debian)
22			FORCE_ABSOLUTE=NO
23			FORCE_RELATIVE=NO
24			;;
25		--symlinks=*)
26			SYMLINKS=$(echo $1 | sed -e 's/--symlinks=//')
27			;;
28		*)
29			echo "Unknown option $1"
30			exit 1
31			;;
32	esac
33	shift;
34done
35
36
37FIX_SYMLINK="$SYMLINKS -c"
38
39SRC="$1"
40DEST="$2"
41DESTDIR="$3"
42
43if ! echo $SRC | grep -q ^/ ; then
44	echo $SRC: Source pathname must be absolute
45	exit 1
46fi
47
48if ! echo $DEST | grep -q ^/ ; then
49	echo $DEST: Destination pathname must be absolute
50	exit 1
51fi
52
53if ! test -e "$DESTDIR$SRC" ; then
54	echo $DESTDIR$SRC: file or directory does not exist
55	exit 1
56fi
57
58$RM -f "$DESTDIR$DEST"
59
60if test "$LN_S" != "ln -s" ; then
61	$LN_S "$DESTDIR$SRC" "$DESTDIR$DEST"
62	exit 0
63fi
64
65if test $(dirname "$SRC") = $(dirname "$DEST") ; then
66	$LN_S "$(basename "$SRC")" "$DESTDIR$DEST"
67	exit 0
68fi
69
70TOP_SRC=$(echo $SRC | awk -F/ '{print $2}')
71TOP_DEST=$(echo $DEST | awk -F/ '{print $2}')
72
73if test $FORCE_RELATIVE = YES ; then
74	TOP_SRC=FORCE
75	TOP_DEST=FORCE
76fi
77
78if test $FORCE_ABSOLUTE = YES ; then
79	TOP_SRC=FORCE
80	TOP_DEST=FORCE_ABSOLUTE
81fi
82
83if test $TOP_SRC != $TOP_DEST ; then
84	$LN_S "$SRC" "$DESTDIR$DEST"
85else
86	$LN_S "$DESTDIR$SRC" "$DESTDIR$DEST"
87	$FIX_SYMLINK "$DESTDIR$DEST"
88fi
89
90