4 # Copyright (C) 2001 Carl D. Worth
6 # This program was inspired by the Debian update-alternatives program
7 # which is Copyright (C) 1995 Ian Jackson. This version of
8 # update-alternatives is command-line compatible with Debian's for a
9 # subset of the options, (only --install, --remove, and --help)
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
24 ad
="$OPKG_OFFLINE_ROOT@opkglibdir@/opkg/alternatives"
27 echo "update-alternatives: $*
29 Usage: update-alternatives --install <link> <name> <path> <priority>
30 update-alternatives --remove <name> <path>
31 update-alternatives --help
32 <link> is the link pointing to the provided path (ie. /usr/bin/foo).
33 <name> is the name in $ad/alternatives (ie. foo)
34 <path> is the name referred to (ie. /usr/bin/foo-extra-spiffy)
35 <priority> is an integer; options with higher numbers are chosen.
41 echo "update-alternatives: $*" >&2
46 [ $# -lt 2 ] && return 1
54 if [ -e "$ad/$name" ]; then
55 local olink
=`head -n 1 $ad/$name`
56 if [ "$link" != "$olink" ]; then
57 echo "update-alternatives: Error: cannot register alternative $name to $link since it is already registered to $olink" >&2
61 echo "$link" > "$ad/$name"
72 [ $# -lt 2 ] && return 1
76 [ ! -f $ad/$name ] && return 0
78 path
=`echo $path | protect_slashes`
79 sed -ne "/^$path\>.*/!p" $ad/$name > $ad/$name.new
80 mv $ad/$name.new
$ad/$name
84 [ $# -lt 3 ] && return 1
88 remove_alt
$name $path
89 echo "$path $priority" >> $ad/$name
93 [ $# -lt 1 ] && return 1
94 [ ! -f $ad/$name ] && return 0
96 link
=$OPKG_OFFLINE_ROOT/`head -n 1 $ad/$name`
98 prio
=`sed -ne "1!p" $ad/$name | sed -e "s/\(.*\) \(.*\)/\2 \1/g" | sort -nr | head -n 1 | sed 's/ [^ ]*$//'`
99 if [ -z "$prio" ]; then
100 echo "update-alternatives: removing $link as no more alternatives exist for it"
102 if [ -L $link ]; then
108 ## Find last line with highest priority.
109 path
=`grep "${prio}$" $ad/$name | tail -n 1 | sed 's/ [^ ]*$//'`
111 if [ ! -e $link -o -L $link ]; then
112 local link_dir
=`dirname $link`
113 if [ ! -d $link_dir ]; then
116 if [ -h $link -a -d $link ]; then
117 # If $link exists and the target is a directory,
118 # 'ln -sf $path $link' doesn't replace the link to
119 # that directory, it creates new link inside.
120 echo "update-alternatives: Removing $link".
124 echo "update-alternatives: Linking $link to $path"
126 echo "update-alternatives: Error: not linking $link to $path since $link exists and is not a link"
134 if [ $# -lt 4 ]; then
135 usage
"--install needs <link> <name> <path> <priority>"
142 path
=`echo $path | sed 's|/\+|/|g'`
144 # This is a bad hack, but I haven't thought of a cleaner solution yet...
145 [ -n "$OPKG_OFFLINE_ROOT" ] && path
=`echo $path | sed "s|^$OPKG_OFFLINE_ROOT/*|/|"`
147 register_alt
$name $link
148 add_alt
$name $path $priority
153 if [ $# -lt 2 ]; then
154 usage
"--remove needs <name> <path>"
159 path
=`echo $path | sed 's|/\+|/|g'`
161 # This is a bad hack, but I haven't thought of a cleaner solution yet...
162 [ -n "$OPKG_OFFLINE_ROOT" ] && path
=`echo $path | sed "s|^$OPKG_OFFLINE_ROOT/*|/|"`
164 remove_alt
$name $path
169 # update-alternatives "main"
172 while [ $# -gt 0 ]; do
190 usage
"unknown argument \`$arg'"
195 usage
"at least one of --install or --remove must appear"