fa08cb16e38c340b92f5e79f7c3783193ad014a3
[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 # remove
81 if [ "$remove" = "1" ] ; then
82         # check if packages depend on package to be removed
83         pkgs=""
84         ((isdep = 0))
85         for fl_file in $inst_root/$DEPS_DIR/*; do
86                 if [ "`grep "^$package$" $fl_file`" != "" ] ; then
87                         pkg=`echo $fl_file | sed "s%$inst_root/$DEPS_DIR/%%"`
88                         if [ "$pkg" != "$package" ] ; then
89                                 ((isdep += 1))
90                                 pkgs="$pkg $pkgs"
91                         fi
92                 fi
93         done
94         if [ "$isdep" != "0" ] ; then
95                 echo "info:"
96                 echo "run time dependencies were autegenerated by build system."
97                 echo "they may be wrong. edit $inst_root/$DEPS_DIR/* files."
98                 echo "deps:"
99                 echo -en "$pkgs\n"
100                 echo "package $package should not be removed ($isdep deps)"
101                 [ "$no_dep" != "1" ] && exit 1
102         fi
103         # looking for flist file
104         fl_pris="unknown"
105         for fl_file in $inst_root/$FL_DIR/$package-[x,0-9]*; do
106                 [ -f $fl_file ] && \
107                 fl_pris="`echo $fl_file | awk -F- '{ print $NF }'` $fl_pris"
108         done
109         fl_max="0"
110         for pri in $fl_pris; do
111                 if [ "$pri" = "x" ] ; then
112                         fl_max="x"
113                         break
114                 fi
115                 [ "$pri" = "unknown" ] && break
116                 [ "$pri" -gt "$fl_max" ] && fl_max=$pri
117         done
118         fl_file="$inst_root/$FL_DIR/$package-$fl_max"
119         [ "$verbose" ] && echo "expecting flist file $fl_file"
120         if [ ! -f $fl_file ] ; then
121                 echo "package $package is not installed (moved the flist file?)"
122                 exit 1
123         fi
124
125         if [ "$simulate" != "1" ] ; then
126
127         # remove the files/dirs
128         ((supposed = 0))
129         ((counter = 0))
130         cat $fl_file | while read pkg file; do
131                 ((supposed += 1))
132                 if [ -e $inst_root/$file ] ; then
133                         if [ "$file" != "tmp" ] ; then
134                         if [ -d $inst_root/$file ] ; then
135                                 rmdir -p $inst_root/$file > /dev/null 2>&1
136                         else
137                                 rm -f $inst_root/$file > /dev/null 2>&1
138                                 rmdir -p `dirname $file` > /dev/null 2>&1
139                         fi
140                         ((counter += 1))
141                         fi
142                 fi
143                 echo -en "\rremoved $counter from $supposed supposed files"
144         done
145         echo -en "\ndone.\n"
146
147         # care about info/dir file
148         echo "recreating info dir file ..."
149         cd $inst_root/usr/share/info
150         rm -f dir
151         for f in *; do
152                 install-info $f dir 2>/dev/null
153         done
154
155         fi
156
157 # install
158 elif [ "$install" = "1" ] ; then
159         # does inst_root exist
160         if [ ! -d $inst_root ] ; then
161                 echo
162                 echo "the install directory doesnt exist, create it first"
163                 echo
164                 exit 1
165         fi
166
167         # does package exist?
168         pkg=`basename $package | sed 's/.tar.bz2//'`
169         if [ -f $inst_root/$FL_DIR/${pkg}-[0-9]* ] ; then
170                 echo "$pkg seems to be installed, candidates:"
171                 for i in $inst_root/$FL_DIR/${pkg}-[0-9]*; do
172                         echo "-> $i"
173                 done
174                 exit 1
175         fi
176
177         # package in database?
178         if [ "`grep "^$pkg\ -" $DBF`" = "" ] ; then
179                 echo "sorry, package $package not found ..."
180                 echo "(use 'hdw-get update' to update database)"
181                 exit 1
182         fi
183
184         # check for runtime deps
185         if [ -z $no_deps ] ; then
186                 deps="`grep "^$pkg\ -" $DBF | sed 's/.* -//'`"
187                 udeps=""
188                 tudeps=""
189                 for dep in $deps; do
190                         [ ! -f $inst_root/$FL_DIR/$dep-* ] && \
191                                 udeps="$dep $udeps"
192                 done
193                 while [ "$udeps" != "$tudeps" ] ; do
194                         tudeps="$udeps"
195                         for tdep in $tudeps; do
196                                 deps="`grep "^$tdep\ -" $DBF | sed 's/.* -//'`"
197                                 for dep in $deps; do
198                                         exists=0
199                                         [ -f $inst_root/$FL_DIR/$dep-* ] && \
200                                                 break
201                                         for i in $udeps; do
202                                                 [ "$i" = "$dep" ] && exists=1
203                                         done
204                                         [ "$exists" = "0" ] && \
205                                                 udeps="$udeps $dep"
206                                 done
207                         done
208                 done
209                 echo "$pkg depends on the following uninstalled packages:"
210                 echo "$udeps"
211                 echo
212                 echo "continue? [y,n] (default 'n')"
213                 if [ -z $auto_resolve_deps ] ; then
214                         read answer
215                         if [ "$answer" = "y" ] ; then
216                                 for i in $udeps; do
217                                         [ "$simulate" != "1" ] && \
218                                         $0 -no-dep install $inst_root $i
219                                 done
220                         fi
221                 fi
222         fi
223         
224         if [ "$simulate" != "1" ] ; then
225         
226         # check/install package
227         if [ -f $package ] ; then
228                 echo "extracting `basename $package` to $inst_root ..."
229                 tar --use-compress-program=bzip2 -xf $package \
230                         -C $inst_root
231                 echo "done"
232         elif [ ! -z "$bin_dir" -a -f $bin_dir/${hdw_arch}-${hdw_arch_opt}/$package/$package.tar.bz2 ] ; then
233                 echo "extracting $package to $inst_root ..."
234                 tar --use-compress-program=bzip2 \
235                 -xf $bin_dir/$package/$package.tar.bz2 -C $inst_root
236         elif [ ! -z "$net_addr" ] ; then
237                 echo "getting package $package via net ..."
238                 wget $net_addr/hdw-linux/hdw-linux-$hdw_version/binaries/${hdw_arch}-${hdw_arch_opt}/$package/${package}.tar.bz2 > /dev/null 2>&1
239                 retval=$?
240                 if [ "$retval" != "0" ] ; then
241                         echo "package not found at $net_addr, aborting ..."
242                         exit
243                 else
244                         echo "extracting $package to $inst_root ..."    
245                         tar --use-compress-program=bzip2 \
246                                 -xf ${package}.tar.bz2 -C $inst_root
247                         rm ${package}.tar.bz2
248                         echo "done"
249                 fi
250         else
251                 echo "sorry, package $package not found"
252                 exit 1
253         fi
254
255         # care about info/dir file
256         echo "recreating info dir file ..."
257         cd $inst_root/usr/share/info
258         rm -f dir
259         for f in *; do
260                 install-info $f dir 2>/dev/null
261         done
262
263         fi
264
265 # dist-install
266 elif [ "$d_install" = "1" ] ; then
267         # check dist file
268         if [ ! -f $dist_file ] ; then
269                 echo "$dist_file not found, aborting"
270         else
271                 # install all distribution related packages
272                 for package in `cat $dist_file`; do
273                         # execute myelf
274                         $0 install $inst_root $package
275                 done
276         fi
277 fi
278