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