#!/bin/bash
## Do not call this function directly. It should be called by another script that has vlaidated the
## DSDT Override. Expect unsafe behavior if this advice is ignored.
if [ $(whoami) != 'root' ]; then
  echo "You must be root to run this script."
  exit 1
fi

# Detect if the install media is running or not
if [ ! -d /tmp/frzr_root ]; then
  SUBVOL=""
  MOUNT_PATH=""
fi

ACPI_BUILD_DIR="/tmp/kernel/firmware/acpi"
BIOS_DATE=$(cat /sys/class/dmi/id/bios_date)
BIOS_RELEASE=$(cat /sys/class/dmi/id/bios_release)
BIOS_VENDOR=$(cat /sys/class/dmi/id/bios_vendor)
BIOS_VERSION=$(cat /sys/class/dmi/id/bios_version)
BOOTLOADER_CONFIG="${MOUNT_PATH}/boot/loader/entries/frzr.conf"
OVERRIDE_LOG="${MOUNT_PATH}/etc/device-quirks/dsdt_override.log"

# Grab previous stored values.
source $OVERRIDE_LOG

# Before doing anything we must check the checksum of the device in use against the known pool of patchable firmwares
cat /sys/firmware/acpi/tables/DSDT > system-dumped-dsdt.dat
iasl -d system-dumped-dsdt.dat
#iasl -tc system-dumped-dsdt.dat
checksum=$(sed -n 's/.*Checksum[[:space:]]\+\(0x[[:xdigit:]]\+\).*/\1/p' "system-dumped-dsdt.dsl")

APPLY_PATCH=""
echo "DSDT_OVERRIDES: $@"
for DSDT in $@; do
  echo "Checking DSDT: ${DSDT}"
  DEPLOYMENT_DSDT_PATH="${SUBVOL}/usr/lib/firmware/dsdt/${DSDT}"
  # Use sed to extract the checksum value from the DSL file
  checksum2=$(sed -n 's/.*Checksum[[:space:]]\+\(0x[[:xdigit:]]\+\).*/\1/p' "$DEPLOYMENT_DSDT_PATH")
  echo "Comparing checksum: $checksum to checksum: $checksum2"
  # Compare the checkqsums
  if [[ $checksum == $checksum2 ]]; then
    APPLY_PATCH=$DSDT
    echo "Checksums match."
    break
  elif [[ $BIOS_DATE == $LAST_BIOS_DATE ]] && \
       [[ $BIOS_RELEASE == $LAST_BIOS_RELEASE ]] && \
       [[ $BIOS_VENDOR == $LAST_BIOS_VENDOR ]] && \
       [[ $BIOS_VERSION == $LAST_BIOS_VERSION ]] && \
       [[ $DSDT == $LAST_DSDT ]]; then
    APPLY_PATCH=$DSDT
    echo "DSDT and BIOS Match previous insllation."
    break
  else
    echo "${DSDT} Does not match."
  fi
done
echo "List Complete"
if [[ $APPLY_PATCH != "" ]]; then
  IFS='.' read -ra split_arr <<< $APPLY_PATCH
  ACPI_OVERRIDE_DEVICE="${split_arr[0]}_acpi_override"
  if ! grep -q "${ACPI_OVERRIDE_DEVICE}" ${BOOTLOADER_CONFIG}; then
    if [ ! -d "${ACPI_BUILD_DIR}" ]; then
      # create directory for acpi DSDT overrides
      mkdir -p "${ACPI_BUILD_DIR}"
    fi
  
    cp ${DEPLOYMENT_DSDT_PATH} ${ACPI_BUILD_DIR}/dsdt.dsl
    echo "Compiling DSDT..."
    iasl -tc ${ACPI_BUILD_DIR}/dsdt.dsl
    cd /tmp/
    find kernel | cpio -H newc --create > ${ACPI_OVERRIDE_DEVICE}
    cp ${ACPI_OVERRIDE_DEVICE} ${MOUNT_PATH}/boot
    echo "Adding $ACPI_OVERRIDE_DEVICE to bootloader"
    sed -i "s|linux .*/vmlinuz-linux|&\ninitrd /${ACPI_OVERRIDE_DEVICE}|" ${BOOTLOADER_CONFIG}
    sed -i "/LAST_BIOS_DATE/c LAST_BIOS_DATE='$BIOS_DATE'" $OVERRIDE_LOG
    sed -i "/LAST_BIOS_RELEASE/c LAST_BIOS_RELEASE='$BIOS_RELEASE'" $OVERRIDE_LOG
    sed -i "/LAST_BIOS_VENDOR/c LAST_BIOS_VENDOR='$BIOS_VENDOR'" $OVERRIDE_LOG
    sed -i "/LAST_BIOS_VERSION/c LAST_BIOS_VERSION='$BIOS_VERSION'" $OVERRIDE_LOG
    sed -i "/LAST_DSDT/c LAST_DSDT='$DSDT'" $OVERRIDE_LOG
    echo "Done!"
  else
    echo "$ACPI_OVERRIDE_DEVICE already detected in bootloader, exiting"
  fi
else
  echo "Unable to find matching DSDT patch. Firmware override skipped."
fi
