#!/usr/bin/env bash
# This script performs a check for system updates that require a reboot
# to be applied. As result it populates a file:
# - If no reboot needed -> delete the file
# - If reboot is needed and the file doesn't exist -> create it with the current TS
# - If reboot is needed and the file exists -> do nothing

source /usr/local/bin/te-reboot-lib.sh

STATUS_FILE=/var/lib/teva/restart-required

check() {
    if is_reboot_required; then
        # Nothing to do if the file already exists and contains a timestamp
        [[ -f $STATUS_FILE && $(grep -c -E '^[0-9]+$' "$STATUS_FILE") == 1 ]] && return

        date +%s > "$STATUS_FILE"
    else
        rm -f "$STATUS_FILE"
    fi 
}

check
