#!/bin/sh
set -e
set -u
config_file="$(apply-dpms printcfg)"
ac_choices="5!10!15!30!60!never"
bat_choices="$ac_choices"
# set default to current values, if any
if [ -e "$config_file" ] ; then
ac_current="$(cat "$config_file" | awk '$1 ~ /AC/ {print($2)}')"
bat_current="$(cat "$config_file" | awk '$1 ~ /BAT/ {print($2)}')"
if [ -z "$ac_current" ] ; then
echo "" # no current AC config
elif echo "$ac_choices" | grep "$ac_current" > /dev/null 2>&1 ; then
ac_choices="$(echo "$ac_choices" | sed 's/'"$ac_current"'/^'"$ac_current"'/g')"
else
ac_choices="$(echo "$ac_choices!^$ac_current")"
fi
if [ -z "$bat_current" ] ; then
echo "" # no current BAT config
elif echo "$bat_choices" | grep "$bat_current" > /dev/null 2>&1 ; then
bat_choices="$(echo "$bat_choices" | sed 's/'"$bat_current"'/^'"$bat_current"'/g')"
else
bat_choices="$(echo "$bat_choices!^$bat_current")"
fi
else
ac_choices="$(echo "$ac_choices" | sed 's/60/^60/g')"
bat_choices="$(echo "$bat_choices" | sed 's/10/^10/g')"
fi
choice="$(yad --form --title "DPMS Profile" \
--text "Configure DPMS profile" \
--field="On AC":CB \
"$ac_choices" \
--field="On Battery":CB \
"$bat_choices")"
ac_profile="$(echo "$choice" | awk -F '|' '{print $1}')"
bat_profile="$(echo "$choice" | awk -F '|' '{print $2}')"
printf "AC\t$ac_profile\n" > "$config_file"
printf "BAT\t$bat_profile\n" >> "$config_file"
apply-dpms