#!/bin/bash
#
# Copyright (C) 2021 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

set -euo pipefail

. ${KOLA_EXT_DATA}/libtest.sh
cd $(mktemp -d)

set -x

rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/vmcheck.repo << EOF
[test-repo]
name=test-repo
baseurl=file:///${KOLA_EXT_DATA}/rpm-repos/0
gpgcheck=0
EOF

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
"")

# try an invalid source
if rpm-ostree override replace zincati --experimental --freeze --from foo=bar 2>err.txt; then
  assert_not_reached "Successfully replaced from source foo=bar?"
fi
assert_file_has_content_literal err.txt 'Parsing override source'

# try an invalid repo
if rpm-ostree override replace zincati --experimental --freeze --from repo=enoent 2>err.txt; then
  assert_not_reached "Successfully replaced from source repo=enoent?"
fi
assert_file_has_content_literal err.txt 'No matches'

# try a non-existent package
if rpm-ostree override replace enoent --experimental --freeze --from repo=test-repo 2>err.txt; then
  assert_not_reached "Successfully replaced non-existent package?"
fi
assert_file_has_content_literal err.txt 'No matches'

# query and fetch for the latest version of a package from enabled repos
rpmostree_assert_status \
  '.deployments[0]["base-local-replacements"]|length == 0' \
  '.deployments[0]["requested-base-local-replacements"]|length == 0'
rpm-ostree override replace zincati --experimental --freeze --from repo=test-repo
rpm-ostree status > status.txt
assert_file_has_content status.txt 'LocalOverrides: zincati .* -> 99.99-3'
rpmostree_assert_status \
  '.deployments[0]["base-local-replacements"]|length == 1' \
  '.deployments[0]["requested-base-local-replacements"]|length == 1'
# try it again, but using its NEVRA
# this also tests that we didn't delete the RPM
rpm-ostree cleanup -p
rpm-ostree override replace zincati-99.99-3.x86_64 --experimental --from repo=test-repo
rpm-ostree status > status.txt
assert_file_has_content status.txt 'LocalOverrides: zincati .* -> 99.99-3'
rpmostree_assert_status \
  '.deployments[0]["base-local-replacements"]|length == 1' \
  '.deployments[0]["requested-base-local-replacements"]|length == 1'
/tmp/autopkgtest-reboot "1"
;;
"1")
rpm-ostree status > status.txt
rpmostree_assert_status \
  '.deployments[0]["base-local-replacements"]|length == 1' \
  '.deployments[0]["requested-base-local-replacements"]|length == 1'
assert_file_has_content status.txt 'LocalOverrides: zincati .* -> 99.99-3'
zincati > zincati_version.txt
assert_file_has_content_literal zincati_version.txt '99.99-3'
rm -f zincati_version.txt
rm -f status.txt 
echo "ok override replace fetch from repos"
;;
*) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;
esac
