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