see CHANGELOG 2004-04-23
[hdw-linux/hdw-linux.git] / misc / hdw-tools / hdw-get
1 #!/bin/sh
2 #
3 # author: hackbard@hackdaworld.dyndns.org
4 #
5 # hdw-tools: 
6 # this is hdw-get script, used to install/remove/(update) binary packages
7
8
9 usage() {
10         echo
11         echo "usage:"
12         echo "$0 [install/remove] [root] [package]"
13         echo "$0 [dist-install] [root] [dist file]"
14         echo
15         echo "options:"
16         echo "-v        tell me whats going on"
17         echo "-a        auto resolve deps, do not prompt user"
18         echo "-no-dep   do not care for dependencies (used by hdw-get itself)"
19         echo "-s        simulation only, do not install/remove packages"
20         echo
21 }
22
23 package="" ; inst_root=""
24 update=""
25 d_install="" ; install=""
26 verbose="" ; no_dep=""
27 auto_resolve_deps=""
28 simulate=""
29
30 FL_DIR="var/adm/flists"
31 DEPS_DIR="var/adm/deps/run"
32 CONF="/etc/hdw-get.conf"
33 DBF="/etc/hdw-get.db"
34
35 while [ "$1" ] ; do
36         case "$1" in
37                 update)         update="1" ; package="fake" ; inst_root="fake"
38                                 shift 1 ;;
39                 install)        install="1" 
40                                 inst_root="$2" ; package="$3" ; shift 3 ;;
41                 remove)         remove="1" 
42                                 inst_root="$2" ; package="$3" ; shift 3 ;;
43                 dist-install)   d_install="1"  ; package="fake"
44                                 inst_root="$2" ; dist_file="$3" ; shift 3 ;;
45                 -v)             verbose=1 ; shift 1 ;;
46                 -a)             auto_resolve_deps="1" ; shift 1 ;;
47                 -no-dep)        no_dep="1" ; shift 1 ;;
48                 -s)             simulate="1" ; shift 1 ;;
49                 *)              usage ; exit 1 ;;
50         esac
51 done
52                         
53 if [ -z "inst_root" -o -z "$package" ] ; then
54         usage ; exit 1
55 fi
56         
57 # read hdw-get configs
58 net_addr="" ; bin_dir="" ; distf_dir=""
59 if [ -f $CONF ] ; then
60         . $CONF
61         if [ -z "$hdw_version" -o -z "$hdw_arch" -o -z "$hdw_arch_opt" ] ; then
62                 echo "version, arch and arch_opt need to be specified."
63                 echo "adjust $CONF file"
64                 exit
65         else
66                 [ "$verbose" ] && echo "hdw-get.conf file looks good ..."
67         fi
68 else
69         echo "$CONF file not found, aborting"
70         exit 1
71 fi
72
73 if [ "$update" = "1" ] ; then
74         echo -en "updating hdw-get.db file ..."
75         wget -q $net_addr/hdw-linux/hdw-linux-$hdw_version/hdw-get/hdw-get.db \
76                 -O $DBF
77         echo -en " done. (`cat $DBF | wc -l` packages)\n"
78 fi
79
80 # create temp directory
81 mkdir -p /tmp/.hdw-get
82
83 # remove
84 if [ "$remove" = "1" ] ; then
85         # check if packages depend on package to be removed
86         pkgs=""
87         ((isdep = 0))
88         for fl_file in $inst_root/$DEPS_DIR/*; do
89                 if [ "`grep "^$package$" $fl_file`" != "" ] ; then
90                         pkg=`echo $fl_file | sed "s%$inst_root/$DEPS_DIR/%%"`
91                         if [ "$pkg" != "$package" ] ; then
92                                 ((isdep += 1))
93                                 pkgs="$pkg $pkgs"
94                         fi
95                 fi
96         done
97         if [ "$isdep" != "0" ] ; then
98                 echo "info:"
99                 echo "run time dependencies were autegenerated by build system."
100                 echo "they may be wrong. edit $inst_root/$DEPS_DIR/* files."
101                 echo "deps:"
102                 echo -en "$pkgs\n"
103                 echo "package $package should not be removed ($isdep deps)"
104                 [ "$no_dep" != "1" ] && exit 1
105         fi
106         # looking for flist file
107         fl_pris="unknown"
108         for fl_file in $inst_root/$FL_DIR/$package-[x,0-9]*; do
109                 [ -f $fl_file ] && \
110                 fl_pris="`echo $fl_file | awk -F- '{ print $NF }'` $fl_pris"
111         done
112         fl_max="0"
113         for pri in $fl_pris; do
114                 if [ "$pri" = "x" ] ; then
115                         fl_max="x"
116                         break
117                 fi
118                 [ "$pri" = "unknown" ] && break
119                 [ "$pri" -gt "$fl_max" ] && fl_max=$pri
120         done
121         fl_file="$inst_root/$FL_DIR/$package-$fl_max"
122         [ "$verbose" ] && echo "expecting flist file $fl_file"
123         if [ ! -f $fl_file ] ; then
124                 echo "package $package is not installed (moved the flist file?)"
125                 exit 1
126         fi
127
128         if [ "$simulate" != "1" ] ; then
129
130         # care about info/dir file
131         rm -f /tmp/.hdw-get/dir* ; modify_info_dir=""
132         if [ -n "`grep usr/share/info/dir $fl_file`" ] ; then
133         modify_info_dir="1"
134         cp $inst_root/usr/share/info/dir /tmp/.hdw-get/
135         for info_obj in `grep 'usr/share/info/.*.info' $fl_file | \
136                          awk -F/ '{ print $4 }'`; do
137                 info_o=`echo $info_obj | sed 's/.info//'`
138                 [ "$verbose" ] && echo "deleting $info_o from info/dir"
139                 grep -v \($info_o\)\. /tmp/.hdw-get/dir | \
140                 grep -v ^$package\ info\ -\ added\ by\ hdw > \
141                 /tmp/.hdw-get/dir~
142                 mv /tmp/.hdw-get/dir~ /tmp/.hdw-get/dir
143         done
144         fi
145
146         # remove the files/dirs
147         ((supposed = 0))
148         ((counter = 0))
149         cat $fl_file | while read pkg file; do
150                 ((supposed += 1))
151                 if [ -e $inst_root/$file ] ; then
152                         if [ "$file" != "tmp" ] ; then
153                         if [ -d $inst_root/$file ] ; then
154                                 rmdir -p $inst_root/$file > /dev/null 2>&1
155                         else
156                                 rm -f $inst_root/$file > /dev/null 2>&1
157                                 rmdir -p `dirname $file` > /dev/null 2>&1
158                         fi
159                         ((counter += 1))
160                         fi
161                 fi
162                 echo -en "\rremoved $counter from $supposed supposed files"
163         done
164         echo -en "\ndone.\n"
165
166         # reinstall info/dir file
167         if [ "$modify_info_dir" ] ; then
168                 [ "$verbose" ] && echo "restoring modified info/dir file"
169                 cp /tmp/.hdw-get/dir $inst_root/usr/share/info/dir
170                 rm -f /tmp/.hdw-get/dir*
171         fi
172
173         fi
174
175 # install
176 elif [ "$install" = "1" ] ; then
177         # does inst_root exist
178         if [ ! -d $inst_root ] ; then
179                 echo
180                 echo "the install directory doesnt exist, create it first"
181                 echo
182                 exit 1
183         fi
184
185         # does package exist?
186         pkg=`basename $package | sed 's/.tar.bz2//'`
187         if [ -f $inst_root/$FL_DIR/${pkg}-[0-9]* ] ; then
188                 echo "$pkg seems to be installed, candidates:"
189                 for i in $inst_root/$FL_DIR/${pkg}-[0-9]*; do
190                         echo "-> $i"
191                 done
192                 exit 1
193         fi
194
195         # package in database?
196         if [ "`grep "^$pkg\ -" $DBF`" = "" ] ; then
197                 echo "sorry, package $package not found ..."
198                 echo "(use 'hdw-get update' to update database)"
199                 exit 1
200         fi
201
202         # check for runtime deps
203         if [ -z $no_deps ] ; then
204                 deps="`grep "^$pkg\ -" $DBF | sed 's/.* -//'`"
205                 udeps=""
206                 tudeps=""
207                 for dep in $deps; do
208                         [ ! -f $inst_root/$FL_DIR/$dep-* ] && \
209                                 udeps="$dep $udeps"
210                 done
211                 while [ "$udeps" != "$tudeps" ] ; do
212                         tudeps="$udeps"
213                         for tdep in $tudeps; do
214                                 deps="`grep "^$tdep\ -" $DBF | sed 's/.* -//'`"
215                                 for dep in $deps; do
216                                         exists=0
217                                         [ -f $inst_root/$FL_DIR/$dep-* ] && \
218                                                 break
219                                         for i in $udeps; do
220                                                 [ "$i" = "$dep" ] && exists=1
221                                         done
222                                         [ "$exists" = "0" ] && \
223                                                 udeps="$udeps $dep"
224                                 done
225                         done
226                 done
227                 echo "$pkg depends on the following uninstalled packages:"
228                 echo "$udeps"
229                 echo
230                 echo "continue? [y,n] (default 'n')"
231                 if [ -z $auto_resolve_deps ] ; then
232                         read answer
233                         if [ "$answer" = "y" ] ; then
234                                 for i in $udeps; do
235                                         [ "$simulate" != "1" ] && \
236                                         $0 -no-dep install $inst_root $i
237                                 done
238                         fi
239                 fi
240         fi
241         
242         if [ "$simulate" != "1" ] ; then
243
244         # store info/dir file
245         rm -f /tmp/.hdw-get/dir*
246         if [ -f $inst_root/usr/share/info/dir ] ; then
247                 [ "$verbose" ] && echo "backing up info/dir file"
248                 cp $inst_root/usr/share/info/dir /tmp/.hdw-get/
249         fi
250         
251         # check/install package
252         if [ -f $package ] ; then
253                 echo "extracting `basename $package` to $inst_root ..."
254                 tar --use-compress-program=bzip2 -xf $package \
255                         -C $inst_root
256                 echo "done"
257         elif [ ! -z "$bin_dir" -a -f $bin_dir/${hdw_arch}-${hdw_arch_opt}/$package/$package.tar.bz2 ] ; then
258                 echo "extracting $package to $inst_root ..."
259                 tar --use-compress-program=bzip2 \
260                 -xf $bin_dir/$package/$package.tar.bz2 -C $inst_root
261         elif [ ! -z "$net_addr" ] ; then
262                 echo "getting package $package via net ..."
263                 wget $net_addr/hdw-linux/hdw-linux-$hdw_version/binaries/${hdw_arch}-${hdw_arch_opt}/$package/${package}.tar.bz2 > /dev/null 2>&1
264                 retval=$?
265                 if [ "$retval" != "0" ] ; then
266                         echo "package not found at $net_addr, aborting ..."
267                         exit
268                 else
269                         echo "extracting $package to $inst_root ..."    
270                         tar --use-compress-program=bzip2 \
271                                 -xf ${package}.tar.bz2 -C $inst_root
272                         rm ${package}.tar.bz2
273                         echo "done"
274                 fi
275         else
276                 echo "sorry, package $package not found"
277                 exit 1
278         fi
279
280         # merging info/dir file
281         if [ "$pkg" != "00-dirtree" ] ; then
282         fl_file=`ls -A $inst_root/$FL_DIR/$pkg-[0-9]* | tail -1`
283         if [ -n "`grep usr/share/info/dir $fl_file`" ] ; then
284                 echo "" >> /tmp/.hdw-get/dir
285                 echo "$pkg info - added by hdw `date`" >> /tmp/.hdw-get/dir
286                 for info_obj in `grep 'usr/share/info/.*.info' $fl_file | \
287                                         awk -F/ '{ print $4 }'`; do
288                         info_o=`echo $info_obj | sed 's/.info//'`
289                         if [ -z "`grep \($info_o\)\. /tmp/.hdw-get/dir`" ]
290                         then
291                                 [ "$verbose" ] && \
292                                         echo "merging $info_o to info/dir"
293                                 grep \($info_o\)\. \
294                                         $inst_root/usr/share/info/dir >> \
295                                         /tmp/.hdw-get/dir
296                         fi
297                 done
298         fi
299         fi
300         [ -f /tmp/.hdw-get/dir ] && \
301                 cp /tmp/.hdw-get/dir $inst_root/usr/share/info/dir
302         rm -f /tmp/.hdw-get/dir
303
304         fi
305
306 # dist-install
307 elif [ "$d_install" = "1" ] ; then
308         # check dist file
309         if [ ! -f $dist_file ] ; then
310                 echo "$dist_file not found, aborting"
311         else
312                 # install all distribution related packages
313                 for package in `cat $dist_file`; do
314                         # execute myelf
315                         $0 install $inst_root $package
316                 done
317         fi
318 fi
319
320 # remove temp directory
321 rm -rf /tmp/.hdw-get
322