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