#!/bin/bash

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2025-present ArchR (https://github.com/archr-linux/Arch-R)

. /etc/profile

if [ -z "${DEVICE_SERIAL_NUMBER_PATH}" ]; then
  log $0 "SERIAL NUMBER CHECK: This device has no unique serial number to check, skipping and continue to Emulation Station"
  exit 0
else
  DEVICE_SERIAL_NUMBER="$(cat ${DEVICE_SERIAL_NUMBER_PATH})"
  log $0 "SERIAL NUMBER CHECK: DEVICE SERIAL NUMBER: ${DEVICE_SERIAL_NUMBER}"
fi

STORED_SERIAL_NUMBER=$(get_setting "device.serial.number")
if [ ! -n "${STORED_SERIAL_NUMBER}" ]; then
  set_setting "device.serial.number" "${DEVICE_SERIAL_NUMBER}"
  echo 1 > "/storage/serial_number_check_status"
  log $0 "SERIAL NUMBER CHECK: No storted serial number found, saving SOC serial number to system.cfg and show warning."
elif [ ! "${STORED_SERIAL_NUMBER}" == "${DEVICE_SERIAL_NUMBER}" ]; then
  set_setting "device.serial.number" "${DEVICE_SERIAL_NUMBER}"
  echo 1 > "/storage/serial_number_check_status"
  log $0 "SERIAL NUMBER CHECK: Device and stored serial numbers do not match, show warning."
else
  log $0 "SERIAL NUMBER CHECK: Device and stored serial numbers match, continue to Emulation Station."
fi
