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