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