ipkg: remove auto-generated files
[oweals/opkg-lede.git] / update-alternatives
1 #!/bin/sh
2 # update-alternatives
3 #
4 # Copyright (C) 2001 Carl D. Worth
5 #
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)
10 #
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)
14 # any later version.
15 #
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.
20
21 set -e
22
23 # admin dir
24 ad="$IPKG_OFFLINE_ROOT/usr/lib/ipkg/alternatives"
25
26 usage() {
27         echo "update-alternatives: $*
28
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.
36 " >&2
37         exit 2
38 }
39
40 quit() {
41         echo "update-alternatives: $*" >&2
42         exit 2
43 }
44
45 register_alt() {
46         [ $# -lt 2 ] && return 1
47         local name="$1"
48         local link="$2"
49
50         if [ ! -d $ad ]; then
51                 mkdir -p $ad
52         fi
53
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
58                         return 1
59                 fi
60         else
61                 echo "$link" > "$ad/$name"
62         fi
63
64         return 0
65 }
66
67 protect_slashes() {
68         sed -e 's/\//\\\//g'
69 }
70
71 remove_alt() {
72         [ $# -lt 2 ] && return 1
73         local name="$1"
74         local path="$2"
75         
76         [ ! -f $ad/$name ] && return 0
77
78         path=`echo $path | protect_slashes`
79         sed -ne "/^$path\>.*/!p" $ad/$name > $ad/$name.new
80         mv $ad/$name.new $ad/$name
81 }
82
83 add_alt() {
84         [ $# -lt 3 ] && return 1
85         local name="$1"
86         local path="$2"
87         local priority="$3"
88         remove_alt $name $path
89         echo "$path $priority" >> $ad/$name
90 }
91
92 find_best_alt() {
93         [ $# -lt 1 ] && return 1
94         [ ! -f $ad/$name ] && return 0
95
96         link=$IPKG_OFFLINE_ROOT/`head -n 1 $ad/$name`
97
98 ##      path=`sed -ne "1!p" $ad/$name | sort -nr -k2 | head -1 | sed 's/ .*//'`
99 ## busybox safe:
100         path=`sed -ne "1!p" $ad/$name | sed -e "s/\(.*\) \(.*\)/\2 \1/g" | sort -nr | head -n 1 | sed 's/[^ ]* //'`
101         if [ -z "$path" ]; then
102                 echo "update-alternatives: removing $link as no more alternatives exist for it"
103                 rm $ad/$name
104                 if [ -L $link ]; then
105                         rm $link
106                 fi
107                 return 0
108         fi
109
110         if [ ! -e $link -o -L $link ]; then
111                 local link_dir=`dirname $link`
112                 if [ ! -d $link_dir ]; then
113                         mkdir -p $link_dir
114                 fi
115                 ln -sf $path $link
116                 echo "update-alternatives: Linking $link to $path"
117         else
118                 echo "update-alternatives: Error: not linking $link to $path since $link exists and is not a link"
119                 return 1
120         fi
121
122         return 0
123 }
124
125 do_install() {
126         if [ $# -lt 4 ]; then
127                 usage "--install needs <link> <name> <path> <priority>"
128         fi
129         local link="$1"
130         local name="$2"
131         local path="$3"
132         local priority="$4"
133
134         path=`echo $path | sed 's|/\+|/|g'`
135
136         # This is a bad hack, but I haven't thought of a cleaner solution yet...
137         [ -n "$IPKG_OFFLINE_ROOT" ] && path=`echo $path | sed "s|^$IPKG_OFFLINE_ROOT/*|/|"`
138
139         register_alt $name $link
140         add_alt $name $path $priority
141         find_best_alt $name
142 }
143
144 do_remove() {
145        if [ $# -lt 2 ]; then
146                 usage "--remove needs <name> <path>"
147         fi      
148         local name="$1"
149         local path="$2"
150
151         path=`echo $path | sed 's|/\+|/|g'`
152
153         # This is a bad hack, but I haven't thought of a cleaner solution yet...
154         [ -n "$IPKG_OFFLINE_ROOT" ] && path=`echo $path | sed "s|^$IPKG_OFFLINE_ROOT/*|/|"`
155
156         remove_alt $name $path
157         find_best_alt $name
158 }
159
160 ###
161 # update-alternatives "main"
162 ###
163
164 while [ $# -gt 0 ]; do
165         arg="$1"
166         shift
167
168         case $arg in
169         --help)
170                 usage "help:"
171                 exit 0
172                 ;;
173         --install)
174                 do_install $*
175                 exit $?
176                 ;;
177         --remove)
178                 do_remove $*
179                 exit $?
180                 ;;
181         *)
182                 usage "unknown argument \`$arg'"
183                 ;;
184         esac
185 done
186
187 usage "at least one of --install or --remove must appear"
188
189 exit 0