]> pd.if.org Git - zpackage/blobdiff - zpm-pkg
let newpackage set additional fields
[zpackage] / zpm-pkg
diff --git a/zpm-pkg b/zpm-pkg
index 28a652cd12d06128c9b4c8081801626eff3b5630..56bd581e96f19fe292d4a4754609f648d7897da1 100755 (executable)
--- a/zpm-pkg
+++ b/zpm-pkg
 #!/bin/sh
 
-package=${1:-$ZPMPACKAGE}
-pkgver=${ZPMPACKAGEVER:-1.0}
-pkgrel=${ZPMPACKAGEREL:-1}
+# edit package metadata
 
-while getopts :n:v:r:d:a:u:l:p:b: opt; do
+#zpm pkg -s packager=foo zpm
+
+# zpm pkg -f <pkgfile> zpm packager=xyz
+# zpm pkg zpm packager
+
+warn() {
+       printf '%s:' "$0" 1>&2
+       printf ' %s' $* 1>&2
+       printf '\n' 1>&2
+       rv=1
+}
+
+die() {
+       printf '%s:' "$0" 1>&2
+       printf ' %s' $* 1>&2
+       printf '\n' 1>&2
+       exit 1
+}
+
+pkgfile=${ZPMDB:-/var/lib/zpm/local.db}
+
+while getopts f: opt; do
        case $opt in
-               n) pkgfile="$OPTARG" ;;
-               v) pkgver="$OPTARG" ;;
-               r) pkgrel="$OPTARG" ;;
-               d) description="$OPTARG" ;;
-               a) arch="$OPTARG" ;;
-               u) url="$OPTARG" ;;
-               l) licenses="$OPTARG" ;;
-               p) packager="$OPTARG" ;;
-               b) builddate="$OPTARG" ;;
+               f) pkgfile="$OPTARG" ;;
        esac
 done
+shift $((OPTIND - 1))
 
-pkgfile="$package-$pkgver-$pkgrel.zpm"
+set -e
 
 if [ ! -f $pkgfile ]; then
-       echo $pkgfile missing 1>&2
+       echo cannot find $pkgfile
        exit 1
 fi
 
-for path in $*; do
-hash=$(./zpm-addfile $pkgfile $path)
-
-sqlite3 $pkgfile <<EOS
-create table if not exists packagefiles (
-        package text,
-        subpackage      text, -- libs, dev, client, server, whatever
-       hash    text,
-        path    text,
-        filetype        text -- e.g. config, etc?
-);
-insert into packagefiles
-values ('$package', nullif('$subpackage', ''), '$hash', '$path', NULL)
-;
-EOS
+item=$1
+shift
+#printf "pkg: %s\n" $pkg
+pkgid=$(zpm findpkg -f $pkgfile "$item")
+
+if [ -z "$pkgid" ]; then
+       die "can't find pkgid for $item in $pkgfile"
+fi
+
+(
+rv=0
+
+{
+printf "begin;\n"
+while [ $# -gt 0 ]; do
+       item=$1
+       shift
+       show=0
+       case "$item" in
+               :*)
+                       pkgid=$(zpm findpkg -f $pkgfile "${item#:}")
+                       continue
+                       ;;
+               *=*)
+                       field=${item%%=*}
+                       value=${item#*=}
+                       ;;
+               *=)
+                       field=${item%%=*}
+                       value=
+                       ;;
+               *)
+               field=$item
+               show=1
+               ;;
+       esac
+
+       if [ -z "$pkgid" ]; then
+               warn "can't find pkgid for $item in $pkgfile"
+               break;
+       fi
+
+       vfield=$(zpm quote "$field")
+
+       hasfield=$(zpm shell $pkgfile "select name from pragma_table_info('packages') where name = '$vfield';")
+       if [ -z "$hasfield" ]; then
+               warn "$field is not a valid package field"
+               break
+       fi
+
+       field=$(zpm quote -i "$field")
+
+       if [ $show -eq 1 ]; then
+               printf "select %s from packages_pkgid where pkgid = '%s';\n" "$field" "$pkgid"
+               continue
+       fi
+
+       if [ -z "$value" ]; then
+               value=NULL
+       else
+               value=$(zpm quote -q "$value")
+       fi
+
+       printf "update packages_pkgid set %s = %s where pkgid = '$pkgid';\n" $field "$value"
 done
+
+if [ $rv -eq 0 ]; then
+       printf "commit;\n"
+else
+       printf "rollback;\n"
+fi
+
+} | zpm shell $pkgfile
+exit $rv
+)