]> pd.if.org Git - zpackage/blobdiff - zpm-contents
improve contents and packagehash
[zpackage] / zpm-contents
index 23c07ae29a1d327050f8c5a06e953c5b141083c0..7522c183a647f0f8f9ab34647963e3954e2021db 100755 (executable)
@@ -2,43 +2,87 @@
 
 pkgfile=${ZPMDB:-/var/lib/zpm/db.zpm}
 
-verbose=0
+long=0
+pkgonly=0
+quiet=0
 
-while getopts f:v opt; do
+while getopts f:qln opt; do
        case $opt in
                f) pkgfile="$OPTARG" ;;
-               v) verbose=1
+               l) long=1 ;;
+               n) pkgonly=1 ;;
+               q) quiet=1 ;;
        esac
 done
 shift $((OPTIND - 1))
 
-set -e
-
 if [ ! -f $pkgfile ]; then
        echo cannot find $pkgfile
        exit 1
 fi
 
-cols=
-if [ $verbose -gt 0 ]; then
-       cols="filetype,printf('%4.4s', mode) as mode,username,groupname,"
+if [ $long -gt 0 ]; then
+       cols="filetype,printf('%3.3s', mode) as mode,username,groupname,"
+fi
+
+if [ $pkgonly -eq 1 ]; then
+       cols="pkgid"
+elif [ $quiet -eq 0 ]; then
+       cols="pkgid,$cols"
 fi
 
-for pkg in "$@"; do
-       pkgid=$(zpm findpkg -f $pkgfile $pkg)
-
-       zpm shell $pkgfile <<EOS
-.separator " "
-select pkgid, $cols
-case when filetype = 'd' then
-       rtrim(path,'/') || '/'
-else
-       path
-end as path
-from packagefiles_pkgid
-where pkgid = '$pkgid'
-order by path
-;
-EOS
+pkglist=
+while [ $# -gt 0 ]; do
+       pkg=$1
+       shift
+       if [ "$pkg" = '--' ]; then
+               break;
+       fi
+
+       pkgid=$(zpm findpkg -f $pkgfile "$pkg")
+       if [ -n "$pkgid" ]; then
+               q=$(zpm quote -q "$pkgid")
+               pkglist=",$q"
+       else
+               warn "package $pkg not found, ignoring"
+       fi
+done
+pkglist=${pkglist#,}
 
+globlist=
+for glob in "$@"; do
+       q=$(zpm quote -q "$glob")
+       globlist="or path glob $q"
 done
+globlist=${globlist#'or '}
+
+cols=${cols%,}
+
+{
+       printf '.separator " "\n'
+       printf 'select %s\n' "$cols"
+
+       if [ $pkgonly -eq 0 ]; then
+               if [ -n "$cols" ]; then
+                       printf ', '
+               fi
+
+               cat <<-EOC
+               case when filetype = 'd' then
+                       rtrim(path,'/') || '/'
+               else
+                       path
+               end as path
+               EOC
+       fi
+       printf 'from packagefiles_pkgid\nwhere true\n'
+       if [ -n "$globlist" ]; then
+               printf "and (%s)\n" "$globlist"
+       fi
+       if [ -n "$pkglist" ]; then
+               printf "and pkgid in (%s)\n" "$pkglist"
+       fi
+       printf 'order by pkgid,path\n'
+       printf ';\n'
+} | zpm shell $pkgfile
+#} | cat