#!/bin/bash
set -euo pipefail

# Colorful output.
function greenprint {
  echo -e "\033[1;32m[$(date -Isecond)] ${1}\033[0m"
}

ALL_ARCHES="aarch64 ppc64le s390x x86_64"

if [ -e ../tools/define-compose-url.sh ]
then
    source ../tools/define-compose-url.sh
else
    source ./tools/define-compose-url.sh
fi

# Create a repository file for installing the osbuild-composer RPMs
greenprint "📜 Generating dnf repository file"
rm -f rhel"${VERSION_ID%.*}"internal.repo
for ARCH in $ALL_ARCHES; do
    tee -a rhel"${VERSION_ID%.*}"internal.repo << EOF

[rhel${VERSION_ID}-internal-baseos-${ARCH}]
name=RHEL Internal BaseOS
baseurl=${COMPOSE_URL}/compose/BaseOS/${ARCH}/os/
enabled=1
gpgcheck=0
# Default dnf repo priority is 99. Lower number means higher priority.
priority=1

[rhel${VERSION_ID}-internal-appstream-${ARCH}]
name=RHEL Internal AppStream
baseurl=${COMPOSE_URL}/compose/AppStream/${ARCH}/os/
enabled=1
gpgcheck=0
# osbuild-composer repo priority is 5
priority=1
EOF
done


