#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: |
##  Update localhost component with aborting script
##  1. setup local repository for maintained component with multiple version
##  2. check if the latest package is installable
##  3. check if all repositories are available
## bugs: [21127]
## roles-not: [basesystem]
## tags: [basic]
## packages:
##  - apache2 | apache2-mpm-prefork
## versions:
##  2.4-2: fixed
## exposure: dangerous

. pool.sh || exit 137

SH_LOG="${BASEDIR}/sh.log"
compname="test${RANDOM}"

setup_apache "${repoprefix}"

version1="${major}.${minor}-${_version_patchlevel}"
mkpdir "${major}.${minor}-0" maintained "${ARCH}"  # 0 1
# 0:dists/ucs500/main/binary-amd64/
# 1:dists/errata500/main/binary-amd64/
mkdeb "${pkgname}" 1 "${ARCH}" "${DIR_POOL}"
mkpkg "${DIR}" "${DIR_POOL}"
mksh "${DIR}" preup postup

mkpdir "${major}.${minor}--component/${compname}" maintained "${ARCH}" all  # 2 3
# 2:5.0/maintained/component/test00000/amd64/
# 3:5.0/maintained/component/test00000/all/
mkdeb "${pkgname}c" 1 all "${DIR}"
mkpkg "${DIR}" "${DIR}"
mksh "${DIR}" preup postup

declare -a BDIR=("${DIRS[@]}")

minor+=1
version2="${major}.${minor}-${_version_patchlevel}"
mkpdir "${major}.${minor}-${_version_patchlevel}" maintained "${ARCH}"  # 4 5
# 4:dists/ucs510/main/binary-amd64/
# 5:dists/errata510/main/binary-amd64/
mkdeb "${pkgname}" 2 "${ARCH}" "${DIR_POOL}"
mkpkg "${DIR}" "${DIR_POOL}"
mksh "${DIR}" preup postup

mkpdir "${major}.${minor}--component/${compname}" maintained "${ARCH}" all  # 6 7
# 6:5.1/maintained/component/test00000/amd64/
# 7:5.1/maintained/component/test00000/all/
mkdeb "${pkgname}c" 2 all "${DIR}"
mkpkg "${DIR}" "${DIR}"
mksh "${DIR}" --return 1 preup
mksh "${DIR}" postup

cat >"${SH_LOG}" <<__SH__
${DIR}/preup.sh [0-9]\\+ pre ${major}.${minor}-${_version_patchlevel}
__SH__

python3 create_releases_json.py "${REPODIR}" "$version1" "$version2"

comp="repository/online/component/${compname}"
config_repo "${comp}=yes" "${comp}/version=current"

	set -e
	checkapt "http://localhost\\(:80\\)\\?/${repoprefix}/" "${BDIR[0]}"
	apt-get -qq update
	apt-get -qq install "${pkgname}" "${pkgname}c"
	dpkg-query -W "${pkgname}" | grep -Fqx "${pkgname}	1"
	dpkg-query -W "${pkgname}c" | grep -Fqx "${pkgname}c	1"

	/usr/share/univention-updater/univention-updater net && rc=$? || rc=$?
	test 0 -ne $rc
	grep -qxf "${SH_LOG}" "${BASEDIR}/install.log"
	dpkg-query -W "${pkgname}" | grep -Fqx "${pkgname}	2" && false
	dpkg-query -W "${pkgname}c" | grep -Fqx "${pkgname}c	2" && false
	test "${_version_version}" = "$(ucr get version/version)"
	test "${_version_patchlevel:?}" = "$(ucr get version/patchlevel)"
# vim:set ft=sh:
