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