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
25 target=${ZPMDB:=/var/lib/zpm/local.db}
27 # TODO option to only update if new
28 # new is hash is different or build time is later
29 # TODO option to merge all packages found in pkgfile
30 while getopts :f:vd:FSs:auon opt; do
32 f) pkgfile="$OPTARG" ;;
34 d) target="$OPTARG" ;;
37 s) newstatus="$OPTARG" ;;
42 *) echo 'zpm-merge unknown option' $OPTARG; exit 1 ;;
48 if [ ! -e "$target" ]; then
49 die "target file $target does not exist"
52 zpm test -v "$target" || exit 1
60 # TODO if pkgfile is specified, allow multiple packages as args
61 if [ $mergeall -eq 0 ]; then
65 if [ -z "$pkgid" ]; then
66 die "must specify pkgid"
69 eval "$(zpm parse -E $pkgid)"
71 if [ -z "$pkgfile" ]; then
72 pkgfile=$ZPM_PACKAGE_FILE
75 # calculate package id, pkgfile, etc
76 # cases R = full package id, F = specified package file
79 # --- 000 error, must specify something
80 if [ -z "$release" ] && [ -z "$pkgfile" ]; then
81 die must specify package file or complete package id
84 # try to get from package file
85 if [ -z "$release" ]; then
86 pkgid=$(zpm findpkg -f $pkgfile $pkgid)
87 if [ -z "$pkgid" ]; then
88 die cannot find package id
90 eval "$(zpm parse -E $pkgid)"
93 # --F 001 error, wouldn't know which pkgid to create, could derive from file?
94 if [ -z "$release" ]; then
95 die must specify complete package id
99 # -R- 010 set file from pkgid, create in file, error if no file
100 if [ -z "$pkgfile" ]; then
104 if [ $verbose -gt 0 ]; then
105 echo merging $pkgfile $pkgid into $target
109 pkglist=$(zpm list -f "$pkgfile")
112 zpm test -v "$pkgfile" || exit 1
114 if [ -n "$newstatus" ]; then
115 newstatus=$(zpm quote "$newstatus")
120 package=$(zpm parse -n "$pkgid")
121 where="where printf('%s-%s-%s', P.package, P.version, P.release) = '$pkgid'"
122 if [ $update -eq 1 ]; then
123 printf "delete from packages_pkgid where pkgid = '%s';\n" "$pkgid"
125 if [ $remove_older -eq 1 ]; then
126 printf "delete from packages_pkgid where package = '$package' and pkgid < '%s' collate vercmp;\n" "$pkgid"
128 printf "insert or rollback into packages select * from remote.packages P %s;\n" "$where"
129 printf "insert or rollback into packagefiles select * from remote.packagefiles P %s;\n" "$where"
130 printf "insert or rollback into scripts select * from remote.scripts P %s;\n" "$where"
131 if [ $mergefiles -eq 1 ]; then
132 printf "insert into files select F.* from remote.files F\n"
133 printf "inner join remote.packagefiles_pkgid P on P.hash = F.hash %s\n" "$where"
134 printf "on conflict (hash) do nothing;\n";
137 if [ $mergescripts -eq 1 ]; then
138 printf "insert into files select F.* from remote.files F\n"
139 printf "inner join remote.scripts_pkgid P on P.hash = F.hash where P.pkgid = '%s'\n" "$pkgid"
140 printf "on conflict (hash) do nothing;\n";
142 if [ -n "$newstatus" ]; then
143 newstatus=$(zpm quote "$newstatus")
144 printf "update packages as P set status = '$newstatus' %s;\n" "$where"
148 # check for already merged packages
149 if [ $update -eq 0 ]; then
152 for pkg in $pkglist; do
153 merged=$(zpm shell "$target" "select 1 from packages_pkgid where pkgid = '$pkgid'")
154 if [ -n "$merged" ]; then
155 warn "$pkgid already exists in $target";
159 if [ $efail -eq 1 ]; then
166 package=$(zpm parse -n "$pkg")
167 newer=$(zpm shell "$target" "select '$pkg' > (select max(pkgid) collate vercmp from packages_pkgid where package = '$package') from packages_pkgid where package = '$package'")
168 if [ "$newer" = '1' ] || [ -z "$newer" ]; then
175 if [ $only_if_newer -eq 1 ]; then
177 for pkgid in $pkglist; do
178 if check_newer "$pkgid" ; then
179 mlist="$mlist $pkgid"
182 if [ -z "$mlist" ]; then
187 # TODO file tags and package tags
190 printf "attach '%s' as remote;\n" "$pkgfile"
193 for pkgid in $pkglist; do
197 printf "insert or ignore into elfneeded select * from remote.elfneeded;\n"
198 printf "insert or ignore into elflibraries select * from remote.elflibraries;\n"
201 } | zpm shell $target
203 if [ $verbose -ne 0 ]; then
204 printf "%s\n" $pkglist