# Create an osbuild-composer JSON file for content consumption
greenprint "📜 Generate osbuild-composer JSON source file"
tee rhel-"${VERSION_ID%.*}".json << EOF
{
EOF

for ARCH in $ALL_ARCHES; do
    tee -a rhel-"${VERSION_ID%.*}".json << EOF
    "${ARCH}": [
        {
            "name": "baseos-${ARCH}",
            "baseurl": "${COMPOSE_URL}/compose/BaseOS/${ARCH}/os/",
            "gpgkey": "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nmQINBErgSTsBEACh2A4b0O9t+vzC9VrVtL1AKvUWi9OPCjkvR7Xd8DtJxeeMZ5eF\n0HtzIG58qDRybwUe89FZprB1ffuUKzdE+HcL3FbNWSSOXVjZIersdXyH3NvnLLLF\n0DNRB2ix3bXG9Rh/RXpFsNxDp2CEMdUvbYCzE79K1EnUTVh1L0Of023FtPSZXX0c\nu7Pb5DI5lX5YeoXO6RoodrIGYJsVBQWnrWw4xNTconUfNPk0EGZtEnzvH2zyPoJh\nXGF+Ncu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lm6MJ/h582L6fJLNEu136UWDtJj5eBYEzX13l+d\nSC4PEHx7ZZRwQKptl9NkinLZGJztg175paUu8C34sAv+SQnM20c0pdOXAq9GKKhi\nvt61kpkXoRGxjTlc6h+69aidSg==\n=ls8J\n-----END PGP PUBLIC KEY BLOCK-----\n"
        },
        {
            "name": "appstream-${ARCH}",
            "baseurl": "${COMPOSE_URL}/compose/AppStream/${ARCH}/os/",
            "gpgkey": "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nmQINBErgSTsBEACh2A4b0O9t+vzC9VrVtL1AKvUWi9OPCjkvR7Xd8DtJxeeMZ5eF\n0HtzIG58qDRybwUe89FZprB1ffuUKzdE+HcL3FbNWSSOXVjZIersdXyH3NvnLLLF\n0DNRB2ix3bXG9Rh/RXpFsNxDp2CEMdUvbYCzE79K1EnUTVh1L0Of023FtPSZXX0c\nu7Pb5DI5lX5YeoXO6RoodrIGYJsVBQWnrWw4xNTconUfNPk0EGZtEnzvH2zyPoJh\nXGF+Ncu9XwbalnYde10OCvSWAZ5zTCpoLMTvQjWpbCdWXJzCm6G+/hx9upke546H\n5IjtYm4dTIVTnc3wvDiODgBKRzOl9rEOCIgOuGtDxRxcQkjrC+xvg5Vkqn7vBUyW\n9pHedOU+PoF3DGOM+dqv+eNKBvh9YF9ugFAQBkcG7viZgvGEMGGUpzNgN7XnS1gj\n/DPo9mZESOYnKceve2tIC87p2hqjrxOHuI7fkZYeNIcAoa83rBltFXaBDYhWAKS1\nPcXS1/7JzP0ky7d0L6Xbu/If5kqWQpKwUInXtySRkuraVfuK3Bpa+X1XecWi24JY\nHVtlNX025xx1ewVzGNCTlWn1skQN2OOoQTV4C8/qFpTW6DTWYurd4+fE0OJFJZQF\nbuhfXYwmRlVOgN5i77NTIJZJQfYFj38c/Iv5vZBPokO6mffrOTv3MHWVgQARAQAB\ntDNSZWQgSGF0LCBJbmMuIChyZWxlYXNlIGtleSAyKSA8c2VjdXJpdHlAcmVkaGF0\nLmNvbT6JAjYEEw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K0SAHVZcuW0LMMXnGe9f4UsEUgCNwo5mvLWPxzKqFq\n6/G3kEZVFwZ0qrlLoJPeHNbOcfkeZ9NgD/OhzQmdylM0IwGM9DMrm2YS4EVsmm2b\n53qKIfIyysp1yAGcTnBwBbZ85osNBl2KRDIPhMs0bnmGB7IAvwlSb+xm6vWKECkO\nlwQDO5Kg8YZ8+Z3pn/oS688t/fPXvWLZYUqwR63oWfIaPJI7Ahv2jJmgw1ofL81r\n2CE3T/OydtUeGLzqWJAB8sbUgT3ug0cjtxsHuroQBSYBND3XDb/EQh5GeVVnGKKH\ngESLFAoweoNjDSXrlIu1gFjCDHF4CqBRmNYKrNQjLmhCrSfwkytXESJwlLzFKY8P\nK1yZyTpDC9YK0G7qgrk7EHmH9JAZTQ5V65pp0vR9KvqTU5ewkQDIljD2f3FIqo2B\nSKNCQE+N6NjWaTeNlU75m+yZocKObSPg0zS8FAuSJetNtzXA7ouqk34OoIMQj4gq\nUnh/i1FcZAd4U6Dtr9aRZ6PeLlm6MJ/h582L6fJLNEu136UWDtJj5eBYEzX13l+d\nSC4PEHx7ZZRwQKptl9NkinLZGJztg175paUu8C34sAv+SQnM20c0pdOXAq9GKKhi\nvt61kpkXoRGxjTlc6h+69aidSg==\n=ls8J\n-----END PGP PUBLIC KEY BLOCK-----\n"
        }
EOF

    # only x86_64 has an RT repository
    if [ "$ARCH" == "x86_64" ]; then
        tee -a rhel-"${VERSION_ID%.*}".json << EOF
        ,{
            "name": "rt-${ARCH}",
            "baseurl": "${COMPOSE_URL}/compose/RT/${ARCH}/os/",
            "gpgkey": "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nmQINBErgSTsBEACh2A4b0O9t+vzC9VrVtL1AKvUWi9OPCjkvR7Xd8DtJxeeMZ5eF\n0HtzIG58qDRybwUe89FZprB1ffuUKzdE+HcL3FbNWSSOXVjZIersdXyH3NvnLLLF\n0DNRB2ix3bXG9Rh/RXpFsNxDp2CEMdUvbYCzE79K1EnUTVh1L0Of023FtP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6NjWaTeNlU75m+yZocKObSPg0zS8FAuSJetNtzXA7ouqk34OoIMQj4gq\nUnh/i1FcZAd4U6Dtr9aRZ6PeLlm6MJ/h582L6fJLNEu136UWDtJj5eBYEzX13l+d\nSC4PEHx7ZZRwQKptl9NkinLZGJztg175paUu8C34sAv+SQnM20c0pdOXAq9GKKhi\nvt61kpkXoRGxjTlc6h+69aidSg==\n=ls8J\n-----END PGP PUBLIC KEY BLOCK-----\n"
        }
EOF
    fi

    tee -a rhel-"${VERSION_ID%.*}".json << EOF
    ]
EOF

    # append comma for all arches except the last one
    if [ "$ARCH" != "x86_64" ]; then
        tee -a rhel-"${VERSION_ID%.*}".json << EOF
        ,
EOF
    fi
done

tee -a rhel-"${VERSION_ID%.*}".json << EOF
}
EOF

