verbose=0
mergefiles=0
mergescripts=1
-target=/var/lib/zpm/local.db
+mergeall=1
+update=0
target=${ZPMDB:=/var/lib/zpm/local.db}
# TODO option to merge all packages found in pkgfile
-while getopts :f:vd:FSs: opt; do
+while getopts :f:vd:FSs:au opt; do
case $opt in
f) pkgfile="$OPTARG" ;;
v) verbose=1 ;;
F) mergefiles=1 ;;
S) mergescripts=0 ;;
s) newstatus="$OPTARG" ;;
+ a) mergeall=1 ;;
+ u) update=1 ;;
+
*) echo 'zpm-merge unknown option' $OPTARG; exit 1 ;;
esac
done
zpm test -v "$target" || exit 1
+if [ $# -eq 0 ]; then
+ mergeall=1
+else
+ mergeall=0
+fi
+
# TODO if pkgfile is specified, allow multiple packages as args
-pkgid=$1
-shift
+if [ $mergeall -eq 0 ]; then
+ pkgid=$1
+ shift
-if [ -z "$pkgid" ]; then
- die "must specify pkgid"
-fi
+ if [ -z "$pkgid" ]; then
+ die "must specify pkgid"
+ fi
-eval "$(zpm parse -E $pkgid)"
+ eval "$(zpm parse -E $pkgid)"
-if [ -z "$pkgfile" ]; then
- pkgfile=$ZPM_PACKAGE_FILE
-fi
+ if [ -z "$pkgfile" ]; then
+ pkgfile=$ZPM_PACKAGE_FILE
+ fi
-# calculate package id, pkgfile, etc
-# cases R = full package id, F = specified package file
+ # calculate package id, pkgfile, etc
+ # cases R = full package id, F = specified package file
-# immediate error
-# --- 000 error, must specify something
-if [ -z "$release" ] && [ -z "$pkgfile" ]; then
- die must specify package file or complete package id
-fi
+ # immediate error
+ # --- 000 error, must specify something
+ if [ -z "$release" ] && [ -z "$pkgfile" ]; then
+ die must specify package file or complete package id
+ fi
-# try to get from package file
-if [ -z "$release" ]; then
- pkgid=$(zpm findpkg -f $pkgfile $pkgid)
- if [ -z "$pkgid" ]; then
- die cannot find package id
+ # try to get from package file
+ if [ -z "$release" ]; then
+ pkgid=$(zpm findpkg -f $pkgfile $pkgid)
+ if [ -z "$pkgid" ]; then
+ die cannot find package id
+ fi
+ eval "$(zpm parse -E $pkgid)"
fi
- eval "$(zpm parse -E $pkgid)"
-fi
-# --F 001 error, wouldn't know which pkgid to create, could derive from file?
-if [ -z "$release" ]; then
- die must specify complete package id
-fi
+ # --F 001 error, wouldn't know which pkgid to create, could derive from file?
+ if [ -z "$release" ]; then
+ die must specify complete package id
+ fi
-# set file from pkgid
-# -R- 010 set file from pkgid, create in file, error if no file
-if [ -z "$pkgfile" ]; then
- pkgfile="$pkgid.zpm"
-fi
+ # set file from pkgid
+ # -R- 010 set file from pkgid, create in file, error if no file
+ if [ -z "$pkgfile" ]; then
+ pkgfile="$pkgid.zpm"
+ fi
-if [ $verbose -gt 0 ]; then
- echo merging $pkgfile $pkgid into $target
+ if [ $verbose -gt 0 ]; then
+ echo merging $pkgfile $pkgid into $target
+ fi
+ pkglist=$pkgid
+else
+ pkglist=$(zpm list "$pkgfile")
fi
zpm test -v "$pkgfile" || exit 1
-merged=$(zpm shell "$target" "select 1 from packages_pkgid where pkgid = '$pkgid'")
-if [ -n "$merged" ]; then
- die "$pkgid already exists in $target";
+if [ -n "$newstatus" ]; then
+ newstatus=$(zpm quote "$newstatus")
fi
-# TODO file tags and package tags
-{
-cat <<EOS
-.bail on
-attach '$pkgfile' as remote;
-
-begin;
-
-insert or rollback into packages
-select * from remote.packages P
-where
-printf('%s-%s-%s', P.package, P.version, P.release) = '$pkgid'
-;
-
-insert or rollback into packagefiles
-select * from remote.packagefiles PF
-where
-printf('%s-%s-%s', PF.package, PF.version, PF.release) = '$pkgid'
-;
-
-insert or rollback into scripts
-select * from remote.scripts PF
-where
-printf('%s-%s-%s', PF.package, PF.version, PF.release) = '$pkgid'
-;
-EOS
-
-# actual files
-if [ $mergefiles -eq 1 ]; then
-cat<<EOS
-insert into files
-select F.* from remote.files F
-inner join remote.packagefiles_pkgid PF
-on PF.hash = F.hash
-where
-PF.pkgid = '$pkgid'
-on conflict (hash) do nothing
-;
-
-EOS
-fi
+mergeone() {
+ pkgid=$1
+ where="where printf('%s-%s-%s', P.package, P.version, P.release) = '$pkgid'"
+ if [ $update -eq 1 ]; then
+ printf "delete from packages %s;\n" "$where"
+ fi
+ printf "insert or rollback into packages select * from remote.packages P %s;\n" "$where"
+ printf "insert or rollback into packagefiles select * from remote.packagefiles P %s;\n" "$where"
+ printf "insert or rollback into scripts select * from remote.scripts P %s;\n" "$where"
+ if [ $mergefiles -eq 1 ]; then
+ printf "insert into files select F.* from remote.files F\n"
+ printf "inner join remote.packagefiles_pkgid P on P.hash = F.hash %s\n" "$where"
+ printf "on conflict (hash) do nothing;\n";
+ fi
+ # scripts
+ if [ $mergescripts -eq 1 ]; then
+ printf "insert into files select F.* from remote.files F\n"
+ printf "inner join remote.scripts_pkgid P on P.hash = F.hash where P.pkgid = '%s'\n" "$pkgid"
+ printf "on conflict (hash) do nothing;\n";
+ fi
+ if [ -n "$newstatus" ]; then
+ newstatus=$(zpm quote "$newstatus")
+ printf "update packages as P set status = '$newstatus' %s;\n" "$where"
+ fi
+}
-# scripts
-if [ $mergescripts -eq 1 ]; then
-cat<<EOS
-insert into files
-select F.* from remote.files F
-inner join remote.scripts_pkgid PF
-on PF.hash = F.hash
-where
-PF.pkgid = '$pkgid'
-on conflict (hash) do nothing
-;
-EOS
+# check for already merged packages
+if [ $update -eq 0 ]; then
+ efail=0
+
+ for pkg in $pkglist; do
+ merged=$(zpm shell "$target" "select 1 from packages_pkgid where pkgid = '$pkgid'")
+ if [ -n "$merged" ]; then
+ warn "$pkgid already exists in $target";
+ efail=1
+ fi
+ done
+ if [ $efail -eq 1 ]; then
+ die "aborting merge"
+ fi
fi
-package=$(zpm quote "$name")
-version=$(zpm quote "$version")
+# TODO file tags and package tags
+{
+ printf ".bail on\n"
+ printf "attach '%s' as remote;\n" "$pkgfile"
+ printf "begin;\n"
-if [ -n "$newstatus" ]; then
- newstatus=$(zpm quote "$newstatus")
-cat <<EOS
-update packages set status = '$newstatus'
-where package = '$package' and version = '$version' and release = '$release'
-;
-EOS
-fi
+ for pkgid in $pkglist; do
+ mergeone "$pkgid"
+ done
+
+ printf "insert or ignore into elfneeded select * from remote.elfneeded;\n"
+ printf "insert or ignore into elflibraries select * from remote.elflibraries;\n"
-# elf info
-cat <<EOS
-insert or ignore into elfneeded
-select * from remote.elfneeded
---on conflict (file, needed) do nothing
-;
-insert or ignore into elflibraries
-select * from remote.elflibraries
---on conflict (file, soname) do nothing
-;
-EOS
-
-
-# TODO check for adding file contents
-echo 'commit;'
+ printf "commit;\n"
} | zpm shell $target