]> pd.if.org Git - zpackage/blob - zpm-install
rewrite zpm-install
[zpackage] / zpm-install
1 #!/bin/sh
2
3 die() {
4         echo $* 1>&2
5         exit 1
6 }
7
8 dryrun=0
9 runscripts=1
10 runconfigure=1
11 localdb=/var/lib/zpm/local.db
12
13 # zpm-install [-SCn] [ -d localdb ] [ -f pkgfile ] [ -R installroot ] pkgstr ...
14 while getopts :f:d:R:nSC opt; do
15         case $opt in
16                 f) pkgfile="$OPTARG" ;;
17                 d) localdb="$OPTARG" ;;
18                 R) rootdir="$OPTARG" ;;
19                 S) runscripts=0 ;;
20                 C) runconfigure=0 ;;
21                 n) dryrun=1
22         esac
23 done
24 shift $(( OPTIND - 1))
25
26 pkgid=$1
27
28 if [ -z "$pkgid" ]; then
29         die "must specify pkgid"
30 fi
31
32 eval "$(zpm parse -E $pkgid)"
33
34 if [ -z "$pkgfile" ]; then
35         pkgfile=$ZPM_PACKAGE_FILE
36 fi
37
38 # cases C = create ok, R = full package id, F = specified package file
39
40 # immediate error
41 # C-- 100 error, must specify something
42 # --- 000 error, must specify something
43 if [ -z "$release" ] && [ -z "$pkgfile" ]; then
44         die must specify package file or complete package id
45 fi
46
47 # --F 001 error, wouldn't know which pkgid to create, could derive from file?
48 # C-F 101 error, since package wouldn't exist in file to find
49 if [ -z "$release" ]; then
50         die must specify complete package id
51 fi
52
53 # set file from pkgid
54 # CR- 110 set file from pkgid, create if needed
55 # -R- 010 set file from pkgid, create in file, error if no file
56 if [ -z "$pkgfile" ]; then
57         pkgfile="$pkgid.zpm"
58 fi
59
60 # will now be one of these
61 # CRF 111 create package in file given, create file if needed
62 # -RF 011 create package in file, error if file doesn't exist
63 if [ ! -f "$pkgfile" ]; then
64         if [ $create -eq 1 ]; then
65                 zpm init $pkgfile
66         else
67                 die $pkgfile does not exist
68         fi
69 fi
70
71 set -e
72
73 if [ "$idempotent" = 1 ]; then
74         idempotent='or ignore'
75 fi
76
77 package=$(zpm quote "$name")
78 pkgver=$(zpm quote "$version")
79 pkgrel=$(zpm quote "$release")
80
81 ZPMDB=$localdb
82 export ZPMDB
83
84 # check if we're installing something already
85 var=$(zpm list -f $localdb -s installing | wc -l)
86 if [ $var -ge 0 ]; then
87         die 'already installing'
88 fi
89
90 if [ -n "$rootdir" ];
91         ZPM_ROOT_DIR="$rootdir"
92         export ZPM_ROOT_DIR
93 fi
94
95 # TODO mark already installed packages as updating?
96 for pkgstr in "$@"; do
97         pkgid=$(zpm findpkg -f $pkgfile $pkgstr)
98         if [ $? -ne 0 ]; then
99                 # TODO log
100                 die "can't find package $pkgstr in $pkgfile"
101         fi
102
103         curstatus=$(zpm pkg $pkgid status)
104         if [ "$curstatus" = 'installed' ]; then
105                 die "$pkgid already installed"
106         fi
107
108         eval $(zpm parse -E $pkgid)
109
110         current=$(zpm list -s installed "$package")
111         
112         if [ $runscripts -gt 0 ]; then
113                 zpm runscript -f $packagefile -p pre-install $pkgid $current
114                 if [ $? -ne 0 ]; then
115                         # TODO log
116                         die "pre-install script for $pkgid failed"
117                 fi
118         fi
119
120         # only merge if localdb and pkgfile are different
121         if [ "$pkgfile" != "$ZPMDB" ]; then
122                 zpm merge -f $pkgfile -s installing $pkgid
123                 if [ $? -ne 0 ]; then
124                         # TODO log
125                         die "merging $pkgid failed"
126                 fi
127         fi
128
129         zpm pkgfiles -f $pkgfile $pkgid
130         if [ $runscripts -gt 0 ]; then
131                 zpm runscript -f $pkgfile -p post-install $pkgid $current
132         fi
133         # need a way to update multiple statuses all in one
134         # zpm pkgstatus $pkgid installed $pkgid updated
135         zpm pkg $pkgid status installed
136         if [ $runconfigure -gt 0 ]; then
137                 zpm runscript -f $pkgfile -p configure $pkgid $current
138         fi
139 done