#!/usr/bin/bash

IMAGE_INFO="/usr/share/ublue-os/image-info.json"
IMAGE_NAME=$(jq -r '."image-name"' < $IMAGE_INFO)
IMAGE_BRANCH=$(jq -r '."image-branch"' < $IMAGE_INFO)
IMAGE_FLAVOR=$(jq -r '."image-flavor"' < $IMAGE_INFO)
FEDORA_VERSION=$(jq -r '."fedora-version"' < $IMAGE_INFO)

# SCRIPT VERSION
HWS_VER=36
HWS_VER_FILE="/etc/bazzite/hws_version"
HWS_VER_RAN=$(cat $HWS_VER_FILE)

# IMAGE IDENTIFIERS
KNOWN_IMAGE_NAME_FILE="/etc/bazzite/image_name"
KNOWN_IMAGE_NAME=$(cat $KNOWN_IMAGE_NAME_FILE)
KNOWN_IMAGE_BRANCH_FILE="/etc/bazzite/image_branch"
KNOWN_IMAGE_BRANCH=$(cat $KNOWN_IMAGE_BRANCH_FILE)
KNOWN_IMAGE_FLAVOR_FILE="/etc/bazzite/image_flavor"
KNOWN_IMAGE_FLAVOR=$(cat $KNOWN_IMAGE_FLAVOR_FILE)
KNOWN_FEDORA_VERSION_FILE="/etc/bazzite/fedora_version"
KNOWN_FEDORA_VERSION=$(cat $KNOWN_FEDORA_VERSION_FILE)

# INITRAMFS CONFIG
INITRAMFS_DIR="/etc/bazzite/initramfs"
INITRAMFS_REBUILD_TRIGGER="$INITRAMFS_DIR/rebuild"
INITRAMFS_BAZZITE_ARG_FILE="$INITRAMFS_DIR/hardware-setup.args"
INITRAMFS_BAZZITE_ARGS=$(cat $INITRAMFS_BAZZITE_ARG_FILE)
INITRAMFS_CUSTOM_ARGS_DIR="$INITRAMFS_DIR/args.d"
INITRAMFS_CUSTOM_ARGS=""

# If the /etc/bazzite/initramfs/args.d dir exists
if [[ -d "$INITRAMFS_CUSTOM_ARGS_DIR" ]]; then
  # Grab all the content of the files (minus comments) and join them together with spaces
  INITRAMFS_CUSTOM_ARGS=$(grep -RvP "^(#|$)" $INITRAMFS_CUSTOM_ARGS_DIR/ | sort | perl -pe 's/(^$INITRAMFS_CUSTOM_ARGS_DIR\/.+:|\\\n$)//g' | tr '\n' ' ')
fi

# If no rebuild is requested
if [[ ! -f $INITRAMFS_REBUILD_TRIGGER ]]; then
  # RUN REMAINDER OF SCRIPT ONLY IF UPDATED
  if [[ -f $HWS_VER_FILE && $HWS_VER = "$HWS_VER_RAN" ]]; then
    if [[ -f $KNOWN_IMAGE_NAME_FILE && -f $KNOWN_IMAGE_FLAVOR_FILE && -f $KNOWN_FEDORA_VERSION_FILE && -f $KNOWN_IMAGE_BRANCH_FILE ]]; then
      # Run script if image has been rebased
      if [[ $IMAGE_NAME = "$KNOWN_IMAGE_NAME" && $IMAGE_FLAVOR = "$KNOWN_IMAGE_FLAVOR" && $FEDORA_VERSION = "$KNOWN_FEDORA_VERSION" && $IMAGE_BRANCH = "$KNOWN_IMAGE_BRANCH" ]]; then
        echo "Hardware setup v$HWS_VER has already run. Exiting..."
        exit 0
      fi
    fi
  fi
