#!/bin/bash
set -xeuo pipefail

. ${KOLA_EXT_DATA}/libtest.sh

tmpdir=$(mktemp -d)
cd ${tmpdir}
echo "using tmpdir: ${tmpdir}"
touch .testtmp
trap cleanup EXIT
function cleanup () {
  if test -z "${TEST_SKIP_CLEANUP:-}"; then
    if test -f "${tmpdir}"/.testtmp; then
      cd /
      rm "${tmpdir}" -rf
    fi
  else
    echo "Skipping cleanup of ${tmpdir}"
  fi
}

bootefidir=/boot/efi/EFI
bootupdir=/usr/lib/bootupd/updates
efiupdir=${bootupdir}/EFI
ostbaseefi=/usr/lib/ostree-boot/efi/EFI
efisubdir=fedora
efidir=${efiupdir}/${efisubdir}
ostefi=${ostbaseefi}/${efisubdir}
shim=shimx64.efi

test -f "${efidir}/${shim}"

prepare_efi_update() {
  test -w /usr
  mkdir -p ${ostbaseefi}
  cp -a ${efiupdir}.orig/* ${ostbaseefi}/
  rm -rf ${efiupdir} ${bootupdir}/EFI.json
}

systemctl start bootupd.socket

bootupctl status > out.txt
assert_file_has_content_literal out.txt 'Component EFI'
assert_file_has_content_literal out.txt '  Installed: grub2-efi-x64-'
assert_file_has_content_literal out.txt 'Update: At latest version'
ok status

if env LANG=C.UTF-8 runuser -u bin bootupctl status 2>err.txt; then
  fatal "Was able to bootupctl status as non-root"
fi
assert_file_has_content err.txt 'error:.*: Permission denied'

# From here we'll fake updates
test -w /usr || rpm-ostree usroverlay
# Save a backup copy of the update dir
cp -a ${efiupdir} ${efiupdir}.orig

prepare_efi_update
# FIXME need to synthesize an RPM for this
# echo somenewfile > ${ostefi}/somenew.efi
rm -v ${ostefi}/shim.efi
echo bootupd-test-changes >> ${ostefi}/grubx64.efi
bootupctl backend generate-update-metadata /
ver=$(jq -r .version < ${bootupdir}/EFI.json)
cat >ver.json << EOF
{ "version": "${ver},test" }
EOF
jq -s add ${bootupdir}/EFI.json ver.json > new.json
mv new.json ${bootupdir}/EFI.json

bootupctl status | tee out.txt
assert_file_has_content_literal out.txt 'Component EFI'
assert_file_has_content_literal out.txt '  Installed: grub2-efi-x64-'
assert_not_file_has_content out.txt '  Installed: grub2-efi-x64.*,test'
assert_file_has_content_literal out.txt 'Update: Available:'
ok update avail

bootupctl status --json > status.json
jq -r '.components.EFI.installed.version' < status.json > installed.txt
assert_file_has_content installed.txt '^grub2-efi-x64'

bootupctl update | tee out.txt
assert_file_has_content out.txt 'Updated EFI: grub2-efi-x64.*,test'

bootupctl status > out.txt
assert_file_has_content_literal out.txt 'Component EFI'
assert_file_has_content out.txt '  Installed: grub2-efi-x64.*,test'
assert_file_has_content_literal out.txt 'Update: At latest version'
ok status after update

# FIXME see above
# assert_file_has_content ${bootefidir}/${efisubdir}/somenew.efi 'somenewfile'
if test -f ${bootefidir}/${efisubdir}/shim.efi; then 
  fatal "failed to remove file"
fi
if ! grep -q 'bootupd-test-changes' ${bootefidir}/${efisubdir}/grubx64.efi; then
  fatal "failed to update modified file"
fi
cmp ${bootefidir}/${efisubdir}/shimx64.efi ${efiupdir}/${efisubdir}/shimx64.efi
ok filesystem changes

bootupctl update | tee out.txt
assert_file_has_content_literal out.txt 'No update available for any component'
assert_not_file_has_content_literal out.txt 'Updated EFI'

tap_finish
