#!/usr/bin/sh

# Update the Copr builder machine, can be called anytime Copr build system
# decides to do so (please keep the script idempotent).

# install the latest versions of those packages
dnf update -y libdnf dnf-plugins-core python3-dnf rpm system-rpm-config mock-core-configs mock libsolv librepo libdnf5 dist-git-client python3-libdnf5 distribution-gpg-keys ca-certificates  *rpm-macros

# The mock-core-configs package was potentially updated above, and it provides
# "noreplace" %config files.  It means that - if the builder cloud image had
# baked-in locally _changed_ configuration files - the updated official
# configuration files from mock-core-configs package wouldn't be used.  So now
# make sure that they _are_ used (those, if any, would reside in .rpmnew files).
find /etc/mock -name '*.rpmnew' | while read -r rpmnew_file; do
    config=${rpmnew_file%.rpmnew}
    mv -f "$config" "$config.copr-builder-backup" && \
    mv "$rpmnew_file" "$config"
done

# And now use the overrides from %mock_config_overrides directory
(
  cd /etc/copr-rpmbuild/mock-config-overrides
  find . -name '*.tpl' -o -name '*.cfg' | while read -r file; do
    base=$(basename "$file")
    dir=/etc/mock/$(dirname "$file")
    mkdir -p "$dir"
    cp "$file" "$dir"
  done
)
