]> pd.if.org Git - zpackage/blobdiff - zpm-addtopackage
use zpm shell instead of sqlite3
[zpackage] / zpm-addtopackage
index 1964b77589e29f6cf58778cb3b8e24fdc4a57cc0..a2e5aeec4c86c3a25c811b31b585d2c7010b4651 100755 (executable)
@@ -3,10 +3,33 @@
 pkgver=${ZPMPKGVER:-1.0}
 pkgrel=${ZPMPKGREL:-1}
 
+die() {
+       echo $* 1>&2
+       exit 1
+}
+
+# basic cleanup on a path
+cleanpath() {
+       clean="$1"
+       if [ -z "$clean" ]; then printf ''; fi
+
+       # multiple slashes
+       clean=$(printf "%s" "$clean" | sed -e 's|/+|/|g')
+       # curdir
+       clean=$(printf "%s" "$clean" | sed -e 's|/\./|/|g')
+       # leading curdir
+       clean=${clean#./}
+       # trailing curdir
+       clean=${clean%/.}
+       # trailing slash
+       clean=${clean%/}
+       printf "%s" "$clean"
+}
+
 # option for "multipackage" just to let the system know that's what you meant
 # option to take filenames from stdin
 # parse package, version, release from file if not given
-while getopts :f:v:r:d:a:u:l:p:b:P: opt; do
+while getopts :f:v:r:d:a:u:l:p:b:P:S: opt; do
        case $opt in
                f) pkgfile="$OPTARG" ;;
                v) pkgver="$OPTARG" ;;
@@ -18,6 +41,7 @@ while getopts :f:v:r:d:a:u:l:p:b:P: opt; do
                p) packager="$OPTARG" ;;
                b) builddate="$OPTARG" ;;
                P) prefix="$OPTARG" ;;
+               S) strip=$(cleanpath "$OPTARG"); ;;
                t) tags="$OPTARG" ;;
                c) tags="$tags +configuration" ;;
        esac
@@ -26,18 +50,21 @@ shift $((OPTIND - 1))
 
 package="$1"
 shift
+if [ -z "$package" ]; then
+       die "must specify package"
+fi
 
 set -e
 if [ -z "$pkgfile" ]; then
        pkgfile="$package-$pkgver-$pkgrel.zpm"
 fi
 
-if [ ! -f $pkgfile ]; then
-       zpm newpackage $package || exit 1
-else
-       zpm test -v $pkgfile
-fi
+# make sure package exists
+zpm newpackage -I -f $pkgfile -v $pkgver -r $pkgrel $package || exit 1
+
+zpm test -v $pkgfile
 
+#strip=$(cleanpath "$strip")
 for path in $*; do
        mtime=$(zpm stat -f '%y' $path)
        uid=$(zpm stat -f '%u' $path)
@@ -45,20 +72,29 @@ for path in $*; do
        username=$(zpm stat -f '%U' $path)
        groupname=$(zpm stat -f '%G' $path)
        mode=$(zpm stat -f '%a' $path)
+       rpath="$path"
+
+       rpath=$(cleanpath "$path")
 
-       # strip off leading slashes
-       rpath=$(echo "$path" | sed -e 's|^/*||')
-       # and a leading ./
-       rpath=${rpath#./}
-       rpath=$(echo "$rpath" | sed -e 's|^/*||')
+       # strip off leading slash
+       rpath=${rpath#/}
 
        if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
                continue
        fi
 
+       if [ ! -z "$strip" ]; then
+               echo "stripping $strip"
+               rpath=${rpath#$strip}
+               rpath=${rpath#/}
+       fi
+
+       if [ -z "$rpath" ]; then
+               die "$path resolves to nothing"
+       fi
+
+       prefix=$(cleanpath "$prefix")
        if [ ! -z "$prefix" ]; then
-               # trailing slashes on prefix
-               prefix=$(echo "$prefix" | sed -e 's|/*$||')
                rpath="$prefix/$rpath"
        fi
 
@@ -67,10 +103,12 @@ for path in $*; do
        target='NULL'
        case "$filetype" in
                regular)
-                       filetype=d
+                       filetype=r
                        hash=$(zpm addfile $pkgfile "$path")
+                       if [ $? -ne 0 ]; then
+                               die "zpm addfile failed ($?): $pkgfile $path" 
+                       fi
                        hash="'$hash'"
-                       filetype=r
                        ;;
                directory)
                        filetype=d
@@ -82,7 +120,7 @@ for path in $*; do
                        ;;
        esac
 
-       sqlite3 $pkgfile <<EOS
+       zpm shell $pkgfile <<EOS
 PRAGMA foreign_keys = ON;
 begin;
 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,target)