--- /dev/null
+#!/bin/sh
+
+# zpm add -f pkgfile pkgid [ files ]
+
+# no package file? take from ZPM_PACKAGE_FILE
+# no pkgid ? take from ZPM_PACKAGE_ID
+# no id impossible
+
+die() {
+ echo $* 1>&2
+ exit 1
+}
+
+while getopts :f: opt; do
+ case $opt in
+ f) pkgfile="$OPTARG" ;;
+ *) echo 'zpm-setscript unknown option' $opt; exit 1 ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+pkgid=$1
+shift
+
+if [ -z "$pkgid" ]; then
+ die "must specify pkgid"
+fi
+
+eval "$(zpm parse -E $pkgid)"
+
+if [ -z "$pkgfile" ]; then
+ pkgfile=$ZPM_PACKAGE_FILE
+fi
+
+# cases R = full package id, F = specified package file
+
+# immediate error
+# --- 000 error, must specify something
+if [ -z "$release" ] && [ -z "$pkgfile" ]; then
+ die must specify package file or complete package id
+fi
+
+# try to get from package file
+if [ -z "$release" ]; then
+ pkgid=$(zpm findpkg $pkgfile $pkgid)
+ if [ -z "$pkgid" ]; then
+ die cannot find package id
+ fi
+ eval "$(zpm parse -E $pkgid)"
+fi
+
+# --F 001 error, wouldn't know which pkgid to create, could derive from file?
+if [ -z "$release" ]; then
+ die must specify complete package id
+fi
+
+#echo "have '$pkgid' '$pkgfile'"
+
+# set file from pkgid
+# -R- 010 set file from pkgid, create in file, error if no file
+if [ -z "$pkgfile" ]; then
+ pkgfile="$pkgid.zpm"
+fi
+
+# will now be one of these
+# -RF 011 create package in file, error if file doesn't exist
+if [ ! -f "$pkgfile" ]; then
+ die $pkgfile does not exist
+fi
+
+set -e
+
+if [ "$idempotent" = 1 ]; then
+ idempotent='or ignore'
+fi
+
+package=$(zpm quote "$name")
+pkgver=$(zpm quote "$version")
+pkgrel=$(zpm quote "$release")
+
+# args are stage and script
+
+addscript() (
+ stage=$1
+ path=$2
+
+ #echo adding $path
+ if [ ! -f "$script" ]; then
+ die "$script is not a regular file"
+ fi
+ hash=$(zpm addfile $pkgfile "$path")
+ if [ $? -ne 0 ]; then
+ die "zpm addfile failed ($?): $pkgfile $path"
+ fi
+ #echo adding script $hash $path
+ hash=$(zpm quote "$hash")
+
+ stage=$(zpm quote "$stage")
+
+ #cat <<EOS
+ zpm shell $pkgfile <<EOS
+PRAGMA foreign_keys = ON;
+begin;
+insert or replace into scripts (package,version,release,stage,hash)
+values ('$package', '$pkgver', $pkgrel, '$stage', '$hash');
+commit;
+EOS
+
+#printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
+#printf "%s\n" $path
+)
+
+removescript() (
+ stage=$1
+ stage=$(zpm quote "$stage")
+
+ #cat <<EOS
+ zpm shell $pkgfile <<EOS
+PRAGMA foreign_keys = ON;
+begin;
+delete from scripts
+where package = '$package' and version = '$pkgver' and release = $pkgrel
+and stage = '$stage'
+;
+commit;
+EOS
+
+#printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
+#printf "%s\n" $path
+)
+
+# TODO better syntax for showing the script
+# TODO a way to set the script hash directly
+while [ $# -gt 1 ]; do
+ stage=$1
+ script=$2
+ shift 2
+
+ if [ "$script" = '-' ]; then
+ removescript "$stage"
+ elif [ -z "$script" ]; then
+ showscript "$stage"
+ else
+ addscript "$stage" "$script"
+ fi
+
+done