CHANGELOG 2004-12-06
[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
20 }
21
22 package="" ; inst_root=""
23 d_install="" ; install=""
24 verbose="" ; no_dep=""
25 auto_resolve_deps=""
26
27 while [ "$1" ] ; do
28         case "$1" in
29                 install)        install="1" 
30                                 inst_root="$2" ; package="$3" ; shift 3 ;;
31                 remove)         remove="1" 
32                                 inst_root="$2" ; package="$3" ; shift 3 ;;
33                 dist-install)   d_install="1"  ; package="fake"
34                                 inst_root="$2" ; dist_file="$3" ; shift 3 ;;
35                 -v)             verbose=1 ; shift 1 ;;
36                 -a)             auto_resolve_deps="1" ; shift 1 ;;
37                 -no-dep)        no_dep="1" ; shift 1 ;;
38                 *)              usage ; exit 1 ;;
39         esac
40 done
41                         
42 if [ -z "inst_root" -o -z "$package" ] ; then
43         usage ; exit 1
44 fi
45         
46 # read hdw-get configs
47 net_addr="" ; bin_dir="" ; distf_dir=""
48 if [ -f /etc/hdw-get.conf ] ; then
49         . /etc/hdw-get.conf
50         if [ -z "$hdw_version" -o -z "$hdw_arch" -o -z "$hdw_arch_opt" ] ; then
51                 echo "version, arch and arch_opt need to be specified."
52                 echo "adjust /etc/hdw-get.conf file"
53                 exit
54         else
55                 [ "$verbose" ] && echo "hdw-get.conf file looks good ..."
56         fi
57 else
58         echo "/etc/hdw-get.conf file not found, aborting"
59         exit 1
60 fi
61
62 # create temp directory
63 mkdir -p /tmp/.hdw-get
64
65 # remove
66 if [ "$remove" = "1" ] ; then
67         # looking for flist file
68         fl_pris=""
69         for fl_file in $inst_root/var/adm/flists/$package-[x,0-9]*; do
70                 fl_pris="`echo $fl_file | awk -F- '{ print $NF }'` $fl_pris"
71         done
72         fl_max="0"
73         for pri in $fl_pris; do
74                 if [ "$pri" = "x" ] ; then
75                         fl_max="x"
76                         break
77                 fi
78                 [ "$pri" -gt "$fl_max" ] && fl_max=$pri
79         done
80         fl_file="$inst_root/var/adm/flists/$package-$fl_max"
81         [ "$verbose" ] && echo "expecting flist file $fl_file"
82         if [ ! -f $fl_file ] ; then
83                 echo "package $apckage is not installed (moved the flist file?)"
84                 exit 1
85         fi
86
87         # care about info/dir file
88         rm -f /tmp/.hdw-get/dir* ; modify_info_dir=""
89         if [ -n "`grep usr/share/info/dir $fl_file`" ] ; then
90         modify_info_dir="1"
91         cp $inst_root/usr/share/info/dir /tmp/.hdw-get/
92         for info_obj in `grep 'usr/share/info/.*.info' $fl_file | \
93                          awk -F/ '{ print $4 }'`; do
94                 info_o=`echo $info_obj | sed 's/.info//'`
95                 [ "$verbose" ] && echo "deleting $info_o from info/dir"
96                 grep -v \($info_o\)\. /tmp/.hdw-get/dir | \
97                 grep -v ^$package\ info\ -\ added\ by\ hdw > \
98                 /tmp/.hdw-get/dir~
99                 mv /tmp/.hdw-get/dir~ /tmp/.hdw-get/dir
100         done
101         fi
102
103         # remove the files/dirs
104         ((supposed = 0))
105         ((counter = 0))
106         cat $fl_file | while read pkg file; do
107                 ((supposed += 1))
108                 if [ -e $inst_root/$file ] ; then
109                         if [ "$file" != "tmp" ] ; then
110                         if [ -d $inst_root/$file ] ; then
111                                 rmdir -p $inst_root/$file > /dev/null 2>&1
112                         else
113                                 rm -f $inst_root/$file > /dev/null 2>&1
114                                 rmdir -p `dirname $file` > /dev/null 2>&1
115                         fi
116                         ((counter += 1))
117                         fi
118                 fi
119                 echo -en "\rremoved $counter from $supposed supposed files"
120         done
121         echo -en "\ndone.\n"
122
123         # reinstall info/dir file
124         if [ "$modify_info_dir" ] ; then
125                 [ "$verbose" ] && echo "restoring modified info/dir file"
126                 cp /tmp/.hdw-get/dir $inst_root/usr/share/info/dir
127                 rm -f /tmp/.hdw-get/dir*
128         fi
129
130 # install
131 elif [ "$install" = "1" ] ; then
132         # does inst_root exist
133         if [ ! -d $inst_root ] ; then
134                 echo
135                 echo "the install directory doesnt exist, create it first"
136                 echo
137                 exit 1
138         fi
139
140         # does package exist?
141         pkg=`basename $package | sed 's/.tar.bz2//'`
142         if [ -f $inst_root/var/adm/flists/${pkg}-[0-9]* ] ; then
143                 echo "$pkg seems to be installed, candidates:"
144                 for i in $inst_root/var/adm/flists/${pkg}-[0-9]*; do
145                         echo "-> $i"
146                 done
147                 exit 1
148         fi
149
150         # store info/dir file
151         rm -f /tmp/.hdw-get/dir*
152         if [ -f $inst_root/usr/share/info/dir ] ; then
153                 [ "$verbose" ] && echo "backing up info/dir file"
154                 cp $inst_root/usr/share/info/dir /tmp/.hdw-get/
155         fi
156         
157         # check/install package
158         if [ -f $package ] ; then
159                 echo "extracting `basename $package` to $inst_root ..."
160                 tar --use-compress-program=bzip2 -xf $package \
161                         -C $inst_root
162                 echo "done"
163         elif [ ! -z "$bin_dir" -a -f $bin_dir/${hdw_arch}-${hdw_arch_opt}/$package/$package.tar.bz2 ] ; then
164                 echo "extracting $package to $inst_root ..."
165                 tar --use-compress-program=bzip2 \
166                 -xf $bin_dir/$package/$package.tar.bz2 -C $inst_root
167         elif [ ! -z "$net_addr" ] ; then
168                 echo "getting package $package via net ..."
169                 wget $net_addr/hdw-linux/hdw-linux-$hdw_version/binaries/${hdw_arch}-${hdw_arch_opt}/$package/${package}.tar.bz2 > /dev/null 2>&1
170                 retval=$?
171                 if [ "$retval" != "0" ] ; then
172                         echo "package not found at $net_addr, aborting ..."
173                         exit
174                 else
175                         echo "extracting $package to $inst_root ..."    
176                         tar --use-compress-program=bzip2 \
177                                 -xf ${package}.tar.bz2 -C $inst_root
178                         rm ${package}.tar.bz2
179                         echo "done"
180                 fi
181         else
182                 echo "sorry, package $package not found"
183                 exit 1
184         fi
185
186         # merging info/dir file
187         if [ "$pkg" != "00-dirtree" ] ; then
188         fl_file=`ls -A $inst_root/var/adm/flists/$pkg-[0-9]* | tail -1`
189         if [ -n "`grep usr/share/info/dir $fl_file`" ] ; then
190                 echo "" >> /tmp/.hdw-get/dir
191                 echo "$pkg info - added by hdw `date`" >> /tmp/.hdw-get/dir
192                 for info_obj in `grep 'usr/share/info/.*.info' $fl_file | \
193                                         awk -F/ '{ print $4 }'`; do
194                         info_o=`echo $info_obj | sed 's/.info//'`
195                         if [ -z "`grep \($info_o\)\. /tmp/.hdw-get/dir`" ]
196                         then
197                                 [ "$verbose" ] && \
198                                         echo "merging $info_o to info/dir"
199                                 grep \($info_o\)\. \
200                                         $inst_root/usr/share/info/dir >> \
201                                         /tmp/.hdw-get/dir
202                         fi
203                 done
204         fi
205         fi
206         [ -f /tmp/.hdw-get/dir ] && \
207                 cp /tmp/.hdw-get/dir $inst_root/usr/share/info/dir
208         rm -f /tmp/.hdw-get/dir
209
210         # check for runtime deps
211         #if [ -z $no_deps ] ; then
212         #       alldeps=`get_all_deps $pkg`
213         #       echo "$pkg depends on the following packages:"
214         #       for i in $alldeps; do
215         #               echo "-> $i"
216         #       done
217         #       echo "continue? [y,n]"
218         #       if [ -z $auto_resolve_deps ] ; then
219         #               read answer
220         #               if [ "$answer" = "y" ] ; then
221         #                       for i in $alldeps; do
222         #                               $0 -no-dep install $inst_root $pkg
223         #                       done
224         #               else
225         #                       echo -n "install dependencies on your own,"
226         #                       echo " things will not work without."
227         #               fi
228         #       fi
229         #fi
230                 
231 # dist-install
232 elif [ "$d_install" = "1" ] ; then
233         # check dist file
234         if [ ! -f $dist_file ] ; then
235                 echo "$dist_file not found, aborting"
236         else
237                 # install all distribution related packages
238                 for package in `cat $dist_file`; do
239                         # execute myelf
240                         $0 install $inst_root $package
241                 done
242         fi
243 fi
244
245 # remove temp directory
246 rm -rf /tmp/.hdw-get
247