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