+zpm test -v "$ZPMDB" || die "$ZPMDB is not a zpm database"
+
+# check if we're installing something already
+var=$(zpm list -f $localdb -s installing | wc -l)
+if [ $var -gt 0 ]; then
+ zpm list -v -f $localdb -s installing
+ die "already ($localdb) installing $var package(s)"
+fi
+# check if we're removing something already
+var=$(zpm list -f $localdb -s removing | wc -l)
+if [ $var -gt 0 ]; then
+ zpm list -v -f $localdb -s removing
+ die "already ($localdb) removing $var package(s)"
+fi
+var=$(zpm list -f $localdb -s updating | wc -l)
+if [ $var -gt 0 ]; then
+ zpm list -v -f $localdb -s updating
+ die "already ($localdb) updating $var package(s)"
+fi
+
+if [ -n "$rootdir" ]; then
+ ZPM_ROOT_DIR="$rootdir"
+ export ZPM_ROOT_DIR
+fi
+
+for pkgid in $pkglist; do
+ curstatus=$(zpm pkg $pkgid status)
+ if [ "$curstatus" = 'installed' ]; then
+ die "$pkgid already installed"
+ fi
+
+ package=$(zpm parse -n $pkgid)
+ current=$(zpm list -s installed "$package")
+
+ if [ $runscripts -gt 0 ]; then
+ # TODO run pre-upgrade script if needed
+ # zpm runscript -p pre-upgrade $current $pkgid
+ zpm script -f $pkgfile -p pre-install $pkgid $current
+ if [ $? -ne 0 ]; then
+ die "pre-install script for $pkgid failed"
+ fi
+ else
+ if zpm script -f $pkgfile -Fql -p pre-install $pkgid; then
+ zpm note -e -p $pkgid -m 'pre-install script not run'
+ fi
+ fi
+
+ # remove the package if it exists. can't be in an installed
+ # state, would have bailed above. So, if it exists, can only
+ # be 'upgraded'. This should be fast, because we won't run
+ # a garbage collect, so any possible duplicate files between
+ # an upgraded or removed files, if they're there, won't need
+ # to be merged.
+
+ # only merge if localdb and pkgfile are different
+ if [ "$pkgfile" != "$ZPMDB" ]; then
+ zpm rmpackage $pkgid
+ if [ $verbose -gt 0 ]; then
+ echo merging $pkgid
+ fi
+ if [ $mergefiles -eq 1 ]; then
+ zpm merge -u -F -f $pkgfile -s installing $pkgid
+ else
+ zpm merge -u -f $pkgfile -s installing $pkgid
+ fi
+ if [ $? -ne 0 ]; then
+ die "merging $pkgid failed"
+ fi
+ else
+ zpm pkg $pkgid status=installing
+ fi
+
+ zpm shell "$ZPMDB" "update packages set status='updating' where status='installed' and package in (select package from packages where status = 'installing')"
+done
+
+# do the sync