]> pd.if.org Git - zpackage/blob - zpm-pathmod
add where clause argument to findpkg
[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 while getopts f: opt; do
28         case $opt in
29                 f) pkgfile="$OPTARG" ;;
30         esac
31 done
32 shift $((OPTIND - 1))
33
34 set -e
35
36 if [ ! -f $pkgfile ]; then
37         echo cannot find $pkgfile
38         exit 1
39 fi
40
41 item=$1
42 shift
43 #printf "pkg: %s\n" $pkg
44 pkgid=$(zpm findpkg -f $pkgfile "$item")
45
46 if [ -z "$pkgid" ]; then
47         die "can't find pkgid for $item in $pkgfile"
48 fi
49
50 path=$1
51 shift
52 path=$(zpm quote "$path")
53
54 (
55 rv=0
56
57 {
58 printf "begin;\n"
59 while [ $# -gt 0 ]; do
60         item=$1
61         shift
62         show=0
63         case "$item" in
64                 :*)
65                         pkgid=$(zpm findpkg -f $pkgfile "${item#:}")
66                         continue
67                         ;;
68                 +*)
69                         path="${item#+}"
70                         path=$(zpm quote "$path")
71                         continue
72                         ;;
73                 *=*)
74                         field=${item%%=*}
75                         value=${item#*=}
76                         ;;
77                 *=)
78                         field=${item%%=*}
79                         value=
80                         ;;
81                 *)
82                 field=$item
83                 show=1
84                 ;;
85         esac
86
87         # TODO if field = pkgid, parse and set
88         # TODO if changing pkgid, and it doesn't exist, better error
89         # message
90
91         if [ -z "$pkgid" ]; then
92                 warn "can't find pkgid for $item in $pkgfile"
93                 break;
94         fi
95
96         if [ $field = delete ]; then
97                 printf "delete from packagefiles_pkgid where pkgid = '%s' and path = '%s';\n" "$pkgid" "$path"
98         else
99                 vfield=$(zpm quote "$field")
100
101                 hasfield=$(zpm shell $pkgfile "select name from pragma_table_info('packagefiles') where name = '$vfield';")
102                 if [ -z "$hasfield" ]; then
103                         warn "$field is not a valid packagefile field"
104                         break
105                 fi
106
107                 field=$(zpm quote -i "$field")
108
109                 if [ $show -eq 1 ]; then
110                         printf "select %s from packagefiles_pkgid where pkgid = '%s' and path='$path';\n" "$field" "$pkgid"
111                         continue
112                 fi
113
114                 if [ -z "$value" ]; then
115                         value=NULL
116                 else
117                         value=$(zpm quote -q "$value")
118                 fi
119
120                 printf "update packagefiles_pkgid set %s = %s where pkgid = '$pkgid' and path='$path';\n" $field $value
121         fi
122 done
123
124 if [ $rv -eq 0 ]; then
125         printf "commit;\n"
126 else
127         printf "rollback;\n"
128 fi
129
130 } | zpm shell $pkgfile
131 exit $rv
132 )