fix svn patch breakage in glib
[openwrt.git] / root / bin / ipkg
1 #!/bin/sh
2 # ipkg - the itsy package management system
3 #
4 # Copyright (C) 2001 Carl D. Worth
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 set -e
17
18 # By default do not do globbing. Any command wanting globbing should
19 # explicitly enable it first and disable it afterwards.
20 set -o noglob
21
22 ipkg_is_upgrade () {
23   local A B a b
24   A=$(echo $1 | sed "s/[0-9]*/ & /g")
25   B=$(echo $2 | sed "s/[0-9]*/ & /g")
26   while [ \! -z "$A" ] && [ \! -z "$B" ]; do {
27     set $A; a=$1; shift; A=$*
28     set $B; b=$1; shift; B=$*
29     { [ "$a" -gt "$b" ] 2>&- || [ "$a" ">" "$b" ]; } && { return 0; }
30   }; done
31   return 1;
32 }
33
34 ipkg_srcs() {
35         local srcre="$1"
36         sed -ne "s/^src[[:space:]]\+$srcre[[:space:]]\+//p" < $IPKG_CONF
37 }
38
39 ipkg_src_names() {
40         sed -ne "s/^src[[:space:]]\+\([^[:space:]]\+\).*/\1/p" < $IPKG_CONF
41 }
42
43 ipkg_src_byname() {
44         local src="$1"
45         ipkg_srcs $src | head -1
46 }
47
48 ipkg_dests() {
49         local destre="`echo $1 | ipkg_protect_slashes`"
50         sed -ne "/^dest[[:space:]]\+$destre/{
51 s/^dest[[:space:]]\+[^[:space:]]\+[[:space:]]\+//
52 s/^/`echo $IPKG_OFFLINE_ROOT | ipkg_protect_slashes`/
53 p
54 }" < $IPKG_CONF
55 }
56
57 ipkg_dest_names() {
58         sed -ne "s/^dest[[:space:]]\+\([^[:space:]]\+\).*/\1/p" < $IPKG_CONF
59 }
60
61 ipkg_dests_all() {
62         ipkg_dests '.*'
63 }
64
65 ipkg_state_dirs() {
66         ipkg_dests_all | sed "s|\$|/$IPKG_DIR_PREFIX|"
67 }
68
69 ipkg_dest_default() {
70         ipkg_dests_all | head -1
71 }
72
73 ipkg_dest_default_name() {
74         ipkg_dest_names | head -1
75 }
76
77 ipkg_dest_byname() {
78         local dest="$1"
79         ipkg_dests $dest | head -1
80 }
81
82 ipkg_option() {
83         local option="$1"
84         sed -ne "s/^option[[:space:]]\+$option[[:space:]]\+//p" < $IPKG_CONF
85 }
86
87 ipkg_load_configuration() {
88         if [ -z "$IPKG_CONF_DIR" ]; then
89                 IPKG_CONF_DIR=/etc
90         fi
91
92         IPKG_CONF="$IPKG_CONF_DIR/ipkg.conf"
93
94         if [ -z "$IPKG_OFFLINE_ROOT" ]; then
95             IPKG_OFFLINE_ROOT="`ipkg_option offline_root`"
96         fi
97         # Export IPKG_OFFLINE_ROOT for use by update-alternatives
98         export IPKG_OFFLINE_ROOT
99         if [ -n "$DEST_NAME" ]; then
100                 IPKG_ROOT="`ipkg_dest_byname $DEST_NAME`"
101                 if [ -z "$IPKG_ROOT" ]; then
102                         if [ -d "$IPKG_OFFLINE_ROOT$DEST_NAME" ]; then
103                                 IPKG_ROOT="$IPKG_OFFLINE_ROOT$DEST_NAME";
104                         else
105                                 echo "ipkg: invalid destination specification: $DEST_NAME
106 Valid destinations are directories or one of the dest names from $IPKG_CONF:" >&2
107                                 ipkg_dest_names >&2
108                                 return 1
109                         fi
110                 fi
111         else
112                 IPKG_ROOT="`ipkg_dest_default`"
113         fi
114
115         # Global ipkg state directories
116         IPKG_DIR_PREFIX=usr/lib/ipkg
117         IPKG_LISTS_DIR=$IPKG_OFFLINE_ROOT/$IPKG_DIR_PREFIX/lists
118         IPKG_PENDING_DIR=$IPKG_OFFLINE_ROOT/$IPKG_DIR_PREFIX/pending
119         IPKG_TMP=$IPKG_ROOT/tmp/ipkg
120
121         # Destination specific ipkg meta-data directory
122         IPKG_STATE_DIR=$IPKG_ROOT/$IPKG_DIR_PREFIX
123
124         # Proxy Support
125         IPKG_PROXY_USERNAME="`ipkg_option proxy_username`"
126         IPKG_PROXY_PASSWORD="`ipkg_option proxy_password`"
127         IPKG_HTTP_PROXY="`ipkg_option http_proxy`"
128         IPKG_FTP_PROXY="`ipkg_option ftp_proxy`"
129         IPKG_NO_PROXY="`ipkg_option no_proxy`"
130         if [ -n "$IPKG_HTTP_PROXY" ]; then 
131                 export http_proxy="$IPKG_HTTP_PROXY"
132         fi
133
134         if [ -n "$IPKG_FTP_PROXY" ]; then 
135                 export ftp_proxy="$IPKG_FTP_PROXY"
136         fi
137
138         if [ -n "$IPKG_NO_PROXY" ]; then 
139                 export no_proxy="$IPKG_NO_PROXY"
140         fi
141
142         IPKG_STATUS_FIELDS='\(Package\|Status\|Essential\|Version\|Conffiles\|Root\)'
143 }
144
145 ipkg_usage() {
146         [ $# -gt 0 ] && echo "ipkg: $*"
147         echo "
148 usage: ipkg [options...] sub-command [arguments...]
149 where sub-command is one of:
150
151 Package Manipulation:
152         update                  Update list of available packages
153         upgrade                 Upgrade all installed packages to latest version
154         install <pkg>           Download and install <pkg> (and dependencies)
155         install <file.ipk>      Install package <file.ipk>
156         install <file.deb>      Install package <file.deb>
157         remove <pkg>            Remove package <pkg>
158
159 Informational Commands:
160         list                    List available packages and descriptions
161         files <pkg>             List all files belonging to <pkg>
162         search <file>           Search for a packaging providing <file>
163         info [pkg [<field>]]    Display all/some info fields for <pkg> or all
164         status [pkg [<field>]]  Display all/some status fields for <pkg> or all
165         depends <pkg>           Print uninstalled package dependencies for <pkg>
166
167 Options:
168         -d <dest_name>          Use <dest_name> as the the root directory for
169         -dest <dest_name>       package installation, removal, upgrading.
170                                 <dest_name> should be a defined dest name from the
171                                 configuration file, (but can also be a directory
172                                 name in a pinch).
173         -o <offline_root>       Use <offline_root> as the root for offline installation.
174         -offline <offline_root>                                 
175
176 Force Options (use when ipkg is too smart for its own good):
177         -force-depends          Make dependency checks warnings instead of errors
178         -force-defaults         Use default options for questions asked by ipkg.
179                                 (no prompts). Note that this will not prevent
180                                 package installation scripts from prompting.
181 " >&2
182         exit 1
183 }
184
185 ipkg_dir_part() {
186         local dir="`echo $1 | sed -ne 's/\(.*\/\).*/\1/p'`"
187         if [ -z "$dir" ]; then
188                 dir="./"
189         fi
190         echo $dir
191 }
192
193 ipkg_file_part() {
194         echo $1 | sed 's/.*\///'
195 }
196
197 ipkg_protect_slashes() {
198         sed -e 's/\//\\\//g'
199 }
200
201 ipkg_download() {
202         local src="$1"
203         local dest="$2"
204
205         local src_file="`ipkg_file_part $src`"
206         local dest_dir="`ipkg_dir_part $dest`"
207         if [ -z "$dest_dir" ]; then
208                 dest_dir="$IPKG_TMP"
209         fi
210
211         local dest_file="`ipkg_file_part $dest`"
212         if [ -z "$dest_file" ]; then
213                 dest_file="$src_file"
214         fi
215
216         # Proxy support
217         local proxyuser=""
218         local proxypassword=""
219         local proxyoption=""
220                 
221         if [ -n "$IPKG_PROXY_USERNAME" ]; then
222                 proxyuser="--proxy-user=\"$IPKG_PROXY_USERNAME\""
223                 proxypassword="--proxy-passwd=\"$IPKG_PROXY_PASSWORD\""
224         fi
225
226         if [ -n "$IPKG_PROXY_HTTP" -o -n "$IPKG_PROXY_FTP" ]; then
227                 proxyoption="--proxy=on"
228         fi
229
230         echo "Downloading $src ..."
231         rm -f $IPKG_TMP/$src_file
232         case "$src" in
233         http://* | ftp://*)
234                 if ! wget --passive-ftp $proxyoption $proxyuser $proxypassword -P $IPKG_TMP $src; then
235                         echo "ipkg_download: ERROR: Failed to retrieve $src, returning $err"
236                         return 1
237                 fi
238                 mv $IPKG_TMP/$src_file $dest_dir/$dest_file 2>/dev/null
239                 ;;
240         file:/* )
241                 ln -s `echo $src | sed 's/^file://'` $dest_dir/$dest_file 2>/dev/null
242                 ;;
243         *)
244         echo "DEBUG: $src"
245                 ;;
246         esac
247
248         echo "Done."
249         return 0
250 }
251
252 ipkg_update() {
253         if [ ! -e "$IPKG_LISTS_DIR" ]; then
254                 mkdir -p $IPKG_LISTS_DIR
255         fi
256
257         local err=
258         for src_name in `ipkg_src_names`; do
259                 local src="`ipkg_src_byname $src_name`"
260                 if ! ipkg_download $src/Packages $IPKG_LISTS_DIR/$src_name; then
261                         echo "ipkg_update: Error downloading $src/Packages to $IPKG_LISTS_DIR/$src_name" >&2
262                         err=t
263                 else
264                         echo "Updated list of available packages in $IPKG_LISTS_DIR/$src_name"
265                 fi
266         done
267
268         [ -n "$err" ] && return 1
269
270         return 0
271 }
272
273 ipkg_list() {
274         for src in `ipkg_src_names`; do
275                 if ipkg_require_list $src; then 
276 # black magic...
277 sed -ne "
278 /^Package:/{
279 s/^Package:[[:space:]]*\<\([a-z0-9.+-]*$1[a-z0-9.+-]*\).*/\1/
280 h
281 }
282 /^Description:/{
283 s/^Description:[[:space:]]*\(.*\)/\1/
284 H
285 g
286 s/\\
287 / - /
288 p
289 }
290 " $IPKG_LISTS_DIR/$src
291                 fi
292         done
293 }
294
295 ipkg_extract_paragraph() {
296         local pkg="$1"
297         sed -ne "/Package:[[:space:]]*$pkg[[:space:]]*\$/,/^\$/p"
298 }
299
300 ipkg_extract_field() {
301         local field="$1"
302 # blacker magic...
303         sed -ne "
304 : TOP
305 /^$field:/{
306 p
307 n
308 b FIELD
309 }
310 d
311 : FIELD
312 /^$/b TOP
313 /^[^[:space:]]/b TOP
314 p
315 n
316 b FIELD
317 "
318 }
319
320 ipkg_extract_value() {
321         sed -e "s/^[^:]*:[[:space:]]*//"
322 }
323
324 ipkg_require_list() {
325         [ $# -lt 1 ] && return 1
326         local src="$1"
327         if [ ! -f "$IPKG_LISTS_DIR/$src" ]; then
328                 echo "ERROR: File not found: $IPKG_LISTS_DIR/$src" >&2
329                 echo "       You probably want to run \`ipkg update'" >&2
330                 return 1
331         fi
332         return 0
333 }
334
335 ipkg_info() {
336         for src in `ipkg_src_names`; do
337                 if ipkg_require_list $src; then
338                         case $# in
339                         0)
340                                 cat $IPKG_LISTS_DIR/$src
341                                 ;;      
342                         1)
343                                 ipkg_extract_paragraph $1 < $IPKG_LISTS_DIR/$src
344                                 ;;
345                         *)
346                                 ipkg_extract_paragraph $1 < $IPKG_LISTS_DIR/$src | ipkg_extract_field $2
347                                 ;;
348                         esac
349                 fi
350         done
351 }
352
353 ipkg_status_sd() {
354         [ $# -lt 1 ] && return 0
355         sd="$1"
356         shift
357         if [ -f $sd/status ]; then
358                 case $# in
359                 0)
360                         cat $sd/status
361                         ;;
362                 1)
363                         ipkg_extract_paragraph $1 < $sd/status
364                         ;;
365                 *)
366                         ipkg_extract_paragraph $1 < $sd/status | ipkg_extract_field $2
367                         ;;
368                 esac
369         fi
370         return 0
371 }
372
373 ipkg_status_all() {
374         for sd in `ipkg_state_dirs`; do
375                 ipkg_status_sd $sd $*
376         done
377 }
378
379 ipkg_status() {
380         if [ -n "$DEST_NAME" ]; then
381                 ipkg_status_sd $IPKG_STATE_DIR $*
382         else
383                 ipkg_status_all $*
384         fi
385 }
386
387 ipkg_status_matching_sd() {
388         local sd="$1"
389         local re="$2"
390         if [ -f $sd/status ]; then
391                 sed -ne "
392 : TOP
393 /^Package:/{
394 s/^Package:[[:space:]]*//
395 s/[[:space:]]*$//
396 h
397 }
398 /$re/{
399 g
400 p
401 b NEXT
402 }
403 d
404 : NEXT
405 /^$/b TOP
406 n
407 b NEXT
408 " < $sd/status
409         fi
410         return 0
411 }
412
413 ipkg_status_matching_all() {
414         for sd in `ipkg_state_dirs`; do
415                 ipkg_status_matching_sd $sd $*
416         done
417 }
418
419 ipkg_status_matching() {
420         if [ -n "$DEST_NAME" ]; then
421                 ipkg_status_matching_sd $IPKG_STATE_DIR $*
422         else
423                 ipkg_status_matching_all $*
424         fi
425 }
426
427 ipkg_status_installed_sd() {
428         local sd="$1"
429         local pkg="$2"
430         ipkg_status_sd $sd $pkg Status | grep -q "Status: install ok installed"
431 }
432
433 ipkg_status_installed_all() {
434         local ret=1
435         for sd in `ipkg_state_dirs`; do
436                 if `ipkg_status_installed_sd $sd $*`; then
437                         ret=0
438                 fi
439         done
440         return $ret
441 }
442
443 ipkg_status_mentioned_sd() {
444         local sd="$1"
445         local pkg="$2"
446         [ -n "`ipkg_status_sd $sd $pkg Status`" ]
447 }
448
449 ipkg_files() {
450         local pkg="$1"
451         if [ -n "$DEST_NAME" ]; then
452                 dests=$IPKG_ROOT
453         else
454                 dests="`ipkg_dests_all`"
455         fi
456         for dest in $dests; do
457                 if [ -f $dest/$IPKG_DIR_PREFIX/info/$pkg.list ]; then
458                         dest_sed="`echo $dest | ipkg_protect_slashes`"
459                         sed -e "s/^/$dest_sed/" < $dest/$IPKG_DIR_PREFIX/info/$pkg.list
460                 fi
461         done
462 }
463
464 ipkg_search() {
465         local pattern="$1"
466
467         for dest_name in `ipkg_dest_names`; do
468                 dest="`ipkg_dest_byname $dest_name`"
469                 dest_sed="`echo $dest | ipkg_protect_slashes`"
470
471                 set +o noglob
472                 local list_files="`ls -1 $dest/$IPKG_DIR_PREFIX/info/*.list 2>/dev/null`"
473                 set -o noglob
474                 for file in $list_files; do
475                         if sed "s/^/$dest_sed/" $file | grep -q $pattern; then
476                                 local pkg="`echo $file | sed "s/^.*\/\(.*\)\.list/\1/"`"
477                                 [ "$dest_name" != `ipkg_dest_default_name` ] && pkg="$pkg ($dest_name)"
478                                 sed "s/^/$dest_sed/" $file | grep $pattern | sed "s/^/$pkg: /"
479                         fi
480                 done
481         done
482 }
483
484 ipkg_status_remove_sd() {
485         local sd="$1"
486         local pkg="$2"
487
488         if [ ! -f $sd/status ]; then
489                 mkdir -p $sd
490                 touch $sd/status
491         fi
492         sed -ne "/Package:[[:space:]]*$pkg[[:space:]]*\$/,/^\$/!p" < $sd/status > $sd/status.new
493         mv $sd/status.new $sd/status
494 }
495
496 ipkg_status_remove_all() {
497         for sd in `ipkg_state_dirs`; do
498                 ipkg_status_remove_sd $sd $*
499         done
500 }
501
502 ipkg_status_remove() {
503         if [ -n "$DEST_NAME" ]; then
504                 ipkg_status_remove_sd $IPKG_STATE_DIR $*
505         else
506                 ipkg_status_remove_all $*
507         fi
508 }
509
510 ipkg_status_update_sd() {
511         local sd="$1"
512         local pkg="$2"
513
514         ipkg_status_remove_sd $sd $pkg
515         ipkg_extract_field "$IPKG_STATUS_FIELDS" >> $sd/status
516         echo "" >> $sd/status
517 }
518
519 ipkg_status_update() {
520         ipkg_status_update_sd $IPKG_STATE_DIR $*
521 }
522
523 ipkg_unsatisfied_dependences() {
524     local pkg=$1
525     local deps="`ipkg_get_depends $pkg`"
526     local remaining_deps=
527     for dep in $deps; do
528         local installed="`ipkg_get_installed $dep`"
529         if [ "$installed" != "installed" ] ; then
530             remaining_deps="$remaining_deps $dep"
531         fi
532     done
533     ## echo "ipkg_unsatisfied_dependences pkg=$pkg $remaining_deps" > /dev/console
534     echo $remaining_deps
535 }
536
537 ipkg_safe_pkg_name() {
538         local pkg=$1
539         local spkg="`echo pkg_$pkg | sed -e y/-+./___/`"
540         echo $spkg
541 }
542
543 ipkg_set_depends() {
544         local pkg=$1; shift 
545         local new_deps="$*"
546         pkg="`ipkg_safe_pkg_name $pkg`"
547         ## setvar ${pkg}_depends "$new_deps"
548         echo $new_deps > /tmp/ipkg/${pkg}.depends
549 }
550
551 ipkg_get_depends() {
552         local pkg=$1
553         pkg="`ipkg_safe_pkg_name $pkg`"
554         cat /tmp/ipkg/${pkg}.depends
555         ## eval "echo \$${pkg}_depends"
556 }
557
558 ipkg_set_installed() {
559         local pkg=$1
560         pkg="`ipkg_safe_pkg_name $pkg`"
561         echo installed > /tmp/ipkg/${pkg}.installed
562         ## setvar ${pkg}_installed "installed"
563 }
564
565 ipkg_set_uninstalled() {
566         local pkg=$1
567         pkg="`ipkg_safe_pkg_name $pkg`"
568         ### echo ipkg_set_uninstalled $pkg > /dev/console
569         echo uninstalled > /tmp/ipkg/${pkg}.installed
570         ## setvar ${pkg}_installed "uninstalled"
571 }
572
573 ipkg_get_installed() {
574         local pkg=$1
575         pkg="`ipkg_safe_pkg_name $pkg`"
576         if [ -f /tmp/ipkg/${pkg}.installed ]; then
577                 cat /tmp/ipkg/${pkg}.installed
578         fi
579         ## eval "echo \$${pkg}_installed"
580 }
581
582 ipkg_depends() {
583         local new_pkgs="$*"
584         local all_deps=
585         local installed_pkgs="`ipkg_status_matching_all 'Status:.*[[:space:]]installed'`"
586         for pkg in $installed_pkgs; do
587             ipkg_set_installed $pkg
588         done
589         while [ -n "$new_pkgs" ]; do
590                 all_deps="$all_deps $new_pkgs"
591                 local new_deps=
592                 for pkg in $new_pkgs; do
593                         if echo $pkg | grep -q '[^a-z0-9.+-]'; then
594                                 echo "ipkg_depends: ERROR: Package name $pkg contains illegal characters (should be [a-z0-9.+-])" >&2
595                                 return 1
596                         fi
597                         # TODO: Fix this. For now I am ignoring versions and alternations in dependencies.
598                         new_deps="$new_deps "`ipkg_info $pkg '\(Pre-\)\?Depends' | ipkg_extract_value | sed -e 's/([^)]*)//g
599 s/\(|[[:space:]]*[a-z0-9.+-]\+[[:space:]]*\)\+//g
600 s/,/ /g
601 s/ \+/ /g'`
602                         ipkg_set_depends $pkg $new_deps
603                 done
604
605                 new_deps=`echo $new_deps | sed -e 's/[[:space:]]\+/\\
606 /g' | sort | uniq`
607
608                 local maybe_new_pkgs=
609                 for pkg in $new_deps; do
610                         if ! echo $installed_pkgs | grep -q "\<$pkg\>"; then
611                                 maybe_new_pkgs="$maybe_new_pkgs $pkg"
612                         fi
613                 done
614
615                 new_pkgs=
616                 for pkg in $maybe_new_pkgs; do
617                         if ! echo $all_deps | grep -q "\<$pkg\>"; then
618                                 if [ -z "`ipkg_info $pkg`" ]; then
619                                         echo "ipkg_depends: Warning: $pkg mentioned in dependency but no package found in $IPKG_LISTS_DIR" >&2
620                                         ipkg_set_installed $pkg
621                                 else
622                                         new_pkgs="$new_pkgs $pkg"
623                                         ipkg_set_uninstalled $pkg
624                                 fi
625                         else
626                                 ipkg_set_uninstalled $pkg
627                         fi
628                 done
629         done
630
631         echo $all_deps
632 }
633
634 ipkg_get_install_dest() {
635         local dest="$1"
636         shift
637         local sd=$dest/$IPKG_DIR_PREFIX
638         local info_dir=$sd/info
639
640         local requested_pkgs="$*"
641         local pkgs="`ipkg_depends $*`"
642
643         mkdir -p $info_dir
644         for pkg in $pkgs; do
645                 if ! ipkg_status_mentioned_sd $sd $pkg; then
646                         echo "Package: $pkg
647 Status: install ok not-installed" | ipkg_status_update_sd $sd $pkg
648                 fi
649         done
650         ## mark the packages that we were directly requested to install as uninstalled
651         for pkg in $requested_pkgs; do ipkg_set_uninstalled $pkg; done
652
653         local new_pkgs=
654         local pkgs_installed=0
655         while [ -n "pkgs" ]; do
656                 curcheck=0
657                 ## echo "pkgs to install: {$pkgs}" > /dev/console
658                 for pkg in $pkgs; do
659                         curcheck="`expr $curcheck + 1`"
660                         local is_installed="`ipkg_get_installed $pkg`"
661                         if [ "$is_installed" = "installed" ]; then
662                                 echo "$pkg is installed" > /dev/console
663                                 continue
664                         fi
665
666                         local remaining_deps="`ipkg_unsatisfied_dependences $pkg`"
667                         if [ -n "$remaining_deps" ]; then
668                                 new_pkgs="$new_pkgs $pkg"
669                                 ### echo "Dependences not satisfied for $pkg: $remaining_deps"
670                                 if [ $curcheck -ne `echo  $pkgs|wc -w` ]; then
671                                         continue
672                                 fi
673                         fi
674
675                         local filename=
676                         for src in `ipkg_src_names`; do
677                                 if ipkg_require_list $src; then
678                                         filename="`ipkg_extract_paragraph $pkg < $IPKG_LISTS_DIR/$src | ipkg_extract_field Filename | ipkg_extract_value`"
679                                         [ -n "$filename" ] && break
680                                 fi
681                         done
682
683                         if [ -z "$filename" ]; then
684                                 echo "ipkg_get_install: ERROR: Cannot find package $pkg in $IPKG_LISTS_DIR"
685                                 echo "ipkg_get_install:        Check the spelling and maybe run \`ipkg update'."
686                                 ipkg_status_remove_sd $sd $pkg
687                                 return 1;
688                         fi
689
690                         [ -e "$IPKG_TMP" ] || mkdir -p $IPKG_TMP
691
692                         echo ""
693                         local tmp_pkg_file="$IPKG_TMP/"`ipkg_file_part $filename`
694                         if ! ipkg_download `ipkg_src_byname $src`/$filename $tmp_pkg_file; then
695                                 echo "ipkg_get_install: Perhaps you need to run \`ipkg update'?"
696                                 return 1
697                         fi
698
699                         if ! ipkg_install_file_dest $dest $tmp_pkg_file; then
700                                 echo "ipkg_get_install: ERROR: Failed to install $tmp_pkg_file"
701                                 echo "ipkg_get_install: I'll leave it there for you to try a manual installation"
702                                 return 1
703                         fi
704
705                         ipkg_set_installed $pkg
706                         pkgs_installed="`expr $pkgs_installed + 1`"
707                         rm $tmp_pkg_file
708                 done
709                 ### echo "Installed $pkgs_installed package(s) this round"
710                 if [ $pkgs_installed -eq 0 ]; then
711                         if [ -z "$new_pkgs" ]; then
712                             break
713                         fi
714                 fi
715                 pkgs_installed=0
716                 pkgs="$new_pkgs"
717                 new_pkgs=
718                 curcheck=0
719         done
720 }
721
722 ipkg_get_install() {
723         ipkg_get_install_dest $IPKG_ROOT $*
724 }
725
726 ipkg_install_file_dest() {
727         local dest="$1"
728         local filename="$2"
729         local sd=$dest/$IPKG_DIR_PREFIX
730         local info_dir=$sd/info
731
732         if [ ! -f "$filename" ]; then
733                 echo "ipkg_install_file: ERROR: File $filename not found"
734                 return 1
735         fi
736
737         local pkg="`ipkg_file_part $filename | sed 's/\([a-z0-9.+-]\+\)_.*/\1/'`"
738         local ext="`echo $filename | sed 's/.*\.//'`"
739         local pkg_extract_stdout
740         if [ "$ext" = "ipk" ]; then
741                 pkg_extract_stdout="tar -xzOf"
742         elif [ "$ext" = "deb" ]; then
743                 pkg_extract_stdout="ar p"
744         else
745                 echo "ipkg_install_file: ERROR: File $filename has unknown extension $ext (not .ipk or .deb)"
746                 return 1
747         fi
748
749         # Check dependencies
750         local depends="`ipkg_depends $pkg | sed -e "s/\<$pkg\>//"`"
751
752         # Don't worry about deps that are scheduled for installation
753         local missing_deps=
754         for dep in $depends; do
755                 if ! ipkg_status_all $dep | grep -q 'Status:[[:space:]]install'; then
756                         missing_deps="$missing_deps $dep"
757                 fi
758         done
759
760         if [ ! -z "$missing_deps" ]; then
761                 if [ -n "$FORCE_DEPENDS" ]; then
762                         echo "ipkg_install_file: Warning: $pkg depends on the following uninstalled programs: $missing_deps"
763                 else
764                         echo "ipkg_install_file: ERROR: $pkg depends on the following uninstalled programs:
765         $missing_deps"
766                         echo "ipkg_install_file: You may want to use \`ipkg install' to install these."
767                         return 1
768                 fi
769         fi
770
771         mkdir -p $IPKG_TMP/$pkg/control
772         mkdir -p $IPKG_TMP/$pkg/data
773         mkdir -p $info_dir
774
775         if ! $pkg_extract_stdout $filename ./control.tar.gz | (cd $IPKG_TMP/$pkg/control; tar -xzf - ) ; then
776                 echo "ipkg_install_file: ERROR unpacking control.tar.gz from $filename"
777                 return 1
778         fi
779
780         if [ -n "$IPKG_OFFLINE_ROOT" ]; then
781                 if grep -q '^InstallsOffline:[[:space:]]*no' $IPKG_TMP/$pkg/control/control; then
782                         echo "*** Warning: Package $pkg may not be installed in offline mode"
783                         echo "*** Warning: Scheduling $filename for pending installation (installing into $IPKG_PENDING_DIR)"
784                         echo "Package: $pkg
785 Status: install ok pending" | ipkg_status_update_sd $sd $pkg
786                         mkdir -p $IPKG_PENDING_DIR
787                         cp $filename $IPKG_PENDING_DIR
788                         rm -r $IPKG_TMP/$pkg/control
789                         rm -r $IPKG_TMP/$pkg/data
790                         rmdir $IPKG_TMP/$pkg
791                         return 0
792                 fi
793         fi
794
795
796         echo -n "Unpacking $pkg..."
797         set +o noglob
798         for file in $IPKG_TMP/$pkg/control/*; do
799                 local base_file="`ipkg_file_part $file`"
800                 mv $file $info_dir/$pkg.$base_file
801         done
802         set -o noglob
803         rm -r $IPKG_TMP/$pkg/control
804
805         if ! $pkg_extract_stdout $filename ./data.tar.gz | (cd $IPKG_TMP/$pkg/data; tar -xzf - ) ; then
806                 echo "ipkg_install_file: ERROR unpacking data.tar.gz from $filename"
807                 return 1
808         fi
809         echo "Done."
810
811         echo -n "Configuring $pkg..."
812         export PKG_ROOT=$dest
813         if [ -x "$info_dir/$pkg.preinst" ]; then
814                 if ! $info_dir/$pkg.preinst install; then
815                         echo "$info_dir/$pkg.preinst failed. Aborting installation of $pkg"
816                         rm -rf $IPKG_TMP/$pkg/data
817                         rmdir $IPKG_TMP/$pkg
818                         return 1
819                 fi
820         fi
821
822         local old_conffiles="`ipkg_status_sd $sd $pkg Conffiles | ipkg_extract_value`"
823         local new_conffiles=
824         if [ -f "$info_dir/$pkg.conffiles" ]; then
825                 for conffile in `cat $info_dir/$pkg.conffiles`; do
826                         if [ -f "$dest/$conffile" ] && ! echo " $old_conffiles " | grep -q " $conffile "`md5sum $dest/$conffile | sed 's/ .*//'`; then
827                                 local use_maintainers_conffile=
828                                 if [ -z "$FORCE_DEFAULTS" ]; then
829                                         while true; do
830                                                 echo -n "Configuration file \`$conffile'
831  ==> File on system created by you or by a script.
832  ==> File also in package provided by package maintainer.
833    What would you like to do about it ?  Your options are:
834     Y or I  : install the package maintainer's version
835     N or O  : keep your currently-installed version
836       D     : show the differences between the versions (if diff is installed)
837  The default action is to keep your current version.
838 *** `ipkg_file_part $conffile` (Y/I/N/O/D) [default=N] ? "
839                                                 read response
840                                                 case "$response" in
841                                                 [YyIi] | [Yy][Ee][Ss])
842                                                         use_maintainers_conffile=t
843                                                         break
844                                                 ;;
845                                                 [Dd])
846                                                         echo "
847 diff -u $dest/$conffile $IPKG_TMP/$pkg/data/$conffile"
848                                                         diff -u $dest/$conffile $IPKG_TMP/$pkg/data/$conffile || true
849                                                         echo "[Press ENTER to continue]"
850                                                         read junk
851                                                 ;;
852                                                 *)
853                                                         break
854                                                 ;;
855                                                 esac
856                                         done
857                                 fi
858                                 if [ -n "$use_maintainers_conffile" ]; then
859                                         local md5sum="`md5sum $IPKG_TMP/$pkg/data/$conffile | sed 's/ .*//'`"
860                                         new_conffiles="$new_conffiles $conffile $md5sum"
861                                 else
862                                         new_conffiles="$new_conffiles $conffile <custom>"
863                                         rm $IPKG_TMP/$pkg/data/$conffile
864                                 fi
865                         else
866                                 md5sum="`md5sum $IPKG_TMP/$pkg/data/$conffile | sed 's/ .*//'`"
867                                 new_conffiles="$new_conffiles $conffile $md5sum"
868                         fi
869                 done
870         fi
871
872         local owd="`pwd`"
873         (cd $IPKG_TMP/$pkg/data/; tar cf - . | (cd $owd; cd $dest; tar xf -))
874         rm -rf $IPKG_TMP/$pkg/data
875         rmdir $IPKG_TMP/$pkg
876         $pkg_extract_stdout $filename ./data.tar.gz | tar tzf - | sed -e 's/^\.//' > $info_dir/$pkg.list
877
878         if [ -x "$info_dir/$pkg.postinst" ]; then
879                 $info_dir/$pkg.postinst configure
880         fi
881
882         if [ -n "$new_conffiles" ]; then
883                 new_conffiles='Conffiles: '`echo $new_conffiles | ipkg_protect_slashes`
884         fi
885         local sed_safe_root="`echo $dest | sed -e "s/^${IPKG_OFFLINE_ROOT}//" | ipkg_protect_slashes`"
886         sed -e "s/\(Package:.*\)/\1\\
887 Status: install ok installed\\
888 Root: ${sed_safe_root}\\
889 ${new_conffiles}/" $info_dir/$pkg.control | ipkg_status_update_sd $sd $pkg
890
891         rm -f $info_dir/$pkg.control
892         rm -f $info_dir/$pkg.conffiles
893         rm -f $info_dir/$pkg.preinst
894         rm -f $info_dir/$pkg.postinst
895
896         echo "Done."
897 }
898
899 ipkg_install_file() {
900         ipkg_install_file_dest $IPKG_ROOT $*
901 }
902
903 ipkg_install() {
904
905         while [ $# -gt 0 ]; do
906                 local pkg="$1"
907                 shift
908         
909                 case "$pkg" in
910                 http://* | ftp://*)
911                         local tmp_pkg_file="$IPKG_TMP/"`ipkg_file_part $pkg`
912                         if ipkg_download $pkg $tmp_pkg_file; then
913                                 ipkg_install_file $tmp_pkg_file
914                                 rm $tmp_pkg_file
915                         fi
916                         ;;
917                 file:/*.ipk  | file://*.deb)
918                                 local ipkg_filename="`echo $pkg|sed 's/^file://'`"
919                                 ipkg_install_file $ipkg_filename
920                         ;;
921                 *.ipk  | *.deb)
922                         if [ -f "$pkg" ]; then
923                                 ipkg_install_file $pkg
924                         else
925                                 echo "File not found $pkg" >&2
926                         fi
927                         ;;
928                 *)
929                         ipkg_get_install $pkg || true
930                         ;;
931                 esac
932         done
933 }
934
935 ipkg_install_pending() {
936         [ -n "$IPKG_OFFLINE_ROOT" ] && return 0
937
938         if [ -d "$IPKG_PENDING_DIR" ]; then
939                 set +o noglob
940                 local pending="`ls -1d $IPKG_PENDING_DIR/*.ipk 2> /dev/null`" || true
941                 set -o noglob
942                 if [ -n "$pending" ]; then
943                         echo "The following packages in $IPKG_PENDING_DIR will now be installed:"
944                         echo $pending
945                         for filename in $pending; do
946                                 if ipkg_install_file $filename; then
947                                         rm $filename
948                                 fi
949                         done
950                 fi
951         fi
952         return 0
953 }
954
955 ipkg_install_wanted() {
956         local wanted="`ipkg_status_matching 'Status:[[:space:]]*install.*not-installed'`"
957
958         if [ -n "$wanted" ]; then
959                 echo "The following package were previously requested but have not been installed:"
960                 echo $wanted
961
962                 if [ -n "$FORCE_DEFAULTS" ]; then
963                         echo "Installing them now."
964                 else
965                         echo -n "Install them now [Y/n] ? "
966                         read response
967                         case "$response" in
968                         [Nn] | [Nn][Oo])
969                                 return 0
970                                 ;;
971                         esac
972                 fi
973
974                 ipkg_install $wanted
975         fi
976
977         return 0
978 }
979
980 ipkg_upgrade_pkg() {
981         local pkg="$1"
982         local avail_ver="`ipkg_info $pkg Version | ipkg_extract_value | head -1`"
983
984         is_installed=
985         for dest_name in `ipkg_dest_names`; do
986                 local dest="`ipkg_dest_byname $dest_name`"
987                 local sd=$dest/$IPKG_DIR_PREFIX
988                 local inst_ver="`ipkg_status_sd $sd $pkg Version | ipkg_extract_value`"
989                 if [ -n "$inst_ver" ]; then
990                         is_installed=t
991
992                         if [ -z "$avail_ver" ]; then
993                                 echo "Assuming locally installed package $pkg ($inst_ver) is up to date"
994                                 return 0
995                         fi
996
997                         if [ "$avail_ver" = "$inst_ver" ]; then 
998                                 echo "Package $pkg ($inst_ver) installed in $dest_name is up to date"
999                         elif ipkg_is_upgrade "$avail_ver" "$inst_ver"; then
1000                                 echo "Upgrading $pkg ($dest_name) from $inst_ver to $avail_ver"
1001                                 ipkg_get_install_dest $dest $pkg
1002                         else
1003                                 echo "Not downgrading package $pkg from $inst_ver to $avail_ver"
1004                         fi
1005                 fi
1006         done
1007
1008         if [ -z "$is_installed" ]; then
1009                 echo "Package $pkg does not appear to be installed"
1010                 return 0
1011         fi
1012
1013 }
1014
1015 ipkg_upgrade() {
1016         if [ $# -lt 1 ]; then
1017                 local pkgs="`ipkg_status_matching 'Status:.*[[:space:]]installed'`"
1018         else
1019                 pkgs="$*"
1020         fi
1021         
1022         for pkg in $pkgs; do
1023                 ipkg_upgrade_pkg $pkg
1024         done
1025 }
1026
1027 ipkg_remove_pkg_dest() {
1028         local dest="$1"
1029         local pkg="$2"
1030         local sd=$dest/$IPKG_DIR_PREFIX
1031         local info_dir=$sd/info
1032
1033         if ! ipkg_status_installed_sd $sd $pkg; then
1034                 echo "ipkg_remove: Package $pkg does not appear to be installed in $dest"
1035                 if ipkg_status_mentioned_sd $sd $pkg; then
1036                         echo "Purging mention of $pkg from the ipkg database"
1037                         ipkg_status_remove_sd $sd $pkg
1038                 fi
1039                 return 1
1040         fi
1041
1042         echo "ipkg_remove: Removing $pkg... "
1043
1044         local files="`cat $info_dir/$pkg.list`"
1045
1046         export PKG_ROOT=$dest
1047         if [ -x "$info_dir/$pkg.prerm" ]; then
1048                 $info_dir/$pkg.prerm remove
1049         fi
1050
1051         local conffiles="`ipkg_status_sd $sd $pkg Conffiles | ipkg_extract_value`"
1052
1053         local dirs_to_remove=
1054         for file in $files; do
1055                 if [ -d "$dest/$file" ]; then
1056                         dirs_to_remove="$dirs_to_remove $dest/$file"
1057                 else
1058                         if echo " $conffiles " | grep -q " $file "; then
1059                                 if echo " $conffiles " | grep -q " $file "`md5sum $dest/$file | sed 's/ .*//'`; then
1060                                         rm -f $dest/$file
1061                                 fi
1062                         else
1063                                 rm -f $dest/$file
1064                         fi
1065                 fi
1066         done
1067
1068         local removed_a_dir=t
1069         while [ -n "$removed_a_dir" ]; do
1070                 removed_a_dir=
1071                 local new_dirs_to_remove=
1072                 for dir in $dirs_to_remove; do
1073                         if rmdir $dir >/dev/null 2>&1; then
1074                                 removed_a_dir=t
1075                         else
1076                                 new_dirs_to_remove="$new_dirs_to_remove $dir"
1077                         fi
1078                 done
1079                 dirs_to_remove="$new_dirs_to_remove"
1080         done
1081
1082         if [ -n "$dirs_to_remove" ]; then
1083                 echo "ipkg_remove: Warning: Not removing the following directories since they are not empty:" >&2
1084                 echo "$dirs_to_remove" | sed -e 's/\/[/]\+/\//g' >&2
1085         fi
1086
1087         if [ -x "$info_dir/$pkg.postrm" ]; then
1088                 $info_dir/$pkg.postrm remove
1089         fi
1090
1091         ipkg_status_remove_sd $sd $pkg
1092         set +o noglob
1093         rm -f $info_dir/$pkg.*
1094         set -o noglob
1095
1096         echo "Done."
1097 }
1098
1099 ipkg_remove_pkg() {
1100         local pkg="$1"
1101         for dest in `ipkg_dests_all`; do
1102                 local sd=$dest/$IPKG_DIR_PREFIX
1103                 if ipkg_status_mentioned_sd $sd $pkg; then
1104                         ipkg_remove_pkg_dest $dest $pkg
1105                 fi
1106         done
1107 }
1108
1109 ipkg_remove() {
1110         while [ $# -gt 0 ]; do
1111                 local pkg="$1"
1112                 shift
1113                 if [ -n "$DEST_NAME" ]; then
1114                         ipkg_remove_pkg_dest $IPKG_ROOT $pkg
1115                 else
1116                         ipkg_remove_pkg $pkg
1117                 fi
1118         done
1119 }
1120
1121 ###########
1122 # ipkg main
1123 ###########
1124
1125 # Parse options
1126 while [ $# -gt 0 ]; do
1127         arg="$1"
1128         case $arg in
1129         -d | -dest)
1130                 [ $# -gt 1 ] || ipkg_usage "option $arg requires an argument"
1131                 DEST_NAME="$2"
1132                 shift
1133                 ;;
1134         -o | -offline)
1135                 [ $# -gt 1 ] || ipkg_usage "option $arg requires an argument"
1136                 IPKG_OFFLINE_ROOT="$2"
1137                 shift
1138                 ;;
1139         -force-depends)
1140                 FORCE_DEPENDS=t
1141                 ;;
1142         -force-defaults)
1143                 FORCE_DEFAULTS=t
1144                 ;;
1145         -*)
1146                 ipkg_usage "unknown option $arg"
1147                 ;;
1148         *)
1149                 break
1150                 ;;
1151         esac
1152         shift
1153 done
1154
1155 [ $# -lt 1 ] && ipkg_usage "ipkg must have one sub-command argument"
1156 cmd="$1"
1157 shift
1158
1159 ipkg_load_configuration
1160 mkdir -p /tmp/ipkg
1161
1162 case "$cmd" in
1163 update|upgrade|list|info|status|install_pending)
1164         ;;
1165 install|depends|remove|files|search)
1166         [ $# -lt 1 ] && ipkg_usage "ERROR: the \`\`$cmd'' command requires an argument"
1167         ;;
1168 *)
1169         echo "ERROR: unknown sub-command \`$cmd'"
1170         ipkg_usage
1171         ;;
1172 esac
1173
1174 # Only install pending if we have an interactive sub-command
1175 case "$cmd" in
1176 upgrade|install)
1177         ipkg_install_pending
1178         ipkg_install_wanted
1179         ;;
1180 esac
1181
1182 ipkg_$cmd $*
1183 for a in `ls $IPKG_TMP`; do
1184         rm -rf $IPKG_TMP/$a
1185 done