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 only update if new
26 # new is hash is different or build time is later
27 # TODO option to merge all packages found in pkgfile
28 while getopts :f:vd:FSs:au opt; do
30 f) pkgfile="$OPTARG" ;;
32 d) target="$OPTARG" ;;
35 s) newstatus="$OPTARG" ;;
39 *) echo 'zpm-merge unknown option' $OPTARG; exit 1 ;;
45 if [ ! -e "$target" ]; then
46 die "target file $target does not exist"
49 zpm test -v "$target" || exit 1
57 # TODO if pkgfile is specified, allow multiple packages as args
58 if [ $mergeall -eq 0 ]; then
62 if [ -z "$pkgid" ]; then
63 die "must specify pkgid"
66 eval "$(zpm parse -E $pkgid)"
68 if [ -z "$pkgfile" ]; then
69 pkgfile=$ZPM_PACKAGE_FILE
72 # calculate package id, pkgfile, etc
73 # cases R = full package id, F = specified package file
76 # --- 000 error, must specify something
77 if [ -z "$release" ] && [ -z "$pkgfile" ]; then
78 die must specify package file or complete package id
81 # try to get from package file
82 if [ -z "$release" ]; then
83 pkgid=$(zpm findpkg -f $pkgfile $pkgid)
84 if [ -z "$pkgid" ]; then
85 die cannot find package id
87 eval "$(zpm parse -E $pkgid)"
90 # --F 001 error, wouldn't know which pkgid to create, could derive from file?
91 if [ -z "$release" ]; then
92 die must specify complete package id
96 # -R- 010 set file from pkgid, create in file, error if no file
97 if [ -z "$pkgfile" ]; then
101 if [ $verbose -gt 0 ]; then
102 echo merging $pkgfile $pkgid into $target
106 pkglist=$(zpm list -f "$pkgfile")
109 zpm test -v "$pkgfile" || exit 1
111 if [ -n "$newstatus" ]; then
112 newstatus=$(zpm quote "$newstatus")
117 where="where printf('%s-%s-%s', P.package, P.version, P.release) = '$pkgid'"
118 if [ $update -eq 1 ]; then
119 printf "delete from packages_pkgid where pkgid = '%s';\n" "$pkgid"
121 printf "insert or rollback into packages select * from remote.packages P %s;\n" "$where"
122 printf "insert or rollback into packagefiles select * from remote.packagefiles P %s;\n" "$where"
123 printf "insert or rollback into scripts select * from remote.scripts P %s;\n" "$where"
124 if [ $mergefiles -eq 1 ]; then
125 printf "insert into files select F.* from remote.files F\n"
126 printf "inner join remote.packagefiles_pkgid P on P.hash = F.hash %s\n" "$where"
127 printf "on conflict (hash) do nothing;\n";
130 if [ $mergescripts -eq 1 ]; then
131 printf "insert into files select F.* from remote.files F\n"
132 printf "inner join remote.scripts_pkgid P on P.hash = F.hash where P.pkgid = '%s'\n" "$pkgid"
133 printf "on conflict (hash) do nothing;\n";
135 if [ -n "$newstatus" ]; then
136 newstatus=$(zpm quote "$newstatus")
137 printf "update packages as P set status = '$newstatus' %s;\n" "$where"
141 # check for already merged packages
142 if [ $update -eq 0 ]; then
145 for pkg in $pkglist; do
146 merged=$(zpm shell "$target" "select 1 from packages_pkgid where pkgid = '$pkgid'")
147 if [ -n "$merged" ]; then
148 warn "$pkgid already exists in $target";
152 if [ $efail -eq 1 ]; then
157 # TODO file tags and package tags
160 printf "attach '%s' as remote;\n" "$pkgfile"
163 for pkgid in $pkglist; do
167 printf "insert or ignore into elfneeded select * from remote.elfneeded;\n"
168 printf "insert or ignore into elflibraries select * from remote.elflibraries;\n"
171 } | zpm shell $target
173 if [ $verbose -ne 0 ]; then
174 printf "%s\n" $pkglist