# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2019-present Shanti Gilbert (https://github.com/shantigilbert)
# Copyright (C) 2023 JELOS (https://github.com/JustEnoughLinuxOS)

. /etc/os-release

export XDG_RUNTIME_DIR=/var/run/0-runtime-dir
export PATH="/usr/bin:/usr/local/bin:/storage/bin:${PATH}"
export SDL_GAMECONTROLLERCONFIG_FILE="/storage/.config/SDL-GameControllerDB/gamecontrollerdb.txt"

J_DIR="/storage/.config/system"
J_CONF="${J_DIR}/configs/system.cfg"
J_CONF_LOCK="/tmp/.system.cfg.lock"
ES_CONF="/storage/.emulationstation/es_settings.cfg"

function tocon() {
  echo -ne "\033[1000H\033[2K==> ${*}" >/dev/console
}

function log() {
  SOURCE=${1//\/*\//}
  MESSAGE=${*#${1}}
  MESSAGE=${MESSAGE# }
  logger -t ${SOURCE} "${MESSAGE}"
  echo "$(date) ${SOURCE}: ${MESSAGE}" >>/var/log/messages
}

function get_setting() {
  if [ -n "${3}" ]
  then
    ### Test to see if we have a game setting.
    VAR="$2\[\"$(echo ${3} | sed -E "s~'~\\\x27~g"';s~[()&]~\\&~g')\"\]\.$1"
    OUTPUT=$(awk 'BEGIN {FS="="} /^'"${VAR}"'/ {print $NF}' ${J_CONF})
    if [ ! -z "${OUTPUT}" ]
    then
      echo ${OUTPUT}
      return
    else
      ### If not, check to see if we have a system setting.
      LOCAL=$(awk -F: '/^'"${2}.${1}"'=/ { st = index($0,"=");print substr($0,st+1);exit}' ${J_CONF})
      if [ ! -z "${LOCAL}" ]
      then
        echo ${LOCAL}
        return
      fi
    fi
  fi

  if [ -z "${3}" ] && [ -n "${2}" ]
  then
    ### Check to see if we have a global setting.
    LOCAL=$(awk -F: '/^'"${2}.${1}"'=/ {  st = index($0,"=");print substr($0,st+1);exit}' ${J_CONF})
    if [ ! -z "${LOCAL}" ]
    then
      echo ${LOCAL}
      return
    fi
  fi

  ### Check to see if we have a "system." global setting.
  SYSTEM=$(awk -F: '/^system.'"${1}"='/ { st = index($0,"=");print substr($0,st+1);exit}' ${J_CONF})
  if [ -n "${SYSTEM}" ]
  then
    echo ${SYSTEM}
    return
  fi

  ### Check to see if we have a "global." global setting."
  LOCAL=$(awk -F: '/^'"${1}"='/ { st = index($0,"=");print substr($0,st+1);exit}' ${J_CONF})
  if [ -z "${LOCAL}" ]
  then
    awk -F: '/^global.'"${1}"='/ { st = index($0,"=");print substr($0,st+1);exit}' ${J_CONF}
    return
  else
    echo ${LOCAL}
  fi
  return
}

function wait_lock() {
  while true
  do
    if (set -o noclobber; echo "$$" > "${J_CONF_LOCK}") 2>/dev/null
    then
      trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
      break
    else
     sleep 1
    fi
  done
}

function del_setting() {
  wait_lock
  if [[ "${1}" =~ ^[[:alnum:]] ]]
  then
    sed -i "/^${1}=/d" "${J_CONF}"
  fi
  rm -f "${J_CONF_LOCK}"
}

function sort_settings() {
  wait_lock
  cat "${J_CONF}" | grep ^[a-z0-9] | sort >"${J_CONF}.tmp"
  mv "${J_CONF}.tmp" "${J_CONF}"
  rm -f "${J_CONF_LOCK}"
}

function set_kill() {
  if [ "${1}" = "set" ]
  then
    cat <<EOF >/tmp/.process-kill-data
${2}
EOF
  elif [ "${1}" = "stop" ]
  then
    if [ -e "/tmp/.process-kill-data" ]
    then
      rm -f "/tmp/.process-kill-data"
    fi
  fi
}

function set_setting() {

  if [ ! -d "/storage/.config/system/configs" ]
  then
    mkdir -p /storage/.config/system/configs
  fi
  if [ ! -e "/storage/.config/system/configs/system.cfg" ]
  then
    cp -f /usr/config/system/configs/system.cfg /storage/.config/system/configs/system.cfg
  fi

  if [[ "${1}" =~ ^[[:alnum:]] ]]
  then
    del_setting "${1}"
    if [ ! "${2}" = "default" ]
    then
      wait_lock
      echo "${1}=${2}" >> "${J_CONF}"
      rm -f "${J_CONF_LOCK}"
    fi
  fi
}

function battery_percent() {
  awk 'BEGIN {FS="="} /POWER_SUPPLY_CAPACITY=/ {print $2}' /sys/class/power_supply/[Bb][Aa][Tt]*/uevent 2>/dev/null
}

function get_es_setting() {
  echo $(sed -n "s|\s*<${1} name=\"${2}\" value=\"\(.*\)\" />|\1|p" ${ES_CONF})
}

function fbwidth() {
  local ORIENTATION=$(</sys/devices/virtual/graphics/fbcon/rotate)
  if [ "${ORIENTATION}" = "0" ]
  then
    fbset | awk '/geometry/ {print $2}'
  else
    fbset | awk '/geometry/ {print $3}'
  fi
}

function fbheight() {
  local ORIENTATION=$(</sys/devices/virtual/graphics/fbcon/rotate)
  if [ "${ORIENTATION}" = "0" ]
  then
    fbset | awk '/geometry/ {print $3}'
  else
    fbset | awk '/geometry/ {print $2}'
  fi
}

function cpu_vendor() {
  awk '/vendor_id/ {print $3;exit}' /proc/cpuinfo
}

# sway window manager utility function to enable fullscreen on a window for an input app_id
function sway_fullscreen {
  # value for swaymsg criteria
  # for 'virtual' attributes pidof / pgrep this is the process to look up pid for
  local VALUE="${1}"
  
  # attribute for swaymsg criteria - default to app_id for backwards compatibility
  # 'virtual' attributes pidof / pgrep result in the value being evaluted at runtime
  local ATTRIBUTE="${2:-app_id}"

  # retry control vars
  local QUERY_LIMIT=5
  local QUERY_COUNT=0

  if echo "${UI_SERVICE}" | grep "sway"; then
    while [ $QUERY_COUNT -lt $QUERY_LIMIT ]; do
      # loop, attempting to fullscreen window for app_id
      ((QUERY_COUNT++))

      # Derive swaymsg criteria attribute and value where they are dynamic
      if [[ "${ATTRIBUTE}" = @(pidof|pgrep) ]]; then
        local PID=""

        case "${ATTRIBUTE}" in
          "pidof")
            PID=$(pidof "${VALUE}")
            ;;
          "pgrep")
            PID=$(pgrep "${VALUE}")
            ;;
        esac

        if [ -z "${PID}" ]; then
          echo "No PID found for VALUE=${VALUE}"
          sleep 1
          continue
        else
          ATTRIBUTE="pid"
          VALUE="${PID}"
        fi
      fi

      echo "Attempting to fullscreen ${ATTRIBUTE}=${VALUE}"
      swaymsg '['"${ATTRIBUTE}"'='"${VALUE}"'] fullscreen enable'
      local RESULT=$?

      if [ $RESULT -eq 0 ]; then
        echo "Successful, exiting"
        break
      else
        echo "Failed, waiting to try again"
        sleep 1
      fi
    done
  else
    echo "sway is not running, exiting"
  fi
}
