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