1
0
Fork 0
mirror of https://github.com/SinTan1729/random.git synced 2024-12-26 13:18:35 -06:00

clean up code and add comments

This commit is contained in:
Sayantan Santra 2022-07-27 02:16:16 -05:00
parent b21fc6a056
commit a8258d2f89

View file

@ -1,8 +1,8 @@
#!/bin/sh #!/bin/sh
# This is a script to set my external monitor's brightness # this is a script to set my external monitor's brightness
# to high (70%) or low (40%) automatically according to day/night. # to high (70%) or low (40%) automatically according to day/night.
# It uses ddcutil to access the monitor's settings and sunwait to know # it uses ddcutil to access the monitor's settings and sunwait to know
# if it's day or night. It expects a file in the following format in # if it's day or night. It expects a file in the following format in
# the .config directory (the values should be according to location) # the .config directory (the values should be according to location)
@ -13,14 +13,16 @@
# high=70 # high=70
# low=40 # low=40
# Just run this script one manually to set it up to run perpetually (hopefully) using 'at' # just run this script one manually to set it up to run perpetually (hopefully) using 'at'
# (Some distros don't come with 'at' preinstalled, so you might need to install 'at' and enable the 'atd' service.) # (some distros don't come with 'at' preinstalled, so you might need to install 'at' and enable the 'atd' service.)
# It might also be a good idea to add '<location-of-this-script> scheduler' to your DE's autostart list, or to '.profile' so that it launches on system login # it might also be a good idea to add '<location-of-this-script> scheduler wait' to your DE's autostart list, or to '.profile' so that it launches on system login
# (Sometimes I forget to turn on the monitor when I start my PC, so this is better than having a crontab) # (sometimes I forget to turn on the monitor when I start my PC, so this is better than having a crontab)
# location of the config file
confdir="/home/sintan/.config" confdir="/home/sintan/.config"
# read from the config file
if test -f $confdir/latlong.toml ; then if test -f $confdir/latlong.toml ; then
source $confdir/latlong.toml source $confdir/latlong.toml
else else
@ -28,19 +30,15 @@ else
exit exit
fi fi
# get sun status
sun_status=$(sunwait poll $latitude $longitude) sun_status=$(sunwait poll $latitude $longitude)
[ $sun_status == "DAY" ] && target=$high || target=$low
if [ $sun_status == "DAY" ]; then # do the brightness adjustment using ddcutil
target=$high
else
target=$low
fi
ddcutil setvcp 10 $target ddcutil setvcp 10 $target
echo "Monitor brightness set to $target%, since it's $(echo $sun_status | tr '[:upper:]' '[:lower:]') time" echo "Monitor brightness set to $target%, since it's $(echo $sun_status | tr '[:upper:]' '[:lower:]') time"
# Check if we want to access the scheduler. If yes, create a schedule using `at` # check if we want to access the scheduler. If yes, create a schedule using `at`
if [ "$1" == "scheduler" ]; then if [ "$1" == "scheduler" ]; then
if [ $sun_status == "DAY" ]; then if [ $sun_status == "DAY" ]; then
time_next=$(sunwait report 35.221050N 97.445938W | grep "Daylight:" | awk '{print $6}') time_next=$(sunwait report 35.221050N 97.445938W | grep "Daylight:" | awk '{print $6}')
@ -49,15 +47,16 @@ if [ "$1" == "scheduler" ]; then
fi fi
task_list=$(atq | grep $time_next | awk '{print $1}') task_list=$(atq | grep $time_next | awk '{print $1}')
# loop through all `at` entries to see if a task already exists
flag=true
for item in $task_list for item in $task_list
do do
if ! [ $(at -c $item | sed 'x;$!d' | awk '{print $4}') == "$scriptdir/brightness-by-daylight.sh" ]; then [ "$(at -c $item | sed 'x;$!d')" == "$scriptdir/brightness-by-daylight.sh scheduler wait" ] && flag=false
# we wait a minute so that the next schedule is set up properly (or sunwait reports day/night in a misleading way)
at -m $time_next <<< "sleep 60 && $scriptdir/brightness-by-daylight.sh scheduler"
fi
done done
if [ -z "$task_list" ]; then # in case there's no at entry # sometimes we need to wait a minute so that the next schedule is set up properly (or sunwait reports day/night in a misleading way)
at -m $time_next <<< "sleep 60 && $scriptdir/brightness-by-daylight.sh scheduler" [ "$2" == "wait" ] && sleep 60
fi
# actually create the schedule
$flag && at -m $time_next <<< "$scriptdir/brightness-by-daylight.sh scheduler wait"
fi fi