#!/bin/sh
-pkgfile=$1
+pkgroot=/
+
+while getopts :f:v:r:d:a:u:l:p:b:P:R: opt; do
+ case $opt in
+ R) pkgroot="$OPTARG" ;;
+ f) 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" ;;
+ P) prefix="$OPTARG" ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+die() {
+ echo $* 1&>2
+ exit 1
+}
+
+pkgfileinfo() {
+ sqlite3 $pkgfile<<EOS
+select $1 from packagefiles
+ where path='$2'
+ and package||'-'||version||'-'||release = '$pkg'
+ ;
+EOS
+}
+
+# if not from a package file, from local db
+if [ -z "$pkgfile" ]; then
+ die "installing from non-pkgfile not supported"
+fi
+
+pkg="$1"
shift
+set -e
for path in $*; do
- mkdir -p tmp/$(dirname $path)
- fhash=$(sqlite3 $pkgfile "select hash from packagefiles where path = '$path';")
- ./zpm-extract $pkgfile $fhash ./tmp/$path
+ fhash=$(pkgfileinfo hash $path)
+ mode=$(pkgfileinfo mode $path)
+ owner=$(pkgfileinfo username $path)
+ group=$(pkgfileinfo groupname $path)
+ ftype=$(pkgfileinfo filetype $path)
+ name=$(basename -- $path)
+
+ if [ "$pkgroot" != '/' ]; then
+ dir=$pkgroot/$(dirname -- $path)
+ dir=${dir%/.}
+ if [ "$dir" = "//" ]; then dir=/ ; fi
+ else
+ dir=''
+ fi
+
+ tmppath=$dir/.installing.$name
+
+ cat <<-EOC
+ mkdir -p $dir
+ zpm extract $pkgfile $fhash $tmppath 0
+ chown $owner:$group $tmppath
+ chmod $mode $tmppath
+ mv $tmppath $dir/$name
+ EOC
done