#!/bin/bash -e

default_target="${TRAVIS+"coverage-codecov"}"
target="${default_target:-"coverage-codecovbash"}"

usage() {
    cat << EOF
Usage: container_run_ci [<options...>] ...

Options:
 -v, --verbose          execute with verbose output
 -h, -?, --help         display this help
 -t --target=<target>   The final target to be called in the build environment,
                        default based on local or CI environment: '$target'
                        Default alternatives are 'coverage-codecov' if
                        'travis CI' is detected and 'coverage-codecovbash'
                        otherwise.

Examples:
 container_run_ci
 container_run_ci --target=coverage-codecovbash
EOF
    exit "$1"
}

set -o pipefail

opts=$(getopt -o vht: --long verbose,help,target: -n 'parse-options' -- "$@") || usage 1
eval set -- "$opts"
while true; do
    case "$1" in
        -v | --verbose)
            set -x
            shift
            ;;
        -h | --help) usage 0 ;;
        -t | --target)
            target="$2"
            shift 2
            ;;
        --)
            shift
            break
            ;;
        *) break ;;
    esac
done

CI_ENV=$(bash <(curl -s https://codecov.io/env))

IMAGE=registry.opensuse.org/devel/openqa/containers/os-autoinst_dev
build_dir="${build_dir:-"build"}" # default is in accordance with file path in `.github/workflows/ci.yml`
cre="${cre:-"podman"}"
$cre pull $IMAGE
$cre images | grep opensuse

# shellcheck disable=SC2086
$cre run --env "QEMU_QMP_CONNECT_ATTEMPTS=10" \
    $CI_ENV --rm --entrypoint '' -v "$PWD":/opt $IMAGE bash -ec "
. /etc/os-release
if [[ \"$cre\" == \"docker\" && \"$ID\" == \"opensuse-leap\" ]]; then
    echo 'Newer docker versions need a patched qemu with disabled membarrier'
    zypper -n addrepo http://download.opensuse.org/repositories/devel:/openQA:/ci/openSUSE_Leap_\${VERSION} qemu
    zypper -n --gpg-auto-import-keys --no-gpg-checks refresh
    zypper -n in --from qemu qemu qemu-x86 qemu-tools qemu-ipxe qemu-sgabios qemu-seabios
fi
if [[ \"$target\" == \"coverage-codecovbash\" ]]; then
	zypper -n in perl-App-cpanminus
	cpanm -nq 'Devel::Cover::Report::Codecovbash'
fi
cd /opt
./tools/install-new-deps.sh
export CI=1 WITH_COVER_OPTIONS=1 CHECK_GIT_STATUS=1 PERL_TEST_WARNINGS_ONLY_REPORT_WARNINGS=$PERL_TEST_WARNINGS_ONLY_REPORT_WARNINGS
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B \"$build_dir\"
cmake --build \"$build_dir\" --verbose --target check-pkg-build
cmake --build \"$build_dir\" --verbose --target $target
cover -report html_minimal \"$build_dir\"/cover_db"
