#!/usr/bin/env bash
# This script takes one argument, which is the ACTION_TYPE
#
# ACTION_TYPE can be one of the following:
#   LID_OPEN - The script is called when the lid is opened
#   LID_CLOSE - The script is called when the lid is closed
#
# Requirements for using clamshell mode:
#   - wm.clamshell.enabled is set to true
#   - An inbuilt display is found
#   - More than one display is connected

ACTION_TYPE=$1


exit_with_error() {
  echo "ERROR: $1"
  echo "Exiting..."
  exit 1
}

get_lid_close_cmd() {
  # Actions to take based on the value of wm.lidclose.action
  local DEFAULT_LOCK="gtklock --background $(trawlcat regolith.lockscreen.wallpaper.file /dev/null)"
  local LOCK=$( trawlcat wm.program.lock "$DEFAULT_LOCK")
  local SLEEP=$( trawlcat wm.program.sleep "systemctl suspend" )
  local HIBERNATE=$( trawlcat wm.program.hibernate "systemctl hibernate" )
  local SHUTDOWN=$( trawlcat wm.program.shutdown "gnome-session-quit --power-off --no-prompt")

  case "$1" in
    "DO_NOTHING")
      echo "true"
      ;;

    "LOCK")
      echo "$LOCK"
      ;;

    "SLEEP")
      echo "$SLEEP"
      ;;

    "HIBERNATE")
      echo "$HIBERNATE"
      ;;

    "SHUTDOWN")
      echo "$SHUTDOWN"
      ;;

    *)
      exit_with_error "Invalid or missing value for resource wm.lidclose.action: $1"
  esac
}

handle_clamshell() {
  if [[ "$ACTION_TYPE" == "LID_CLOSE" ]]; then
    echo "clamshell mode: Turning off inbuilt display for lid close"
    swaymsg output "$INBUILT_DISPLAY" disable
  elif [[ "$ACTION_TYPE" == "LID_OPEN" ]]; then
    echo "clamshell mode: Turning on inbuilt display for lid open"
    swaymsg output "$INBUILT_DISPLAY" enable
  else
    exit_with_error "clamshell mode: Unhandled ACTION_TYPE - $ACTION_TYPE"
  fi
}

DEFAULT_INBUILT_DISPLAY=$( swaymsg -t get_outputs --raw | jq -r '[ .[] | select(.name | startswith("eDP")).name ] | .[0]' )
INBUILT_DISPLAY=$( trawlcat wm.clamshell.display "$DEFAULT_INBUILT_DISPLAY" )
DISPLAY_COUNT=$( swaymsg -t get_outputs --raw | jq 'length')
CLAMSHELL_ENABLED=$( trawlcat wm.clamshell.enabled "true" )
AC_ACTION=$( trawlcat wm.lidclose.action.power "LOCK" )
BATTERY_ACTION=$( trawlcat wm.lidclose.action.battery "SLEEP" )
AC_ACTION_CMD=$( get_lid_close_cmd "$AC_ACTION" )
BATTERY_ACTION_CMD=$( get_lid_close_cmd "$BATTERY_ACTION" )


if [[ $DISPLAY_COUNT > 1 ]]; then
  if [[ -n $INBUILT_DISPLAY && "$CLAMSHELL_ENABLED" == "true" ]]; then
    handle_clamshell
    exit 0
  elif [[ -n $INBUILT_DISPLAY && "$CLAMSHELL_ENABLED" == "false" ]]; then
    echo "External display connected but clamshell mode is disabled"
  elif [[ -z $INBUILT_DISPLAY ]]; then
    echo "Inbuilt display not found."
  else
    exit_with_error "External display connected but invalid or missing value for resource wm.clamshell.enabled"
  fi
else
  echo "No external display is connected."
fi

echo "Clamshell requirements not met. Executing lid close action."

if [[ "$ACTION_TYPE" == "LID_OPEN" ]]; then
  echo "Turning on inbuilt display for lid open"
  exec swaymsg output "$INBUILT_DISPLAY" enable
elif [[ "$ACTION_TYPE" == "LID_CLOSE" ]]; then
  if on_ac_power; then
    echo "Executing lid close action on Power - $AC_ACTION"
    exec $AC_ACTION_CMD
  else
    echo "Executing lid close action on Battery- $BATTERY_ACTION"
    exec $BATTERY_ACTION_CMD
  fi
else
  exit_with_error "Unhandled ACTION_TYPE - $ACTION_TYPE"
fi

