#!/bin/sh
# Remember this boot. If the stored id is from an earlier boot, shutdown
# never cleared it, so the previous stop was not orderly.
set -u

STATE=${ZOS_LOG_COLLECTOR_STATE:-/var/lib/zos-log-collector}
BOOT_ID_PATH=${ZOS_LOG_COLLECTOR_BOOT_ID:-/proc/sys/kernel/random/boot_id}

mkdir -p "$STATE"
if [ ! -r "$BOOT_ID_PATH" ]; then
    exit 0
fi
boot_id=$(cat "$BOOT_ID_PATH")
if [ -z "$boot_id" ]; then
    exit 0
fi

if [ -f "$STATE/booted" ]; then
    previous=$(cat "$STATE/booted" 2>/dev/null || true)
    if [ -n "$previous" ] && [ "$previous" != "$boot_id" ]; then
        touch "$STATE/collect-pending"
    fi
fi

printf '%s\n' "$boot_id" > "$STATE/booted"
chmod 0644 "$STATE/booted" 2>/dev/null || true
exit 0
