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