#!/usr/bin/bash

set -euo pipefail
IFS=$'\n\t'

if [[ $USER != 'compose' ]]; then
    echo "This script must be run as the compose user."
    exit 1
fi

COMPOSE="latest-CentOS-Stream-8"
CENTOS_RELEASE="8-stream"


function message() {
    echo -e "\e[36m* $1\e[m"
}


function sync_rpms() {
    arch=$1
    shift
    variants=($@)
    for variant in ${variants[@]}; do
        message "syncing $variant $arch"
        pushd /old-compose/staged/$CENTOS_RELEASE/$variant/$arch/os/
        # delete old comps files first to ensure we use the new ones
        rm -v repodata/*-comps-*.xml*
        # rsync repo from compose
        rsync -ahH --info progress2 \
            /compose/trees/$COMPOSE/compose/$variant/$arch/os/ ./
        # modular metadata must be decompressed for createrepo_c to pick it up
        # https://github.com/rpm-software-management/createrepo_c/issues/263
        find repodata -type f -name '*-modules.yaml.gz' -exec gunzip -vfk {} \;
        find repodata -type f -name '*-modules.yaml.xz' -exec unxz -vfk {} \;
        # update repodata
        createrepo_c --update --workers 8 --xz \
            --distro 'cpe:/o:centos-stream:centos-stream:8,CentOS Stream 8' \
            --revision 8-stream \
            --groupfile $(find repodata -type f -name '*-comps-*.xml') \
            --retain-old-md-by-age 1m \
            .
        popd
    done
}


function sync_isos() {
    arch=$1
    message "syncing $arch isos"
    pushd /old-compose/staged/$CENTOS_RELEASE/isos/$arch/
    # rsync iso directory from compose
    rsync -ahH --info progress2 --delete \
        /compose/trees/$COMPOSE/compose/BaseOS/$arch/iso/ ./
    # set up latest symlinks
    for f in *.iso*; do ln -sf $f $(sed -r 's/-[0-9]{8}-/-latest-/' <<< $f); done
    # modify checksum file
    sed -r 's/-[0-9]{8}-/-latest-/' CHECKSUM > CHECKSUM.latest
    mv CHECKSUM CHECKSUM.date
    cat CHECKSUM.date CHECKSUM.latest > CHECKSUM
    rm CHECKSUM.date CHECKSUM.latest
    popd
}


function sync_debug_rpms() {
    arch=$1
    shift
    variants=($@)
    message "syncing debug $arch"
    pushd /old-compose/staged/debug/$CENTOS_RELEASE/$arch/
    for variant in ${variants[@]}; do
        rsync -ahH --info progress2 \
            /compose/trees/$COMPOSE/compose/$variant/$arch/debug/tree/Packages/ Packages/
    done
    createrepo_c --update --workers 8 --xz .
    popd
}


function sync_source_rpms() {
    variants=($@)
    for variant in ${variants[@]}; do
        message "syncing source $variant"
        pushd /old-compose/staged/vault/$CENTOS_RELEASE/$variant/Source/
        rsync -ahH --info progress2 \
            /compose/trees/$COMPOSE/compose/$variant/source/tree/Packages/ SPackages/
        createrepo_c --update --workers 8 --xz .
        popd
    done
}


sync_rpms aarch64 BaseOS AppStream PowerTools HighAvailability
sync_rpms ppc64le BaseOS AppStream PowerTools HighAvailability ResilientStorage
sync_rpms x86_64  BaseOS AppStream PowerTools HighAvailability ResilientStorage RT NFV

sync_isos aarch64
sync_isos ppc64le
sync_isos x86_64

sync_debug_rpms aarch64 BaseOS AppStream PowerTools HighAvailability
sync_debug_rpms ppc64le BaseOS AppStream PowerTools HighAvailability ResilientStorage
sync_debug_rpms x86_64  BaseOS AppStream PowerTools HighAvailability ResilientStorage RT NFV

sync_source_rpms BaseOS AppStream PowerTools HighAvailability ResilientStorage RT NFV

rsync -a /compose/trees/$COMPOSE/COMPOSE_ID /old-compose/staged/$CENTOS_RELEASE/COMPOSE_ID
message "staging $(cat /compose/trees/$COMPOSE/COMPOSE_ID) complete"
