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