SERVICES=("bluetooth" "scx_loader")

update_user() {
  # Get user name and update it in config files.
  export LANG=C
  if usr=$(id -nu 1000) && [[ ! -z ${usr} ]]; then
     echo "Setting ${usr} as name for autologin and decky environment variables."
  elif usr=$(id -nu 1001) && [[ ! -z ${usr} ]]; then
     echo "Setting ${usr} as name for autologin and decky environment variables."
  else
     echo "Get user name failed, setting 'User=' in /etc/plasmalogin.conf.d/steam-deckify.conf"
  fi
  # Autologin
  sed -ri "s|^(User=).*|User=${usr}|" /etc/plasmalogin.conf.d/steam-deckify.conf
  # Decky loader
  sed -ri "s|^(DECKY_USER=).*|DECKY_USER=${usr}|" /etc/environment.d/handheld.conf
  sed -ri "s|^(DECKY_USER_HOME=).*|DECK_USER_HOME=/home/${usr}|" /etc/environment.d/handheld.conf

  # Return to Gaming Mode file
  cp /etc/skel/Desktop/steamos-gamemode.desktop /home/${usr}/Desktop/steamos-gamemode.desktop
}

scx_lavd_default() {
  # migrate all users to scx_loader
  local config_path="/etc/scx_loader.toml"
  if [[ ! -f "$config_path" ]]; then
     cat <<'EOF' > "$config_path"
default_sched = "scx_lavd"
EOF
  fi
}

steamos_steam() {
  # Grab the steam bootstrap for first boot
  URL="https://steamdeck-packages.steamos.cloud/archlinux-mirror/jupiter-main/os/x86_64/steam-jupiter-stable-1.0.0.79-1.1-x86_64.pkg.tar.zst"
  TMP_PKG="/tmp/package.pkg.tar.zst"
  OUTPUT_FILE="/tmp/bootstraplinux_ubuntu12_32.tar.xz"
  TARGET_FILE="/usr/lib/steam/bootstraplinux_ubuntu12_32.tar.xz"

  echo "Downloading $URL"
  curl -o "$TMP_PKG" "$URL"
  ZST_CHECKSUM=54b40e62f20202c5e9be83f7be9130976327a51981c6ac5841f83738ea191c168d6974563bcfe82b6a116c680e45a9dfe15709eb39f94a9f6cc1bcc7a53b7996
  TMP_PKG_CHECKSUM=$(sha512sum ${TMP_PKG} | cut -d " " -f1)
  if [[ "$ZST_CHECKSUM" == "$TMP_PKG_CHECKSUM" ]]; then
     echo "Extracting $OUTPUT_FILE"
     tar -I zstd -xvf "$TMP_PKG" usr/lib/steam/bootstraplinux_ubuntu12_32.tar.xz -O > "$OUTPUT_FILE"
  else
     echo "Download failed!"
     exit 1
  fi
   XZ_CHECKSUM=ba46762da3a809735e57c84afea525f703e6a5c67d2ab14e2dde949f2fdb490f198eb147deb5297349a788e5645949490574175935e66dfe11b6f9d4069140cc
  BS_CHECKSUM=$(sha512sum ${OUTPUT_FILE} | cut -d " " -f1)
  if [[ "$XZ_CHECKSUM" == "$BS_CHECKSUM" ]]; then
     echo "moving $OUTPUT_FILE to $TARGET_FILE"
     mv "$OUTPUT_FILE" "$TARGET_FILE"
  else
     echo "Extraction failed!"
     exit 1
  fi
  [[ -e "$TMP_PKG" ]] && rm "$TMP_PKG"
  [[ -e "$OUTPUT_FILE" ]] && rm "$OUTPUT_FILE"
}

post_install() {
  # Use scx_lavd as default scheduler
  scx_lavd_default

  echo "Enabling services..."
  for service in "${SERVICES[@]}"; do
    systemctl enable "$service" --now
  done

  # Enable Plasma Login Manager
  ln -sf /usr/lib/systemd/system/plasmalogin.service /etc/systemd/system/display-manager.service

  # First boot setup
  steamos_steam

  # Update users plasmalogin config to reflect the changes
  echo "Updating Handheld Configs."
  update_user
  # echo "If you are installing this without the CachyOS Installation, please update your username in the /etc/plasmalogin.conf.d/*"
}

pre_upgrade() {
  rm -f /etc/limine-entry-tool.d/20-chwd.conf
}

post_upgrade() {
  # Use scx_lavd as default scheduler
  scx_lavd_default

  # Enable Plasma Login Manager
  ln -sf /usr/lib/systemd/system/plasmalogin.service /etc/systemd/system/display-manager.service

  # Update files to reflect the changes
  echo "Updating Handheld Configs."
  update_user
}
