#!/bin/sh
set -eu

# install/upgrade RPMs that apply to OSTree
# Note: cockpit-selinux would be desirable, but needs setroubleshoot-server which isn't installed
cd /var/tmp/

# rhel4edge includes cockpit packages in the image itself, fedora-coreos overlays them
. /usr/lib/os-release
if [ "${ID:-}" = "rhel" ]; then
    rpm-ostree override replace --cache-only cockpit-bridge-*.rpm \
        cockpit-system-*.rpm
    rpm-ostree install --cache-only cockpit-tests-*.rpm
else
    rpm-ostree install --cache-only cockpit-bridge-*.rpm \
        cockpit-networkmanager-*.rpm cockpit-system-*.rpm cockpit-tests-*.rpm
fi

# rhel4edge has passwordless sudo but a loads of tests don't expect that
if [ "${ID:-}" = "rhel" ]; then
    sed -i '$ d' /etc/sudoers
fi

# update cockpit-ws and install scripts in the container
for rpm in /var/tmp/cockpit-ws-*.rpm /var/tmp/cockpit-bridge-*.rpm; do
    rpm2cpio "$rpm" | cpio -i --make-directories --directory=/var/tmp/install
done
podman run --name build-cockpit -i \
    -v /var/tmp/:/run/build:Z \
    quay.io/cockpit/ws sh -exc '
cp -a /run/build/install/* /
cp /run/build/containers/ws/label-* /run/build/containers/ws/default-bastion.conf /run/build/containers/ws/cockpit-auth-ssh-key /container/
'
podman commit --change CMD=/container/label-run build-cockpit localhost/cockpit/ws
podman rm -f build-cockpit

# move original quay.io image away, to make sure that our tests use the updated one
podman tag quay.io/cockpit/ws:latest quay.io/cockpit/original-ws:released
podman rmi quay.io/cockpit/ws:latest

# run cockpit/ws once to generate certificate; avoids slow down on every start
podman container runlabel INSTALL cockpit/ws
