#!/bin/bash

## Copyright (C) 2025 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

true "$0: START"

source /usr/libexec/helper-scripts/lockfile.sh
source /usr/libexec/helper-scripts/strings.bsh

bl_pct_file='/run/backlight-tool-dist/bl_pct'

command -v accountctl >/dev/null
command -v leaprun >/dev/null

backlight_get_val() {
  if ! leaprun backlight-tool-dist-handler-get; then
    printf '%s\n' "$0: ERROR: Could not get backlight percentage!" >&2
    return 1
  fi
}

backlight_set_val() {
  local bl_pct;

  bl_pct="${1:-}";
  if [ -z "$bl_pct" ]; then
    printf '%s\n' "$0: ERROR: No backlight brightness percentage specified!" >&2
    return 1
  fi
  if ! [[ "$bl_pct" =~ ^[0-9]+$ ]]; then
    printf '%s\n' "$0: ERROR: Non-numeric brightness percentage specified!" >&2
    return 1
  fi
  if (( bl_pct < 1 )) || (( bl_pct > 100 )); then
    printf '%s\n' "$0: ERROR: Specified brightness percentage is out of range!" >&2
    return 1
  fi

  if ! printf '%s\n' "$bl_pct" > "$bl_pct_file" ; then
    printf '%s\n' "$0: ERROR: Unable to write brightness percentage to '/run/backlight-tool-dist/bl_pct'!" >&2
    return 1
  fi
  if ! leaprun backlight-tool-dist-handler-set ; then
    printf '%s\n' "$0: ERROR: 'leaprun backlight-tool-dist-handler' exited with non-zero exit code!" >&2
    return 1
  fi
}

backlight_inc_val() {
  if ! leaprun backlight-tool-dist-handler-inc; then
    printf '%s\n' "$0: ERROR: Could not increment backlight brightness!" >&2
    return 1
  fi
}

backlight_dec_val() {
  if ! leaprun backlight-tool-dist-handler-dec; then
    printf '%s\n' "$0: ERROR: Could not decrement backlight brightness!" >&2
    return 1
  fi
}

prep_config_dir() {
  local config_dir
  config_dir="${HOME}/.config"
  last_bright_file="${config_dir}/backlight-tool-dist-last-bright-pct"
  if ! mkdir --parents -- "$config_dir" ; then
    printf '%s\n' "$0: ERROR: Unable to create '$config_dir'!" >&2
    return 1
  fi
}

backlight_save_val() {
  local bl_pct

  if ! bl_pct="$(backlight_get_val)"; then
    printf '%s\n' "$0: ERROR: Could not get backlight percentage to save!" >&2
    return 1
  fi

  ## Sets: last_bright_file
  prep_config_dir || return 1
  if ! overwrite "$last_bright_file" "$bl_pct" >/dev/null ; then
    printf '%s\n' "$0: ERROR: Unable to save brightness percentage to '$last_bright_file'!" >&2
    return 1
  fi
}

backlight_restore_val() {
  local bl_pct

  ## Sets: last_bright_file
  prep_config_dir || return 1

  if ! [ -f "$last_bright_file" ]; then
    printf '%s\n' "$0: INFO: No brightness value saved at '$last_bright_file', therefore not restoring, ok."
    return 0
  fi
  if ! bl_pct="$(read_integer_file "$last_bright_file" 1 100)" ; then
    return 0
  fi
  backlight_set_val "$bl_pct" || return 1
}

case "${1:-}" in
'get')
  backlight_get_val || exit 1
  ;;
'set')
  backlight_set_val "${2:-}" || exit 1
  backlight_save_val || exit 1
  ;;
'inc')
  backlight_inc_val || exit 1
  backlight_save_val || exit 1
  ;;
'dec')
  backlight_dec_val || exit 1
  backlight_save_val || exit 1
  ;;
'restore')
  backlight_restore_val || exit 1
  ;;
'')
  printf '%s\n' "$0: ERROR: No mode specified!" >&2
  exit 1
  ;;
*)
  printf '%s\n' "$0: ERROR: Unrecognized mode '$1' specified!" >&2
  exit 1
  ;;
esac

true "$0: EMD"
