summaryrefslogtreecommitdiffstats
path: root/utils/update-alternatives.in
blob: 2a6f6cc7fae03d620f8c7e9c31e0a7d2106d57ca (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/sh
# update-alternatives
#
# Copyright (C) 2001 Carl D. Worth
#
# This program was inspired by the Debian update-alternatives program
# which is Copyright (C) 1995 Ian Jackson. This version of
# update-alternatives is command-line compatible with Debian's for a
# subset of the options, (only --install, --remove, and --help)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

set -e

# admin dir
ad="$OPKG_OFFLINE_ROOT@opkglibdir@/opkg/alternatives"

usage() {
	echo "update-alternatives: $*

Usage: update-alternatives --install <link> <name> <path> <priority>
       update-alternatives --remove <name> <path>
       update-alternatives --help
<link> is the link pointing to the provided path (ie. /usr/bin/foo).
<name> is the name in $ad/alternatives (ie. foo)
<path> is the name referred to (ie. /usr/bin/foo-extra-spiffy)
<priority> is an integer; options with higher numbers are chosen.
" >&2
	exit 2
}

quit() {
	echo "update-alternatives: $*" >&2
	exit 2
}

register_alt() {
	[ $# -lt 2 ] && return 1
	local name="$1"
	local link="$2"

	if [ ! -d $ad ]; then
		mkdir -p $ad
	fi

	if [ -e "$ad/$name" ]; then
		local olink=`head -n 1 $ad/$name`
		if [ "$link" != "$olink" ]; then
			echo "update-alternatives: Error: cannot register alternative $name to $link since it is already registered to $olink" >&2
			return 1
		fi
	else
		echo "$link" > "$ad/$name"
	fi

	return 0
}

protect_slashes() {
	sed -e 's/\//\\\//g'
}

remove_alt() {
	[ $# -lt 2 ] && return 1
	local name="$1"
	local path="$2"
	
	[ ! -f $ad/$name ] && return 0

	path=`echo $path | protect_slashes`
	sed -ne "/^$path\>.*/!p" $ad/$name > $ad/$name.new
	mv $ad/$name.new $ad/$name
}

add_alt() {
	[ $# -lt 3 ] && return 1
	local name="$1"
	local path="$2"
	local priority="$3"
	remove_alt $name $path
	echo "$path $priority" >> $ad/$name
}

find_best_alt() {
	[ $# -lt 1 ] && return 1
	[ ! -f $ad/$name ] && return 0

	link=$OPKG_OFFLINE_ROOT/`head -n 1 $ad/$name`

	prio=`sed -ne "1!p" $ad/$name | sed -e "s/\(.*\) \(.*\)/\2 \1/g" | sort -nr | head -n 1 | sed 's/ [^ ]*$//'`
	if [ -z "$prio" ]; then
		echo "update-alternatives: removing $link as no more alternatives exist for it"
		rm $ad/$name
		if [ -L $link ]; then
			rm $link
		fi
		return 0
	fi

	## Find last line with highest priority.
	path=`grep "${prio}$" $ad/$name | tail -n 1 | sed 's/ [^ ]*$//'`

	if [ ! -e $link -o -L $link ]; then
		local link_dir=`dirname $link`
		if [ ! -d $link_dir ]; then
			mkdir -p $link_dir
		fi
		ln -snf $path $link
		echo "update-alternatives: Linking $link to $path"
	else
		echo "update-alternatives: Error: not linking $link to $path since $link exists and is not a link"
		return 1
	fi

	return 0
}

do_install() {
	if [ $# -lt 4 ]; then
		usage "--install needs <link> <name> <path> <priority>"
	fi
	local link="$1"
	local name="$2"
	local path="$3"
	local priority="$4"

	path=`echo $path | sed 's|/\+|/|g'`

	# This is a bad hack, but I haven't thought of a cleaner solution yet...
	[ -n "$OPKG_OFFLINE_ROOT" ] && path=`echo $path | sed "s|^$OPKG_OFFLINE_ROOT/*|/|"`

	register_alt $name $link
	add_alt $name $path $priority
	find_best_alt $name
}

do_remove() {
       if [ $# -lt 2 ]; then
		usage "--remove needs <name> <path>"
	fi	
	local name="$1"
	local path="$2"

	path=`echo $path | sed 's|/\+|/|g'`

	# This is a bad hack, but I haven't thought of a cleaner solution yet...
	[ -n "$OPKG_OFFLINE_ROOT" ] && path=`echo $path | sed "s|^$OPKG_OFFLINE_ROOT/*|/|"`

	remove_alt $name $path
	find_best_alt $name
}

###
# update-alternatives "main"
###

while [ $# -gt 0 ]; do
	arg="$1"
	shift

	case $arg in
	--help)
		usage "help:"
		exit 0
		;;
	--install)
		do_install $*
		exit $?
		;;
	--remove)
		do_remove $*
		exit $?
		;;
	*)
		usage "unknown argument \`$arg'"
		;;
	esac
done

usage "at least one of --install or --remove must appear"

exit 0