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
31 target=${ZPMDB:=/var/lib/zpm/local.db}
33 # TODO option to only update if new
34 # new is hash is different or build time is later
35 # TODO option to merge all packages found in pkgfile
36 while getopts :f:vd:FSs:auonO opt; do
38 f) pkgfile="$OPTARG" ;;
39 v) verbose=$(( verbose + 1 )) ;;
40 d) target="$OPTARG" ;;
43 s) newstatus="$OPTARG" ;;
49 *) echo 'zpm-merge unknown option' $OPTARG; exit 1 ;;
55 if [ ! -e "$target" ]; then
56 die "target file $target does not exist"
59 zpm test -v "$target" || exit 1
67 # TODO if pkgfile is specified, allow multiple packages as args
68 if [ $mergeall -eq 0 ]; then
72 if [ -z "$pkgid" ]; then
73 die "must specify pkgid"
76 eval "$(zpm parse -E $pkgid)"
78 if [ -z "$pkgfile" ]; then
79 pkgfile=$ZPM_PACKAGE_FILE
82 # calculate package id, pkgfile, etc
83 # cases R = full package id, F = specified package file
86 # --- 000 error, must specify something
87 if [ -z "$release" ] && [ -z "$pkgfile" ]; then
88 die must specify package file or complete package id
91 # try to get from package file
92 if [ -z "$release" ]; then
93 pkgid=$(zpm findpkg -f $pkgfile $pkgid)
94 if [ -z "$pkgid" ]; then
95 die cannot find package id
97 eval "$(zpm parse -E $pkgid)"
100 # --F 001 error, wouldn't know which pkgid to create, could derive from file?
101 if [ -z "$release" ]; then
102 die must specify complete package id
105 # set file from pkgid
106 # -R- 010 set file from pkgid, create in file, error if no file
107 if [ -z "$pkgfile" ]; then
111 if [ $verbose -gt 1 ]; then
112 echo merging $pkgfile $pkgid into $target
116 pkglist=$(zpm list -f "$pkgfile")
119 zpm test -v "$pkgfile" || exit 1
121 if [ -n "$newstatus" ]; then
122 newstatus=$(zpm quote "$newstatus")
127 package=$(zpm parse -n "$pkgid")
128 where="where printf('%s-%s-%s', P.package, P.version, P.release) = '$pkgid'"
129 if [ $update -eq 1 ]; then
130 printf "delete from packages_pkgid where pkgid = '%s';\n" "$pkgid"
132 if [ $remove_older -eq 1 ]; then
133 printf "delete from packages_pkgid where package = '$package' and pkgid < '%s' collate vercmp;\n" "$pkgid"
135 printf "insert or rollback into packages select * from remote.packages P %s;\n" "$where"
136 printf "insert or rollback into packagefiles select * from remote.packagefiles P %s;\n" "$where"
137 printf "insert or rollback into scripts select * from remote.scripts P %s;\n" "$where"
138 if [ $mergefiles -eq 1 ]; then
139 printf "insert into files select F.* from remote.files F\n"
140 printf "inner join remote.packagefiles_pkgid P on P.hash = F.hash %s\n" "$where"
141 printf "on conflict (hash) do nothing;\n";
144 if [ $mergescripts -eq 1 ]; then
145 printf "insert into files select F.* from remote.files F\n"
146 printf "inner join remote.scripts_pkgid P on P.hash = F.hash where P.pkgid = '%s'\n" "$pkgid"
147 printf "on conflict (hash) do nothing;\n";
149 if [ -n "$newstatus" ]; then
150 newstatus=$(zpm quote "$newstatus")
151 printf "update packages as P set status = '$newstatus' %s;\n" "$where"
156 merged=$(zpm shell "$target" "select 1 from packages_pkgid where pkgid = '$1'")
162 for pkgid in "$@"; do
163 if ! check_have "$pkgid" ; then
164 mlist="$mlist $pkgid"
173 for pkgid in "$@"; do
174 if check_have "$pkgid" ; then
175 mlist="$mlist $pkgid"
184 package=$(zpm parse -n "$pkg")
185 newer=$(zpm shell "$target" "select '$pkg' > (select max(pkgid collate vercmp) from packages_pkgid where package = '$package') collate vercmp")
186 if [ "$newer" = '1' ] || [ -z "$newer" ]; then
194 for pkgid in "$@"; do
195 if check_newer "$pkgid" ; then
196 mlist="$mlist $pkgid"
204 # TODO file tags and package tags
207 printf "attach '%s' as remote;\n" "$pkgfile"
210 for pkgid in "$@"; do
214 printf "insert or ignore into elfneeded select * from remote.elfneeded;\n"
215 printf "insert or ignore into elflibraries select * from remote.elflibraries;\n"
216 printf "insert or ignore into package_signatures select * from remote.package_signatures PS where PS.packagehash in (select hash from remote.packages);\n"
219 ) | zpm shell $target
222 # check for already merged packages
223 if [ $update -eq 0 ] && [ $only_if_newer -eq 0 ]; then
224 have=$(filter_have $pkglist)
225 if [ -n "$have" ]; then
226 die "already merged: $have"
230 if [ $update -eq 0 ]; then
231 pkglist=$(filter_out_have $pkglist)
234 if [ $only_if_newer -eq 1 ]; then
235 pkglist=$(filter_newer $pkglist)
236 if [ -z "$pkglist" ]; then
241 if [ $one_at_a_time -eq 1 ]; then
242 for pkgid in $pkglist; do
243 if [ $verbose -ne 0 ]; then
249 if [ $verbose -ne 0 ]; then
250 printf "%s\n" $pkglist