#!/bin/sh
#
# Called by the version page before it uploads an offline update bundle (custom_btn_callback, returns at once).
# Refuses while another update runs, and resets the update state to "uploading" so that the page does not read the
# result of the update before (which would look like "finished").

if systemctl is-active --quiet eurogard-ota-update.service || systemctl is-active --quiet eurogard-offline-update.service; then
    exit 1
fi

# the fleet client runs eurogard_ota_update in the foreground, it holds this lock
exec 9> /run/eurogard_ota_update.lock
flock -n 9 || exit 1

echo uploading > /run/eurogard_ota_update.state
usi set eurogard_version.installState uploading > /dev/null 2>&1

exit 0
