Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / bpm / bpm.sh.in
blobbcd78774c6b9d50b342b0caff79ef83b80cc3dc6
1 #! /bin/sh
3 # $NetBSD$
5 # Copyright (c) 2003,2009 The NetBSD Foundation, Inc.
6 # All rights reserved.
8 # This code is derived from software contributed to The NetBSD Foundation
9 # by Alistair Crooks (agc@NetBSD.org)
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
20 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
33 die()
35 echo >&2 "$@"
36 exit 1
39 check_prog()
41 _var="$1"; _name="$2"
43 eval _tmp=\"\$$_var\"
44 if [ "x$_tmp" != "x" ]; then
45 # Variable is already set (by the user, for example)
46 return 0
49 for _d in `echo $PATH | tr ':' ' '`; do
50 if [ -x "$_d/$_name" ]; then
51 # Program found
52 eval $_var=\""$_d/$_name"\"
53 return 1
55 done
57 die "$_name not found in path."
60 check_prog awkprog awk
61 check_prog echoprog echo
62 check_prog ftpprog ftp
63 check_prog idprog id
64 check_prog moreprog more
65 check_prog pkg_addprog pkg_add
66 check_prog rmprog rm
67 check_prog sedprog sed
68 check_prog suprog su
69 check_prog unameprog uname
71 # print version and exit
72 version() {
73 $pkg_addprog -V
74 exit 0
77 # temporary files
78 tmpcategories=/tmp/categories.$$
79 tmppackages=/tmp/packages.$$
81 # some base parameters
82 base=ftp://ftp.NetBSD.org/pub/pkgsrc/packages
83 release=`${unameprog} -r | ${sedprog} -e 's/_STABLE//'`
84 machine=`${unameprog} -m`
86 sleepsecs=1
88 doit=""
90 while [ $# -gt 0 ]; do
91 case $1 in
92 -V) version ;;
93 -b) base=$2; shift ;;
94 -h) ${echoprog} "$0 [-b BaseURL] [-h] [-m machine] [-n] [-r release] [-v] [-w secs]"; exit 0;;
95 -m) machine=$2; shift ;;
96 -n) doit=":" ;;
97 -r) release=$2; shift ;;
98 -v) set -x ;;
99 -w) sleepsecs=$2; shift ;;
100 *) break ;;
101 esac
102 shift
103 done
105 category=""
107 while true; do
108 # if we don't have a packages file, then we need to choose a category
109 case "$category" in
110 "") # get possible categories
111 if [ ! -f $tmpcategories ]; then
112 ${echoprog} "Downloading package categories from ${base}..."
113 ${echoprog} "** QUIT" > $tmpcategories
114 ${echoprog} ls | ${ftpprog} ${base}/${release}/${machine}/ 2>/dev/null | \
115 ${awkprog} 'NF == 9 { if ($9 != "All") print $9 }' >> $tmpcategories
118 # check for bad release numbering
119 # - it usually shows with 0 categories being displayed
120 ${awkprog} 'END { if (NR == 1) { print "\n\n\n*** No categories found - is the OS release set properly? ***\n\n\n" } }' < $tmpcategories
122 # display possible categories
123 ${awkprog} '{ print NR ". " $0 }' < $tmpcategories | ${moreprog}
125 # read a category number from the user
126 ${echoprog} -n "Please type the category number: "
127 read choice
129 # validate user's choice
130 case "$choice" in
131 0|1) ${rmprog} -f $tmpcategories $tmppackages; exit 0 ;;
132 [2-9]|[0-9]*) category=`${awkprog} 'NR == '$choice' { print }' < $tmpcategories` ;;
133 *) category="" ;;
134 esac
135 case "$category" in
136 "") ${echoprog} "No such category \"$choice\""
137 sleep $sleepsecs
138 continue
140 esac
142 # get possible packages
143 ${echoprog} ""
144 ${echoprog} "Downloading package names from ${base}/${category}..."
145 ${echoprog} "** QUIT" > $tmppackages
146 ${echoprog} "** Change category" >> $tmppackages
147 ${echoprog} ls | ${ftpprog} ${base}/${release}/${machine}/${category}/ 2>/dev/null \
148 | ${awkprog} 'NF == 11 { print $9 }' >> $tmppackages
150 esac
152 # display possible packages
153 ${awkprog} '{ print NR ". " $0 }' < $tmppackages | ${moreprog}
155 # read a package number from the user
156 ${echoprog} -n "Please type the package number: "
157 read choice
159 # validate user's choice
160 case "$choice" in
161 1) ${rmprog} -f $tmppackages $tmpcategories; exit 0 ;;
162 2) category=""; continue ;; # no package to install - choose new category
163 [3-9]|[0-9]*) package=`${awkprog} 'NR == '$choice' { print }' < $tmppackages` ;;
164 *) package="" ;;
165 esac
166 case "$package" in
167 "") ${echoprog} "No such package \"$choice\""
168 sleep $sleepsecs
169 continue
171 esac
173 # check it's not already installed
174 pkgbase=`${echoprog} ${package} | ${sedprog} -e 's|-[0-9].*||'`
175 installed=`pkg_info -e $pkgbase`
176 case "$installed" in
177 "") ;;
178 *) ${echoprog} "$package selected, but $installed already installed"
179 sleep $sleepsecs
180 continue
182 esac
184 # Tell people what we're doing
185 ${echoprog} ""
186 ${echoprog} "Adding package ${base}/${release}/${machine}/${category}/${package}"
188 cmd="env PKG_PATH=${base}/${release}/${machine}/All ${pkg_addprog} ${package}"
190 # check if we need to become root for this
191 if [ `${idprog} -u` != 0 ]; then
192 ${echoprog} "Becoming root@`/bin/hostname` to add a binary package"
193 ${echoprog} -n "`${echoprog} ${suprog} | $awkprog '{ print $1 }'` "
194 $doit ${suprog} root -c "$cmd"
195 success=$?
196 else
197 $doit $cmd
198 success=$?
201 # give feedback after adding the package
202 case $success in
203 0) ${echoprog} "$package successfully installed" ;;
204 *) ${echoprog} "Problems when installing $package - please try again" ;;
205 esac
207 ${echoprog} ""
208 ${echoprog} -n "[Q]uit, [C]hange category, [I]nstall another package: "
209 read choice
211 case "$choice" in
212 [Qq]) break ;;
213 [Cc]) category="" ;;
214 [Ii]) ;;
215 esac
216 done
218 ${rmprog} -f $tmpcategories $tmppackages
220 exit 0