else
  # REMOVE REBUILD TRIGGER TO AVOID INFINITE BOOT LOOP!
  rm $INITRAMFS_REBUILD_TRIGGER
  # Rebuild current initramfs (this will add any new configs added to /etc/dracut.conf.d/) then reboot
  echo "Initramfs rebuild triggered, using: $INITRAMFS_BAZZITE_ARGS $INITRAMFS_CUSTOM_ARGS"
  plymouth display-message --text="Updating initramfs - Please wait, this may take a while" || true
  rpm-ostree initramfs --enable "${INITRAMFS_BAZZITE_ARGS} ${INITRAMFS_CUSTOM_ARGS}" --reboot
  exit 0 
fi

# GLOBAL
SYS_ID="$(cat /sys/devices/virtual/dmi/id/product_name)"
MINIMUM_FREE_ZRAM=$(awk '/MemTotal/ {printf "%.0f", $2 * 0.01}' /proc/meminfo)
CURRENT_FREE_ZRAM=$(sysctl vm.min_free_kbytes | awk '{print $3}')
KARGS=$(rpm-ostree kargs)
NEEDED_KARGS=()
INITRAMFS=$(rpm-ostree initramfs)
NEEDED_INITRAMFS=("/etc/crypttab")
NEEDS_INITRAMFS_APPLICATION=false

# INITRAMFS
echo "Current initramfs: $INITRAMFS"

if [[ $IMAGE_FLAVOR =~ "nvidia" ]]; then
  NEEDED_INITRAMFS+=("/usr/lib/modprobe.d/nvidia.conf")
else
  NEEDED_INITRAMFS+=("/etc/modprobe.d/amdgpu.conf")
fi

if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME =~ "ally" || $IMAGE_NAME =~ "framegame" ]]; then
  NEEDED_INITRAMFS+=("/etc/modprobe.d/deck-blacklist.conf")
fi

for INITRAMFS_ARG in ${NEEDED_INITRAMFS[@]}; do
  if [[ ! $INITRAMFS =~ "$INITRAMFS_ARG" ]]; then
    NEEDS_INITRAMFS_APPLICATION=true
  fi
done

# If there is nothing in INITRAMFS_BAZZITE_ARGS
if [[ ! $INITRAMFS_BAZZITE_ARGS ]]; then
  # Make sure we will have /etc/bazzite/initramfs/bazzite.args for the future
  mkdir -p /etc/bazzite/initramfs/args.d
  echo "--arg=\"-I ${NEEDED_INITRAMFS[*]}\"" > $INITRAMFS_BAZZITE_ARG_FILE
  INITRAMFS_BAZZITE_ARGS=$(cat $INITRAMFS_BAZZITE_ARG_FILE)
fi

if $NEEDS_INITRAMFS_APPLICATION; then
  echo "Found needed initramfs changes, applying the following: ${NEEDED_INITRAMFS[*]}"
  echo "--arg=\"-I ${NEEDED_INITRAMFS[*]}\"" > $INITRAMFS_BAZZITE_ARG_FILE
  INITRAMFS_BAZZITE_ARGS=$(cat $INITRAMFS_BAZZITE_ARG_FILE)
  echo "Building initramfs with following custom args: $INITRAMFS_CUSTOM_ARGS"
  plymouth display-message --text="Updating initramfs - Please wait, this may take a while" || true
  rpm-ostree initramfs --enable "${INITRAMFS_BAZZITE_ARGS} ${INITRAMFS_CUSTOM_ARGS}"
else
  echo "No initramfs changes needed"
fi

# KERNEL ARGUMENTS
echo "Current kargs: $KARGS"

if /usr/libexec/hardware/valve-hardware; then
  echo "Checking for needed karg changes (Jupiter/Galileo)"

  if [[ ! $KARGS =~ "amd_iommu" ]]; then
    NEEDED_KARGS+=("--append-if-missing=amd_iommu=off")
  fi

  if [[ ! $KARGS =~ "amdgpu.gttsize" ]]; then
    if [[ "$(awk '/MemTotal/{print $(NF-1)}' /proc/meminfo)" == "31664740" ]]; then
      echo "32GB RAM Steam Deck detected"
      NEEDED_KARGS+=("--append-if-missing=amdgpu.gttsize=16256")
    else
      NEEDED_KARGS+=("--append-if-missing=amdgpu.gttsize=8128")
    fi
  fi
