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