cp rhel-"${VERSION_ID%.*}".json rhel-"${VERSION_ID%.*}"-beta.json

JOB_NAME="${JOB_NAME:-${CI_JOB_ID}}"
# Do not create tests repo if it's provided from ENV
if [ -z "${REPO_URL+x}" ]; then
    greenprint "📦 Installing requirements"
    sudo dnf -y install createrepo_c wget python3-pip

    # Install s3cmd if it is not present.
    if ! s3cmd --version > /dev/null 2>&1; then
        greenprint "📦 Installing s3cmd"
        sudo pip3 -q install s3cmd
    fi

    REPO_DIR_LATEST="repo/${JOB_NAME}/latest/internal"
    mkdir -p "$REPO_DIR_LATEST"

    greenprint "Discover latest osbuild-composer NVR"
    # Download only osbuild-composer-worker-*.rpm and use it in rpm --qf below
    wget --quiet --recursive --no-parent --no-directories --accept "osbuild-composer-worker-*.rpm" \
        "${COMPOSE_URL}/compose/AppStream/x86_64/os/Packages/"

    # Download osbuild-composer-tests from Brew b/c it is not available in the distro
    # version matches osbuild-composer from the internal tree
    greenprint "Downloading osbuild-composer-tests RPMs from Brew"
    for ARCH in $ALL_ARCHES; do
        TESTS_RPM_URL=$(rpm -qp ./osbuild-composer-worker-*.rpm --qf "http://download.devel.redhat.com/brewroot/vol/rhel-${VERSION_ID%.*}/packages/osbuild-composer/%{version}/%{release}/$ARCH/osbuild-composer-tests-%{version}-%{release}.$ARCH.rpm")
        wget --directory-prefix "$REPO_DIR_LATEST" "$TESTS_RPM_URL"
    done

    greenprint "⛓ Creating dnf repository"
    createrepo_c "${REPO_DIR_LATEST}"

    # Bucket in S3 where our artifacts are uploaded
    REPO_BUCKET=osbuild-composer-repos

    # Remove the previous latest repo for this job.
    # Don't fail if the path is missing.
    AWS_ACCESS_KEY_ID="$V2_AWS_ACCESS_KEY_ID" \
    AWS_SECRET_ACCESS_KEY="$V2_AWS_SECRET_ACCESS_KEY" \
    s3cmd --recursive rm "s3://${REPO_BUCKET}/${REPO_DIR_LATEST}" || true

    # Upload repository to S3.
    greenprint "☁ Uploading RPMs to S3"
    AWS_ACCESS_KEY_ID="$V2_AWS_ACCESS_KEY_ID" \
    AWS_SECRET_ACCESS_KEY="$V2_AWS_SECRET_ACCESS_KEY" \
    s3cmd --acl-public put --recursive repo/ s3://${REPO_BUCKET}/repo/

    # Public URL for the S3 bucket with our artifacts.
    MOCK_REPO_BASE_URL="http://osbuild-composer-repos.s3-website.us-east-2.amazonaws.com"

    # Full URL to the RPM repository after they are uploaded.
    REPO_URL=${MOCK_REPO_BASE_URL}/${REPO_DIR_LATEST}
fi

# amend repository file.
greenprint "📜 Amend dnf repository file"
tee -a rhel"${VERSION_ID%.*}"internal.repo << EOF

[osbuild-composer-tests-multi-arch]
name=Tests ${JOB_NAME}
baseurl=${REPO_URL}
enabled=1
gpgcheck=0
# osbuild-composer repo priority is 5
priority=1
EOF
