]> pd.if.org Git - zpackage/blob - zpm-install
create rootdir on install
[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 [ -n "$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         if [ -n "$rootdir" ] && [ ! -d $rootdir ]; then
101                 mkdir $rootdir || die "can't create $rootdir: $!"
102         fi
103         for d in /var /var/lib /var/lib/zpm; do
104                 test -d $rootdir/$d || mkdir $rootdir/$d || die "can't create $rootdir/$d/: $!"
105         done
106         zpm init "$localdb"
107         if [ $? -ne 0 ]; then
108                 die "aborting install"
109         fi
110 fi
111
112 ZPMDB=$localdb
113 export ZPMDB
114
115 if [ -z "$ZPMDB" ]; then
116         die "no local db"
117 else
118         #echo "localdb = $ZPMDB"
119         true
120 fi
121
122 zpm test -v "$ZPMDB" || die "$ZPMDB is not a zpm database"
123
124 # check if we're installing something already
125 var=$(zpm list -f $localdb -s installing | wc -l)
126 if [ $var -gt 0 ]; then
127         zpm list -v -f $localdb -s installing 
128         die "already ($localdb) installing $var package(s)"
129 fi
130 # check if we're installing something already
131 var=$(zpm list -f $localdb -s removing | wc -l)
132 if [ $var -gt 0 ]; then
133         zpm list -v -f $localdb -s removing 
134         die "already ($localdb) removing $var package(s)"
135 fi
136 var=$(zpm list -f $localdb -s updating | wc -l)
137 if [ $var -gt 0 ]; then
138         zpm list -v -f $localdb -s updating 
139         die "already ($localdb) updating $var package(s)"
140 fi
141
142 if [ -n "$rootdir" ]; then
143         ZPM_ROOT_DIR="$rootdir"
144         export ZPM_ROOT_DIR
145 fi
146
147 # TODO mark already installed packages as updating?
148 for pkgstr in "$@"; do
149         pkgid=$(zpm findpkg -f $pkgfile $pkgstr)
150         if [ $? -ne 0 ]; then
151                 # TODO log
152                 die "can't find package $pkgstr in $pkgfile"
153         fi
154
155         curstatus=$(zpm pkg $pkgid status)
156         if [ "$curstatus" = 'installed' ]; then
157                 die "$pkgid already installed"
158         fi
159
160         eval $(zpm parse -E $pkgid)
161
162         #zpm list -v
163         current=$(zpm list -s installed "$package")
164         
165         if [ $runscripts -gt 0 ]; then
166                 # TODO run pre-upgrade script if needed
167                 # zpm runscript -p pre-upgrade $current $pkgid
168                 zpm runscript -f $pkgfile -p pre-install $pkgid $current
169                 if [ $? -ne 0 ]; then
170                         # TODO log
171                         die "pre-install script for $pkgid failed"
172                 fi
173         fi
174
175         # remove the package if it exists.  can't be in an installed
176         # state, would have bailed above.  So, if it exists, can only
177         # be 'upgraded'.  This should be fast, because we won't run
178         # a garbage collect, so any possible duplicate files between
179         # an upgraded or removed files, if they're there, won't need
180         # to be merged.
181
182         # only merge if localdb and pkgfile are different
183         if [ "$pkgfile" != "$ZPMDB" ]; then
184                 zpm rmpackage $pkgid
185                 zpm merge -f $pkgfile -s installing $pkgid
186                 if [ $? -ne 0 ]; then
187                         # TODO log
188                         die "merging $pkgid failed"
189                 fi
190         fi
191         # TODO but need to mark as installing if not merged
192
193         #zpm shell $ZPMDB 'select * from install_status' 1>&2
194         if [ $dryrun -gt 0 ]; then
195                 #zpm list -v
196                 #zpm shell $ZPMDB 'select * from install_status'
197                 zpm syncfs $syncopts -nv -f $pkgfile
198                 zpm pkg $pkgid status=dryrun
199                 continue
200         fi
201
202         if [ $verbose -gt 0 ]; then
203                 syncopts="${syncopts} -v"
204         fi
205
206         zpm syncfs $syncopts -f $pkgfile
207
208         if [ $? -ne 0 ]; then
209                 die 'zpm-syncfs failed';
210         fi
211
212         if [ $runscripts -gt 0 ]; then
213                 zpm runscript -f $pkgfile -p post-install $pkgid $current
214         fi
215
216         if [ -n "$current" ]; then
217                 zpm pkg $pkgid status=installed :$current status=updated
218         else
219                 zpm pkg $pkgid status=installed
220         fi
221
222         if [ $(id -u) -eq 0 ]; then
223                 if [ ! -d $rootdir/etc ]; then
224                         warn "no etc directory in $rootdir, skipping ldconfig"
225                 elif [ -f $rootdir/sbin/ldconfig ]; then
226                         $rootdir/sbin/ldconfig -r ${rootdir:-/}
227                 elif [ -f /sbin/ldconfig ]; then
228                         /sbin/ldconfig -r ${rootdir:-/}
229                 else
230                         true
231                 fi
232         fi
233
234         # TODO skip configure if not on a terminal, regardless of settings
235         # TODO will need force option
236         if [ $runconfigure -gt 0 ]; then
237                 zpm runscript -f $pkgfile -p configure $pkgid $current
238         fi
239 done