4 # collects info from package file and copies it into another package file
6 # merge [ -d ZPMDB ] [-f $pkgfile] pkg
7 # -F include file content
8 # -S don't include script content
11 printf 'zpm-merge:' 1>&2
12 printf ' %s' "$@" 1>&2
23 target=${ZPMDB:=/var/lib/zpm/local.db}
25 # TODO option to merge all packages found in pkgfile
26 while getopts :f:vd:FSs:au opt; do
28 f) pkgfile="$OPTARG" ;;
30 d) target="$OPTARG" ;;
33 s) newstatus="$OPTARG" ;;
37 *) echo 'zpm-merge unknown option' $OPTARG; exit 1 ;;
43 if [ ! -e "$target" ]; then
44 die "target file $target does not exist"
47 zpm test -v "$target" || exit 1
55 # TODO if pkgfile is specified, allow multiple packages as args
56 if [ $mergeall -eq 0 ]; then
60 if [ -z "$pkgid" ]; then
61 die "must specify pkgid"
64 eval "$(zpm parse -E $pkgid)"
66 if [ -z "$pkgfile" ]; then
67 pkgfile=$ZPM_PACKAGE_FILE
70 # calculate package id, pkgfile, etc
71 # cases R = full package id, F = specified package file
74 # --- 000 error, must specify something
75 if [ -z "$release" ] && [ -z "$pkgfile" ]; then
76 die must specify package file or complete package id
79 # try to get from package file
80 if [ -z "$release" ]; then
81 pkgid=$(zpm findpkg -f $pkgfile $pkgid)
82 if [ -z "$pkgid" ]; then
83 die cannot find package id
85 eval "$(zpm parse -E $pkgid)"
88 # --F 001 error, wouldn't know which pkgid to create, could derive from file?
89 if [ -z "$release" ]; then
90 die must specify complete package id
94 # -R- 010 set file from pkgid, create in file, error if no file
95 if [ -z "$pkgfile" ]; then
99 if [ $verbose -gt 0 ]; then
100 echo merging $pkgfile $pkgid into $target
104 pkglist=$(zpm list -f "$pkgfile")
107 zpm test -v "$pkgfile" || exit 1
109 if [ -n "$newstatus" ]; then
110 newstatus=$(zpm quote "$newstatus")
115 where="where printf('%s-%s-%s', P.package, P.version, P.release) = '$pkgid'"
116 if [ $update -eq 1 ]; then
117 printf "delete from packages %s;\n" "$where"
119 printf "insert or rollback into packages select * from remote.packages P %s;\n" "$where"
120 printf "insert or rollback into packagefiles select * from remote.packagefiles P %s;\n" "$where"
121 printf "insert or rollback into scripts select * from remote.scripts P %s;\n" "$where"
122 if [ $mergefiles -eq 1 ]; then
123 printf "insert into files select F.* from remote.files F\n"
124 printf "inner join remote.packagefiles_pkgid P on P.hash = F.hash %s\n" "$where"
125 printf "on conflict (hash) do nothing;\n";
128 if [ $mergescripts -eq 1 ]; then
129 printf "insert into files select F.* from remote.files F\n"
130 printf "inner join remote.scripts_pkgid P on P.hash = F.hash where P.pkgid = '%s'\n" "$pkgid"
131 printf "on conflict (hash) do nothing;\n";
133 if [ -n "$newstatus" ]; then
134 newstatus=$(zpm quote "$newstatus")
135 printf "update packages as P set status = '$newstatus' %s;\n" "$where"
139 # check for already merged packages
140 if [ $update -eq 0 ]; then
143 for pkg in $pkglist; do
144 merged=$(zpm shell "$target" "select 1 from packages_pkgid where pkgid = '$pkgid'")
145 if [ -n "$merged" ]; then
146 warn "$pkgid already exists in $target";
150 if [ $efail -eq 1 ]; then
155 # TODO file tags and package tags
158 printf "attach '%s' as remote;\n" "$pkgfile"
161 for pkgid in $pkglist; do
165 printf "insert or ignore into elfneeded select * from remote.elfneeded;\n"
166 printf "insert or ignore into elflibraries select * from remote.elflibraries;\n"
169 } | zpm shell $target