fix packman cd/net quirks
[minix3.git] / commands / scripts / packman.sh
blob7503918f1748a8f776efee3ae301b2b57ec6dac2
1 #!/bin/sh
3 TAG=`uname -p`-`uname -r`.`uname -v`
4 PACKDIR=`uname -p`/`uname -r`.`uname -v`
5 RC=/usr/etc/rc.package
6 CDDIR=PACKAGES
7 CDMP=/mnt
8 CDPACK=${CDMP}/install/packages
9 CDSRC=${CDMP}/install/package-sources
10 SRC=/usr/bigsrc
11 LISTFILE=/etc/packages-$TAG
12 LISTURL=http://www.minix3.org/packages/$PACKDIR/List
13 TMPDIR=/usr/tmp/packages
14 mkdir -p $TMPDIR
15 URL1=http://www.minix3.org/packages/$PACKDIR
16 SRCURL1=http://www.minix3.org/software
17 PATH=/bin:/sbin:/usr/bin:/usr/sbin
19 # can we execute bunzip2?
20 if bunzip2 --help 2>&1 | grep usage >/dev/null
21 then BUNZIP2=bunzip2
22 else BUNZIP2=smallbunzip2
25 if id | fgrep "uid=0(" >/dev/null
26 then :
27 else echo "Please run $0 as root."
28 exit 1
31 chmod 700 $TMPDIR
33 if [ -f "$RC" ]
34 then . "$RC"
37 cd /
39 # Make sure there is a $SRC dir
40 if [ ! -d "$SRC" ]
41 then mkdir $SRC || exit
44 # Is there a usable CD to install packages from?
45 cdpackages=""
46 if [ -n "$cddrive" ]
47 then pack=${cddrive}p2
48 umount $pack >/dev/null 2>&1 || true
49 echo "Checking for CD in $pack."
50 if mount -r $pack $CDMP 2>/dev/null
51 then fn="$CDPACK/List"
52 echo "Found."
53 cdpackages=$fn
54 if [ ! -f $cdpackages ]
55 then cdpackages=""
56 echo "No package list found on CD in $fn."
58 else echo "Not found."
60 else echo "Don't know where the install CD is. You can set it in $RC."
63 TMPF=$TMPDIR/.list.$$
64 rm -f $TMPF
65 rm -f $TMPDIR/.* # Remove any remaining .postinstall script or .list*
67 # Check for network packages too
68 netpackages=""
69 if ( : </dev/tcp ) 2>/dev/null
70 then echo -n "Update package list from network? (Y/n) "
71 read y
72 if [ "$y" != n -a "$y" != N ]
73 then echo "Fetching package list from $LISTURL."
74 urlget $LISTURL >$TMPF && mv $TMPF $LISTFILE || echo "Update not successful."
76 netpackages=$LISTFILE
77 if [ ! -f "$netpackages" -o ! `cat "$netpackages" 2>/dev/null | wc -l | awk '{ print $1 }'` -gt 1 ]
78 then netpackages=""
80 else echo "No working network detected."
83 # Is there at least one package type?
84 if [ ! -n "$netpackages" -a ! -n "$cdpackages" ]
85 then echo "No packages found."
86 exit 1
89 # Is there more than one package type?
90 if [ -n "$netpackages" -a -n "$cdpackages" ]
91 then echo -n "Would you like to install from (C)D or (N)etwork? [C] "
92 read whichsrc
93 if [ "$whichsrc" = N -o "$whichsrc" = n ]
94 then unset cdpackages
95 else unset netpackages
99 if [ -n "$netpackages" ]
100 then source=net
101 else source=cdrom
104 cont=y
105 while [ "$cont" = y ]
106 do cd $TMPDIR
107 echo ""
108 echo "Showing you a list of packages using more. Press q when"
109 echo "you want to leave the list."
110 echo -n "Press RETURN to continue.."
111 read xyzzy
112 echo "Package list:"
113 ( echo "No.|Package|Description"
115 if [ -f "$netpackages" -a "$source" = net ]
116 then cat $netpackages
118 if [ -f "$cdpackages" -a "$source" = cdrom ]
119 then cat $cdpackages
121 ) | sort -f -t'|' +0 | awk '{ n++; printf "%d|%s\n", n, $0 }'
122 ) >$TMPF
123 highest="`wc -l $TMPF | awk '{ print $1 - 1 }'`"
124 awk -F'|' <$TMPF '{ printf "%3s %-15s %s\n", $1, $2, $3 }' | more
125 echo "Format examples: '3', '3,6', '3-9', '3-9,11-15', 'all'"
126 echo -n "Package(s) to install (RETURN or q to exit)? "
127 read packnolist
128 if [ "$packnolist" = "" -o "$packnolist" = "q" ]
129 then exit 0
131 if [ "$packnolist" = all ]
132 then packnolist=1-$highest
134 IFS=','
135 set $packnolist
136 echo -n "Get source(s) too? (y/N) "
137 read getsources
138 for packrange in $packnolist
140 # Get a-b range.
141 IFS='-'
142 set $packrange
143 start=$1
144 if [ $# = 2 ]
145 then end=$2
146 else end=$1
148 IFS=' '
149 # use awk to make the range list
150 for packno in `awk </dev/null "BEGIN { for(i=$start; i<=$end; i++) { printf \"%d \", i } }"`
152 ok=y
153 pat="^$packno|"
154 if [ "`grep -c $pat $TMPF`" -ne 1 ]
155 then if [ "$packno" ]
156 then echo "$packno: Wrong package number."
158 ok=n
160 if [ $ok = y ]
161 then
162 packagename="`grep $pat $TMPF | awk -F'|' '{ print $2 }'`"
163 file=$packagename.tar.bz2
165 echo ""
167 if [ -f $file ]
168 then echo "Skipping $file - it's already in $TMPDIR."
169 echo "Remove that file if you want to re-retrieve and install this package."
170 else
171 case $source in
172 net*) echo "Retrieving $packno ($packagename) from primary location into $TMPDIR .."
173 srcurl=""
174 if urlget $URL1/$file >$file
175 then echo "Retrieved ok. Installing .."
176 packit $file && echo Installed ok.
177 srcurl=$SRCURL1/$file
178 else echo "Retrieval failed."
180 if [ "$getsources" = y -o "$getsources" = Y ]
181 then ( cd $SRC || exit
182 srcfile=${packagename}-src.tar.bz2
183 echo "Retrieving source from $srcurl .."
184 urlget $srcurl >$srcfile || exit
185 echo "Source retrieved in $SRC/$srcfile."
186 $BUNZIP2 -dc $srcfile | tar xf - >/dev/null || exit
187 echo "Source unpacked in $SRC."
191 cdrom*)
192 if [ -f $CDPACK/$file ]
193 then echo "Installing from $CDPACK/$file .."
194 packit $CDPACK/$file && echo Installed ok.
195 else echo "$CDPACK/$file not found."
197 srcfile=$CDSRC/${packagename}.tar.bz2
198 if [ -f $srcfile -a "$getsources" = y ]
199 then
200 ( cd $SRC || exit
201 $BUNZIP2 -dc $srcfile | tar xf - || exit
202 echo "Source $srcfile unpacked in $SRC."
206 esac
208 else cont=n
210 done # Iterate package range
211 done # Iterate package range list
212 done
214 rm -f $TMPDIR/.* # Remove any remaining .postinstall script or .list*