fi

if [[ $IMAGE_NAME =~ "deck" && ":AOKZOE A1 AR07:" =~ ":$SYS_ID:" ]]; then
  echo "AOKZOE A1 on deck build detected, fixing edid"
  if [[ ! $KARGS =~ "drm.edid_firmware" ]]; then
    NEEDED_KARGS+=("--append-if-missing=drm.edid_firmware=eDP-1:edid/aokzoea1ar07_edid.bin")
  fi
fi

if [[ ":Galileo:" =~ ":$SYS_ID:" ]]; then
  if [[ ! $KARGS =~ "video" ]]; then
    echo "Adding panel orientation for Steam Deck OLED"
    NEEDED_KARGS+=("--append-if-missing=video=eDP-1:panel_orientation=right_side_up")
  fi
elif [[ ":83E1:" =~ ":$SYS_ID:" ]]; then
  if [[ ! $KARGS =~ "video" ]]; then
    echo "Adding panel orientation for Lenovo Legion Go"
    NEEDED_KARGS+=("--append-if-missing=video=eDP-1:panel_orientation=left_side_up")
  fi
  if [[ ! $KARGS =~ "amdgpu.gttsize" ]]; then
    echo "Adding GTTSize for Lenovo Legion Go"
    NEEDED_KARGS+=("--append-if-missing=amdgpu.gttsize=8128")
  fi
  if [[ ! $KARGS =~ "amdgpu.sg_display" ]]; then
    echo "Adding SG Display for Lenovo Legion Go"
    NEEDED_KARGS+=("--append-if-missing=amdgpu.sg_display=0")
  fi
elif [[ ":ROG Ally RC71L_RC71L:ROG Ally RC71L:" =~ ":$SYS_ID:" ]]; then
  if [[ ! $KARGS =~ "amdgpu.gttsize" ]]; then
    echo "Adding GTTSize for ASUS Ally"
    NEEDED_KARGS+=("--append-if-missing=amdgpu.gttsize=8128")
  fi
  if [[ ! $KARGS =~ "amdgpu.sg_display" ]]; then
    echo "Adding SG Display for ASUS Ally"
    NEEDED_KARGS+=("--append-if-missing=amdgpu.sg_display=0")
  fi
elif [[ ":G1617-01:" =~ ":$SYS_ID:" ]]; then
  if [[ ! $KARGS =~ "video" ]]; then
    echo "Adding panel orientation for GPD Win Mini"
    NEEDED_KARGS+=("--append-if-missing=video=eDP-1:panel_orientation=right_side_up")
  fi
elif [[ ":WIN2:" =~ ":$SYS_ID:" ]]; then
  if [[ ! $KARGS =~ "video" ]]; then
    echo "Adding panel orientation for GPD Win 2"
    NEEDED_KARGS+=("--append-if-missing=video=eDP-1:panel_orientation=right_side_up")
  fi
fi

if /usr/libexec/hardware/simpledeckytdp-supported-hardware; then
  if [[ ! $KARGS =~ "iomem" ]]; then
    echo "Adding needed kargs for ryzenadj"
    NEEDED_KARGS+=("--append-if-missing=iomem=relaxed")
  fi
fi

if [[ $KARGS =~ "nomodeset" ]]; then
  echo "Removing nomodeset"
  NEEDED_KARGS+=("--delete-if-present=nomodeset")
fi

if [[ ! $KARGS =~ "gpu_sched.sched_policy" ]]; then
  echo "Fixing GPU scheduler default"
  NEEDED_KARGS+=("--append-if-missing=gpu_sched.sched_policy=0")
fi

