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