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
30 target=${ZPMDB:=/var/lib/zpm/local.db}
32 # TODO option to only update if new
33 # new is hash is different or build time is later
34 # TODO option to merge all packages found in pkgfile
35 while getopts :f:vd:FSs:auon opt; do
37 f) pkgfile="$OPTARG" ;;
38 v) verbose=$(( verbose + 1 )) ;;
39 d) target="$OPTARG" ;;
42 s) newstatus="$OPTARG" ;;
47 *) echo 'zpm-merge unknown option' $OPTARG; exit 1 ;;
53 if [ ! -e "$target" ]; then
54 die "target file $target does not exist"
57 zpm test -v "$target" || exit 1
65 # TODO if pkgfile is specified, allow multiple packages as args
66 if [ $mergeall -eq 0 ]; then
70 if [ -z "$pkgid" ]; then
71 die "must specify pkgid"
74 eval "$(zpm parse -E $pkgid)"
76 if [ -z "$pkgfile" ]; then
77 pkgfile=$ZPM_PACKAGE_FILE
80 # calculate package id, pkgfile, etc
81 # cases R = full package id, F = specified package file
84 # --- 000 error, must specify something
85 if [ -z "$release" ] && [ -z "$pkgfile" ]; then
86 die must specify package file or complete package id
89 # try to get from package file
90 if [ -z "$release" ]; then
91 pkgid=$(zpm findpkg -f $pkgfile $pkgid)
92 if [ -z "$pkgid" ]; then
93 die cannot find package id
95 eval "$(zpm parse -E $pkgid)"
98 # --F 001 error, wouldn't know which pkgid to create, could derive from file?
99 if [ -z "$release" ]; then
100 die must specify complete package id
103 # set file from pkgid
104 # -R- 010 set file from pkgid, create in file, error if no file
105 if [ -z "$pkgfile" ]; then
109 if [ $verbose -gt 1 ]; then
110 echo merging $pkgfile $pkgid into $target
114 pkglist=$(zpm list -f "$pkgfile")
117 zpm test -v "$pkgfile" || exit 1
119 if [ -n "$newstatus" ]; then
120 newstatus=$(zpm quote "$newstatus")
125 package=$(zpm parse -n "$pkgid")
126 where="where printf('%s-%s-%s', P.package, P.version, P.release) = '$pkgid'"
127 if [ $update -eq 1 ]; then
128 printf "delete from packages_pkgid where pkgid = '%s';\n" "$pkgid"
130 if [ $remove_older -eq 1 ]; then
131 printf "delete from packages_pkgid where package = '$package' and pkgid < '%s' collate vercmp;\n" "$pkgid"
133 printf "insert or rollback into packages select * from remote.packages P %s;\n" "$where"
134 printf "insert or rollback into packagefiles select * from remote.packagefiles P %s;\n" "$where"
135 printf "insert or rollback into scripts select * from remote.scripts P %s;\n" "$where"
136 if [ $mergefiles -eq 1 ]; then
137 printf "insert into files select F.* from remote.files F\n"
138 printf "inner join remote.packagefiles_pkgid P on P.hash = F.hash %s\n" "$where"
139 printf "on conflict (hash) do nothing;\n";
142 if [ $mergescripts -eq 1 ]; then
143 printf "insert into files select F.* from remote.files F\n"
144 printf "inner join remote.scripts_pkgid P on P.hash = F.hash where P.pkgid = '%s'\n" "$pkgid"
145 printf "on conflict (hash) do nothing;\n";
147 if [ -n "$newstatus" ]; then
148 newstatus=$(zpm quote "$newstatus")
149 printf "update packages as P set status = '$newstatus' %s;\n" "$where"
153 # check for already merged packages
154 if [ $update -eq 0 ]; then
157 for pkg in $pkglist; do
158 merged=$(zpm shell "$target" "select 1 from packages_pkgid where pkgid = '$pkgid'")
159 if [ -n "$merged" ]; then
160 warn "$pkgid already exists in $target";
164 if [ $efail -eq 1 ]; then
171 package=$(zpm parse -n "$pkg")
172 newer=$(zpm shell "$target" "select '$pkg' > (select max(pkgid collate vercmp) from packages_pkgid where package = '$package') collate vercmp")
173 if [ "$newer" = '1' ] || [ -z "$newer" ]; then
180 if [ $only_if_newer -eq 1 ]; then
182 for pkgid in $pkglist; do
183 if check_newer "$pkgid" ; then
184 mlist="$mlist $pkgid"
187 if [ -z "$mlist" ]; then
192 # TODO file tags and package tags
195 printf "attach '%s' as remote;\n" "$pkgfile"
198 for pkgid in $pkglist; do
202 printf "insert or ignore into elfneeded select * from remote.elfneeded;\n"
203 printf "insert or ignore into elflibraries select * from remote.elflibraries;\n"
206 } | zpm shell $target
208 if [ $verbose -ne 0 ]; then
209 printf "%s\n" $pkglist