if [[ -n "$NEEDED_KARGS" ]]; then
  echo "Found needed karg changes, applying the following: ${NEEDED_KARGS[*]}"
  plymouth display-message --text="Updating kargs - Please wait, this may take a while" || true
  rpm-ostree kargs ${NEEDED_KARGS[*]} --reboot || exit 1
else
  echo "No karg changes needed"
fi

if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME =~ "ally" || $IMAGE_NAME =~ "framegame" ]]; then
  if /usr/libexec/hardware/valve-hardware; then
    if [[ ":Jupiter:" =~ ":$SYS_ID:" ]]; then
      RESOLUTION=$(lshw -json -c display | jq -r .[]."configuration"."resolution")
      if [[ "${RESOLUTION}" = "1200,1920" ]]; then
        echo "Jupiter with DeckHD detected, disabling BIOS updates..."
        systemctl disable --now jupiter-biosupdate.service
      elif [[ "$(awk '/MemTotal/{print $(NF-1)}' /proc/meminfo)" == "31664740" ]]; then
        echo "Jupiter with 32GB RAM detected, disabling BIOS updates..."
        systemctl disable --now jupiter-biosupdate.service
      fi
    fi

    systemctl enable --now jupiter-fan-control.service
    systemctl enable --now vpower.service
  else
    echo "Generic device detected. Performing setup..."
    systemctl disable --now jupiter-fan-control.service
    systemctl disable --now vpower.service
    systemctl disable --now jupiter-biosupdate.service
    systemctl disable --now jupiter-controller-update.service
    systemctl disable --now ryzenadj.service
    systemctl disable --now batterylimit.service
  fi
  systemctl enable --now ds-inhibit.service
fi

# FSTAB CONFIGURATION
if [[ $(grep "compress=zstd" /etc/fstab) ]]; then
  echo "Applying fstab param adjustments"
  if grep -q '64GB' <<< "$(lsblk -o MODEL)"; then
    echo "64GB eMMC detected"
    sed -i 's/compress=zstd:1/noatime,lazytime,discard=sync,compress-force=zstd:3,space_cache=v2/g' /etc/fstab
  else
    sed -i 's/compress=zstd:1/noatime,lazytime,commit=120,discard=async,compress-force=zstd:1,space_cache=v2/g' /etc/fstab
  fi
else
  echo "No fstab param adjustments needed"
fi

# ZRAM MINIMUM-FREE CONFIGURATION
echo "Current minimum-free ZRAM value: $CURRENT_FREE_ZRAM"

if ((MINIMUM_FREE_ZRAM > CURRENT_FREE_ZRAM)); then
    sysctl -w "vm.min_free_kbytes=${MINIMUM_FREE_ZRAM}"
    echo "Found needed minimum-free ZRAM changes, applying the following: ${MINIMUM_FREE_ZRAM}"
else
  echo "No minimum-free ZRAM changes needed"
fi

# HOSTNAME FIX
# If the hostname is too long Distrobox will fail during setup
# Let's check the length and reset it to something sensible if that happens.
if (( $(hostname | wc -m) > 20 )); then
  hostnamectl set-hostname bazzite
fi

# Set default target to graphical, fixes rebase from base image
if grep -qv "graphical.target" <<< "$(systemctl get-default)"; then
  systemctl set-default graphical.target
fi

mkdir -p /etc/bazzite
echo $HWS_VER > $HWS_VER_FILE
echo $IMAGE_NAME > $KNOWN_IMAGE_NAME_FILE
echo $IMAGE_FLAVOR > $KNOWN_IMAGE_FLAVOR_FILE
echo $FEDORA_VERSION > $KNOWN_FEDORA_VERSION_FILE
echo $IMAGE_BRANCH > $KNOWN_IMAGE_BRANCH_FILE

if [[ $IMAGE_FLAVOR =~ "nvidia" ]]; then
  echo "Nvidia image detected, rebooting to avoid screen freeze issue in 550."
  systemctl reboot
fi