--- /dev/null
+#!/bin/sh
+
+# garbage collect a package database
+# flags to skip phases
+
+db=${1:-${ZPMDB:-/var/lib/zpm/local.db}}
+
+if [ -z "$db" ]; then
+ echo must specify database file
+ exit 1
+fi
+
+ZPMDB=$db
+export ZPMDB
+
+# check for incorrect hash file content
+
+# remove orphaned elf info
+
+# remove failed packages
+for pkg in $(zpm list -s failed); do
+ zpm log -i -a 'gc removing' -t "$pkg"
+ zpm rmpackage "$pkg"
+done
+
+for pkg in $(zpm list -s dryrun); do
+ zpm log -i -a 'gc removing' -t "$pkg"
+ zpm rmpackage "$pkg"
+done
+
+# remove incomplete packages
+
+# remove outdated packages, unless preserved
+# TODO need 'rstatus' flag
+
+# remove removed packages, unless preserved
+
+# remove orphaned rows in files table
+# references in packagefiles, notes, scripts, possibly others
+
+# remove old logs, given by date
+
+# remove acked notes
+
+# remove install history by date
+
+# remove history for removed packages
+
+# remove command history by date
+
+# compactify the database file
+