#!/bin/bash

set -uex

mock_config_dir="${WORKSPACE:-$PWD}/mock"
original_cfg_file="/etc/mock/${CHROOT_NAME}.cfg"
cfg_file="$mock_config_dir/${CHROOT_NAME}.cfg"
mkdir -p "$mock_config_dir"
ln -sf /etc/mock/templates "$mock_config_dir/"
ln -sf /etc/mock/logging.ini "$mock_config_dir/"

cp "$original_cfg_file" "$cfg_file"

if [[ $CHROOT_NAME == *epel-8-x86_64 ]]; then
    cat <<EOF >> "$cfg_file"
config_opts['module_setup_commands'] = [
  ('enable', 'javapackages-tools:201801'),
  ('disable',  'go-toolset')
]
EOF
fi

# Use dnf on CentOS 7
if [[ $CHROOT_NAME == *epel-7-x86_64 ]]; then
    MOCK_OPTIONS="--dnf --no-bootstrap-chroot${MOCK_OPTIONS:+ }$MOCK_OPTIONS"
fi

# disable bootstrap_image
# https://github.com/rpm-software-management/mock/issues/1184
MOCK_OPTIONS="--no-bootstrap-image${MOCK_OPTIONS:+ }$MOCK_OPTIONS"

# Allow BR: foo-devel < 1.2 to work when foo-devel-1.3 is actually available
cat <<EOF >> "$cfg_file"
config_opts['dnf.conf'] += """
[main]
best=0
"""
EOF

# shellcheck disable=SC2153
repo_adds=()
repo_dels=()

echo -e "config_opts['yum.conf'] += \"\"\"\n" >> "$cfg_file"

if [ -n "${ARTIFACTORY_URL:-}" ] && "$LOCAL_REPOS"; then
    repo_dels+=("--disablerepo=\*")

    if [ -n "${REPO_FILE_URL:-}" ]; then
        if [ -n "$REPO_FILES_PR" ]; then
            if [[ $REPO_FILES_PR = PR-* ]]; then
                build_number="lastSuccessfulBuild"
                branch="$REPO_FILES_PR"
                if [[ $branch = *:* ]]; then
                    build_number="${branch#*:}"
                    branch="${branch%:*}"
                fi
                REPO_FILE_URL="${JENKINS_URL:-https://build.hpdd.intel.com/}job/daos-do/job/repo-files/job/$branch/$build_number/artifact/"
            else
                if [ ! -d "$REPO_FILES_PR" ]; then
                    echo "Could not find your specified directory \"$REPO_FILES_PR\" to read repo-files from"
                    exit 1
                fi
                REPO_FILE_URL="file://$(readlink -e "$REPO_FILES_PR")/"
            fi
        fi
        curl -sSf "$REPO_FILE_URL"daos_ci-"$DISTRO"-mock-artifactory.repo >> "$cfg_file"
        repo_adds+=("--enablerepo *-artifactory")
    fi
fi

for repo in $DISTRO_BASE_PR_REPOS $PR_REPOS; do
    branch="master"
    build_number="lastSuccessfulBuild"
    if [[ $repo = *@* ]]; then
        branch="${repo#*@}"
        repo="${repo%@*}"
        if [[ $branch = *:* ]]; then
            build_number="${branch#*:}"
            branch="${branch%:*}"
        fi
    fi
    repo_adds+=("--enablerepo $repo:$branch:$build_number")
    echo -e "[$repo:$branch:$build_number]\n\
name=$repo:$branch:$build_number\n\
baseurl=${JENKINS_URL:-https://build.hpdd.intel.com/}job/daos-stack/job/$repo/job/$branch/$build_number/artifact/artifacts/$DISTRO/\n\
enabled=1\n\
gpgcheck=False\n" >> "$cfg_file"
done
for repo in $JOB_REPOS; do
    repo_name=${repo##*://}
    repo_name=${repo_name//\//_}
    repo_adds+=("--enablerepo $repo_name")
    echo -e "[${repo_name//@/_}]\n\
name=${repo_name}\n\
baseurl=${repo}\n\
enabled=1\n" >> "$cfg_file"
done
echo "\"\"\"" >> "$cfg_file"

if [ -n "$DISTRO_VERSION" ]; then
    releasever_opt=("--config-opts=releasever=$DISTRO_VERSION")
fi
# shellcheck disable=SC2086
if ! eval mock --configdir "$mock_config_dir" -r "${CHROOT_NAME}"           \
          ${repo_dels[*]} ${repo_adds[*]} --disablerepo=\*-debug*           \
          "${releasever_opt[@]}" $MOCK_OPTIONS $RPM_BUILD_OPTIONS "$TARGET"; then
    # Debug information for filing bugs on mock
    rpm -q mock
    mock --debug-config
fi
