]> pd.if.org Git - zpackage/blob - zpm-install
fix status after dry run
[zpackage] / zpm-install
1 #!/bin/sh
2
3 warn() {
4         echo $* 1>&2
5 }
6
7 die() {
8         echo $* 1>&2
9         zpm log -i -a 'aborting install' -t "$pkgid" "$*"
10         exit 1
11 }
12
13 dryrun=0
14 verbose=0
15 runscripts=1
16 runconfigure=1
17 absorb=0
18 overwrite=0
19 syncopts=''
20 installall=0
21 mergefiles=0
22
23 # zpm-install [-SCn] [ -d localdb ] [ -f pkgfile ] [ -R installroot ] pkgstr ...
24 while getopts f:d:R:nSCvAOaF opt; do
25         case $opt in
26                 f) pkgfile="$OPTARG" ;;
27                 F) mergefiles=1 ;;
28                 d) localdb="$OPTARG" ;;
29                 R) rootdir="$OPTARG" ;;
30                 S) runscripts=0 ;;
31                 C) runconfigure=0 ;;
32                 A) absorb=1; syncopts="${syncopts} -A" ;;
33                 a) installall=1; ;;
34                 O) overwrite=1; syncopts="${syncopts} -O" ;;
35                 n) dryrun=1 ;;
36                 v) verbose=1 ;;
37                 *) die "usage ..." ;;
38         esac
39 done
40 shift $(( OPTIND - 1))
41
42 if [ -z "$1" ]; then
43         installall=1
44 fi
45
46 if [ -z "$pkgfile" ]; then
47         pkgfile=$ZPM_PACKAGE_FILE
48 fi
49
50 if [ $installall -eq 1 ]; then
51         pkglist="$(zpm list -f $pkgfile)"
52 else
53         pkglist=$(zpm findpkg -f $pkgfile "$1")
54 fi
55
56 if [ $verbose -gt 0 ]; then
57         echo $pkglist
58 fi
59
60 if [ -z "$pkglist" ]; then
61         die "$0 no packages specified for install"
62 fi
63
64 if [ "$idempotent" = 1 ]; then
65         idempotent='or ignore'
66 fi
67
68 if [ -n "$rootdir" ]; then
69         : rootdir=${rootdir%%/}
70 fi
71
72 if [ -z "$localdb" ]; then
73         localdb=${rootdir}/var/lib/zpm/local.db
74 fi
75
76 if [ ! -f "$localdb" ]; then
77         if [ -n "$rootdir" ] && [ ! -d $rootdir ]; then
78                 mkdir $rootdir || die "can't create $rootdir: $!"
79         fi
80         for d in /var /var/lib /var/lib/zpm; do
81                 test -d $rootdir/$d || mkdir $rootdir/$d || die "can't create $rootdir/$d/: $!"
82         done
83         zpm init "$localdb"
84         if [ $? -ne 0 ]; then
85                 die "aborting install"
86         fi
87 fi
88
89 ZPMDB=$localdb
90 export ZPMDB
91
92 if [ -z "$ZPMDB" ]; then
93         die "no local db"
94 else
95         #echo "localdb = $ZPMDB"
96         true
97 fi
98
99 zpm test -v "$ZPMDB" || die "$ZPMDB is not a zpm database"
100
101 # check if we're installing something already
102 var=$(zpm list -f $localdb -s installing | wc -l)
103 if [ $var -gt 0 ]; then
104         zpm list -v -f $localdb -s installing 
105         die "already ($localdb) installing $var package(s)"
106 fi
107 # check if we're removing something already
108 var=$(zpm list -f $localdb -s removing | wc -l)
109 if [ $var -gt 0 ]; then
110         zpm list -v -f $localdb -s removing 
111         die "already ($localdb) removing $var package(s)"
112 fi
113 var=$(zpm list -f $localdb -s updating | wc -l)
114 if [ $var -gt 0 ]; then
115         zpm list -v -f $localdb -s updating 
116         die "already ($localdb) updating $var package(s)"
117 fi
118
119 if [ -n "$rootdir" ]; then
120         ZPM_ROOT_DIR="$rootdir"
121         export ZPM_ROOT_DIR
122 fi
123
124 for pkgid in $pkglist; do
125         curstatus=$(zpm pkg $pkgid status)
126         if [ "$curstatus" = 'installed' ]; then
127                 die "$pkgid already installed"
128         fi
129
130         package=$(zpm parse -n $pkgid)
131         current=$(zpm list -s installed "$package")
132         
133         if [ $runscripts -gt 0 ]; then
134                 # TODO run pre-upgrade script if needed
135                 # zpm runscript -p pre-upgrade $current $pkgid
136                 zpm script -f $pkgfile -p pre-install $pkgid $current
137                 if [ $? -ne 0 ]; then
138                         die "pre-install script for $pkgid failed"
139                 fi
140         else
141                 if zpm script -f $pkgfile -Fql -p pre-install $pkgid; then
142                         zpm note -p $pkgid -m 'pre-install script not run'
143                 fi
144         fi
145
146         # remove the package if it exists.  can't be in an installed
147         # state, would have bailed above.  So, if it exists, can only
148         # be 'upgraded'.  This should be fast, because we won't run
149         # a garbage collect, so any possible duplicate files between
150         # an upgraded or removed files, if they're there, won't need
151         # to be merged.
152
153         # only merge if localdb and pkgfile are different
154         if [ "$pkgfile" != "$ZPMDB" ]; then
155                 zpm rmpackage $pkgid
156                 if [ $verbose -gt 0 ]; then
157                         echo merging $pkgid
158                 fi
159                 if [ $mergefiles -eq 1 ]; then
160                         zpm merge -u -F -f $pkgfile -s installing $pkgid
161                 else
162                         zpm merge -u -f $pkgfile -s installing $pkgid
163                 fi
164                 if [ $? -ne 0 ]; then
165                         die "merging $pkgid failed"
166                 fi
167         else
168                 zpm pkg $pkgid status=installing
169         fi
170 done
171
172 # do the sync
173
174 #zpm shell $ZPMDB 'select * from install_status' 1>&2
175 if [ $dryrun -gt 0 ]; then
176         syncopts="$syncopts -n"
177 fi
178
179 if [ $verbose -gt 0 ]; then
180         syncopts="$syncopts -v"
181 fi
182
183 if [ "$pkgfile" != "$ZPMDB" ]; then
184         syncopts="$syncopts -f $pkgfile"
185 fi
186
187 zpm syncfs $syncopts
188
189 syncrv=$?
190
191 if [ $dryrun -gt 0 ]; then
192         for pkgid in $pkglist; do
193                 zpm pkg $pkgid status=dryrun
194         done
195 fi
196
197 if [ $syncrv -ne 0 ]; then
198         zpm pkg $pkgid status=failed
199         die 'zpm-syncfs failed';
200 fi
201
202 if [ $(id -u) -eq 0 ]; then
203         if [ ! -d $rootdir/etc ]; then
204                 warn "no etc directory in $rootdir, skipping ldconfig"
205         elif [ -f $rootdir/sbin/ldconfig ]; then
206                 $rootdir/sbin/ldconfig -r ${rootdir:-/}
207         elif [ -f /sbin/ldconfig ]; then
208                 /sbin/ldconfig -r ${rootdir:-/}
209         else
210                 true
211         fi
212 fi
213
214 for pkgid in $pkglist; do
215         package=$(zpm parse -n $pkgid)
216         current=$(zpm list -s installed "$package")
217         
218         if [ $runscripts -gt 0 ]; then
219                 zpm script -f $pkgfile -p post-install $pkgid $current
220         else
221                 if zpm script -f $pkgfile -Fql -p post-install $pkgid; then
222                         zpm note -p $pkgid -m 'post-install script not run'
223                 fi
224         fi
225
226         if [ -n "$current" ]; then
227                 zpm pkg $pkgid status=installed :$current status=updated
228         else
229                 zpm pkg $pkgid status=installed
230         fi
231
232         # TODO skip configure if not on a terminal, regardless of settings
233         # TODO will need force option
234         if [ $runconfigure -gt 0 ]; then
235                 zpm script -f $pkgfile -p configure $pkgid
236         else
237                 if zpm script -f $pkgfile -Fql -p configure $pkgid; then
238                         zpm note -p $pkgid -m 'configure script not run'
239                 fi
240         fi
241
242 done