]> pd.if.org Git - zpackage/blob - zpm-setscript
add gc script
[zpackage] / zpm-setscript
1 #!/bin/sh
2
3 # zpm add -f pkgfile pkgid [ files ]
4
5 # no package file? take from ZPM_PACKAGE_FILE
6 # no pkgid ? take from ZPM_PACKAGE_ID
7 # no id impossible
8
9 die() {
10         echo $* 1>&2
11         exit 1
12 }
13
14 while getopts :f: opt; do
15         case $opt in
16                 f) pkgfile="$OPTARG" ;;
17                 *) echo 'zpm-setscript unknown option' $opt; exit 1 ;;
18         esac
19 done
20 shift $((OPTIND - 1))
21
22 pkgid=$1
23 shift
24
25 if [ -z "$pkgid" ]; then
26         die "must specify pkgid"
27 fi
28
29 eval "$(zpm parse -E $pkgid)"
30
31 if [ -z "$pkgfile" ]; then
32         pkgfile=$ZPM_PACKAGE_FILE
33 fi
34
35 # cases R = full package id, F = specified package file
36
37 # immediate error
38 # --- 000 error, must specify something
39 if [ -z "$release" ] && [ -z "$pkgfile" ]; then
40         die must specify package file or complete package id
41 fi
42
43 # try to get from package file
44 if [ -z "$release" ]; then
45         pkgid=$(zpm findpkg -f $pkgfile $pkgid)
46         if [ -z "$pkgid" ]; then
47                 die cannot find package id
48         fi
49         eval "$(zpm parse -E $pkgid)"
50 fi
51
52 # --F 001 error, wouldn't know which pkgid to create, could derive from file?
53 if [ -z "$release" ]; then
54         die must specify complete package id
55 fi
56
57 #echo "have '$pkgid' '$pkgfile'"
58
59 # set file from pkgid
60 # -R- 010 set file from pkgid, create in file, error if no file
61 if [ -z "$pkgfile" ]; then
62         pkgfile="$pkgid.zpm"
63 fi
64
65 # will now be one of these
66 # -RF 011 create package in file, error if file doesn't exist
67 if [ ! -f "$pkgfile" ]; then
68         die $pkgfile does not exist
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 # args are stage and script
82
83 addscript() (
84         stage=$1
85         path=$2
86
87         #echo adding $path
88         if [ ! -f "$script" ]; then
89                 die "$script is not a regular file"
90         fi
91         hash=$(zpm addfile $pkgfile "$path")
92         if [ $? -ne 0 ]; then
93                 die "zpm addfile failed ($?): $pkgfile $path" 
94         fi
95         #echo adding script $hash $path
96         hash=$(zpm quote "$hash")
97
98         stage=$(zpm quote "$stage")
99
100         #cat <<EOS
101         zpm shell $pkgfile <<EOS
102 PRAGMA foreign_keys = ON;
103 begin;
104 insert or replace into scripts (package,version,release,stage,hash)
105 values ('$package', '$pkgver', $pkgrel, '$stage', '$hash');
106 commit;
107 EOS
108
109 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
110 #printf "%s\n" $path
111 )
112
113 removescript() (
114         stage=$1
115         stage=$(zpm quote "$stage")
116
117         #cat <<EOS
118         zpm shell $pkgfile <<EOS
119 PRAGMA foreign_keys = ON;
120 begin;
121 delete from scripts 
122 where package = '$package' and version = '$pkgver' and release = $pkgrel
123 and stage = '$stage'
124 ;
125 commit;
126 EOS
127
128 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
129 #printf "%s\n" $path
130 )
131
132 showscript() (
133         stage=$1
134         stage=$(zpm quote "$stage")
135         hash=$(zpm shell $pkgfile "select hash from scripts_pkgid
136         where pkgid = '$pkgid' and stage = '$stage'")
137         if [ -z "$hash" ]; then
138                 die "no script for $stage $pkgid"
139         fi
140         zpm extract $pkgfile "$hash" -
141 )
142
143 # TODO better syntax for showing the script
144 # TODO a way to set the script hash directly
145 while [ $# -gt 1 ]; do
146         stage=$1
147         script=$2
148         shift 2
149
150         if [ "$script" = '--' ]; then
151                 removescript "$stage"
152         elif [ "$script" = '-' ]; then
153                 showscript "$stage"
154         else
155                 addscript "$stage" "$script"
156         fi
157
158 done