#!/bin/bash

operation="${1}"
cache_dir="${HOME}/.cache/sway-profiles"
cache_file="${cache_dir}/kb_brightness"
mkdir -p "${cache_dir}"

if [[ ! -f "${cache_file}" ]];then
  echo "100" > "${cache_file}"
fi

kb_brightness=$( cat "${cache_file}" )
case "$operation" in
  up)
    kb_brightness="$((${kb_brightness}+5))"
    [[ ${kb_brightness} -gt 100 ]] && kb_brightness="100"
    ;;&
  down)
    kb_brightness="$((${kb_brightness}-5))"
    [[ ${kb_brightness} -lt 0 ]] && kb_brightness="0"
    ;;&
  up|down)
    echo "${kb_brightness}" > "${cache_file}"
    for s in ~/.config/sway/scripts/kb_brightness.d/*;do
      bash -c "${s} ${kb_brightness}"
    done
    ;;
esac
