#!/bin/sh
-pkgfile=$1
+# install a file from a package
+# -f pkgfile
+# -R root of pkg
+# -P prefix to file
+# -S strip prefix
+# -p pkgname
+
+pkgroot=/
+
+while getopts :f:v:r:d:a:u:g:l:p:b:P:R: opt; do
+ case $opt in
+ R) pkgroot="$OPTARG" ;;
+ f) pkgfile="$OPTARG" ;;
+ v) pkgver="$OPTARG" ;;
+ r) pkgrel="$OPTARG" ;;
+ P) prefix="$OPTARG" ;;
+ u) owner="$OPTARG" ;;
+ g) group="$OPTARG" ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+die() {
+ echo $* 1>&2
+ exit 1
+}
+
+pkgfileinfo() {
+ zpm shell $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
-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
+ZPMPKGFILE=$pkgfile
+export ZPMPKGFILE
+
+set -e
+for path in "$@"; do
+ name=$(basename -- "$path")
+ dir=$(dirname -- "$path")
+ dir=${dir#/}
+
+ if [ "$pkgroot" != '/' ]; then
+ dir=$pkgroot/$dir
+ dir=${dir%/.}
+# if [ "$dir" = "//" ]; then dir=/ ; fi
+ else
+ dir="/$dir"
+ fi
+
+ # TODO generate some unique hash
+ tmppath=$dir/.installing.$name
+
+ printf '%s installing %s\n' $pkg $dir/$name
+
+ fhash=$(pkgfileinfo hash $path)
+ mode=$(pkgfileinfo mode $path)
+ ftype=$(pkgfileinfo filetype $path)
+ : ${owner:=$(pkgfileinfo username $path)}
+ : ${group:=$(pkgfileinfo groupname $path)}
+
+ # TODO if the file already exists, and is the correct
+ # hash or directory, do nothing
+
+ # can't use install because it's not posix.
+ # probably worth writing a zpm-install, but with
+ # a different name since that wants to be a package install
+ # probably makes the most sense for extract to know how
+ # to do the extract to temp and such, then
+ # you can just add the -S option (secure)
+ #cat <<-EOC
+ mkdir -p $dir
+ umask 0700
+ case $ftype in
+ r*) zpm extract $pkgfile $fhash $tmppath
+ chown $owner:$group $tmppath
+ chmod $mode $tmppath
+ # TODO mv -n non-posix, going to need to have extract
+ # do all of this
+ mv $tmppath $dir/$name
+ ;;
+ d*)
+ mkdir -m 000 $dir/$name
+ chown $owner:$group $dir/$name
+ chmod $mode $dir/name
+ ;;
+
+ *) echo unknown filetype ; exit 1 ;;
+ esac
+
+ #EOC
done