]> pd.if.org Git - zpackage/blob - zpm-pathmod
cleanup foreach-path
[zpackage] / zpm-pathmod
1 #!/bin/sh
2
3 # edit package metadata
4
5 #zpm pkg -s packager=foo zpm
6
7 # zpm pkg -f <pkgfile> zpm path packager=xyz
8 # zpm pkg zpm packager zyz
9 # zpm pkg zpm packager
10
11 warn() {
12         printf '%s:' "$0" 1>&2
13         printf ' %s' $* 1>&2
14         printf '\n' 1>&2
15         rv=1
16 }
17
18 die() {
19         printf '%s:' "$0" 1>&2
20         printf ' %s' $* 1>&2
21         printf '\n' 1>&2
22         exit 1
23 }
24
25 pkgfile=${ZPMDB:-/var/lib/zpm/db.zpm}
26
27 pathglob=0
28
29 while getopts f:g opt; do
30         case $opt in
31                 f) pkgfile="$OPTARG" ;;
32                 g) pathglob=1 ;;
33         esac
34 done
35 shift $((OPTIND - 1))
36
37 if [ $pathglob -eq 1 ]; then
38         pathop='glob'
39 else
40         pathop='='
41 fi
42
43 set -e
44
45 if [ ! -f $pkgfile ]; then
46         echo cannot find $pkgfile
47         exit 1
48 fi
49
50 item=$1
51 shift
52 #printf "pkg: %s\n" $pkg
53 pkgid=$(zpm findpkg -f $pkgfile "$item")
54
55 if [ -z "$pkgid" ]; then
56         die "can't find pkgid for $item in $pkgfile"
57 fi
58
59 path=$1
60 shift
61 path=$(zpm quote "$path")
62
63 (
64 rv=0
65
66 {
67 printf "begin;\n"
68 while [ $# -gt 0 ]; do
69         item=$1
70         shift
71         show=0
72         case "$item" in
73                 :*)
74                         pkgid=$(zpm findpkg -f $pkgfile "${item#:}")
75                         continue
76                         ;;
77                 +*)
78                         path="${item#+}"
79                         path=$(zpm quote "$path")
80                         continue
81                         ;;
82                 *=*)
83                         field=${item%%=*}
84                         value=${item#*=}
85                         ;;
86                 *=)
87                         field=${item%%=*}
88                         value=
89                         ;;
90                 *)
91                 field=$item
92                 show=1
93                 ;;
94         esac
95
96         # TODO if field = pkgid, parse and set
97         # TODO if changing pkgid, and it doesn't exist, better error
98         # message
99
100         if [ -z "$pkgid" ]; then
101                 warn "can't find pkgid for $item in $pkgfile"
102                 break;
103         fi
104
105         if [ $field = delete ]; then
106                 printf "delete from packagefiles_pkgid where pkgid = '%s' and path $pathop '%s';\n" "$pkgid" "$path"
107         else
108                 vfield=$(zpm quote "$field")
109
110                 hasfield=$(zpm shell $pkgfile "select name from pragma_table_info('packagefiles') where name = '$vfield';")
111                 if [ -z "$hasfield" ]; then
112                         warn "$field is not a valid packagefile field"
113                         break
114                 fi
115
116                 field=$(zpm quote -i "$field")
117
118                 if [ $show -eq 1 ]; then
119                         printf "select %s from packagefiles_pkgid where pkgid = '%s' and path $pathop '$path';\n" "$field" "$pkgid"
120                         continue
121                 fi
122
123                 if [ -z "$value" ]; then
124                         value=NULL
125                 else
126                         value=$(zpm quote -q "$value")
127                 fi
128
129                 printf "update packagefiles_pkgid set %s = %s where pkgid = '$pkgid' and path $pathop '$path';\n" $field $value
130         fi
131 done
132
133 if [ $rv -eq 0 ]; then
134         printf "commit;\n"
135 else
136         printf "rollback;\n"
137 fi
138
139 } | zpm shell $pkgfile
140 exit $rv
141 )