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