#!/bin/bash

# brightness.bash	A bash script to set the brightness [gamma value] of a monitor using a Linux terminal's "X11 Windowing System"
# 1/27/2025 William Craig - bill@wrcraig.com     https://wrcraig.com

# Requires the Linux bash shell and a Linux X11 windowing system.

# INTERACTIVE MODE is initiated at the terminal by entering "bash brightness.bash"
# in the directory containg this script. Allows direct entry of gamma values
# or upward iteration of gamma values from 0.5 to 3.0, the default being 1.0

# PARAMETER MODE is initiated at the terminal, or with a cron job after deciding
# on your monitor's best value for the parameter, i.e. "bash brightness.bash [parameter].
# Parameter mode can be overridden by using interactive mode at any time.

# Rebooting will return your system to its default value, usually "1.0"

# Color Definitions
	BIWhite='\033[1;97m'
	On_Green='\033[42m'
	On_Blue='\033[44m'
	On_Red='\033[41m'
	IYellow='\033[0;93m'
	NC='\033[0m'	# no colors

# Usage
#    either
# echo -e "${colorname} ${variable} and/or text ${NC}"
#    or
# printf "${colorname} ${variable} and/or text ${NC} \n"
#
# Can also use the following fg/bg combinations instead of ${colorname}
#	${BIWhite%%m*};${On_Red##*;}
#	${BIWhite%%m*};${On_Blue##*;}
#	${BIWhite%%m*};${On_Green##*;}
#	${IYellow}
#	${NC}
# printf "${BIWhite%%m*};${On_Blue##*;} ${variable} and/or text ${NC} \n"

function gracefulexit {
	#clear
	tput cup 11 0
	echo Rebooting will return your system to its default value, usually 1.0
	echo Parameter mode can automatically set a value on reboot if needed.
	echo Rerun command is [bash brightness.bash]
	echo
	echo -e "${BIWhite%%m*};${On_Blue##*;}  Goodbye  ${NC}"
	echo
	exit
}

function testing {
# Test for a valid entry or parameter
flag=n
IFS="."		# Split the entered value entry on the decimal
read left right <<< $value
left="${left//0}"
IFS=" "

if [[ "$left" == "" ]];then
	left=0
fi

if [[ "$right" == "" ]];then
	right=0
fi

if [[ "$right" == "0" ]] && [[ "$left" == "0" ]];then
	lowval=$(bc <<< "$lowval + .1")
	value=$lowval
fi

if [[ "$left" == [0-9] ]] && [[ "$right" == [0-9] ]];then
	if ([[ "$left" -ge "3" ]] && [[ "$right" -gt "0" ]]) || [[ "$left" -gt "3" ]];then
		message="Value is over limit of 3.0"
		flag=y
		if [[ "$param" != "y" ]];then
			tput cup 10 0
			printf "${BIWhite%%m*};${On_Red##*;}$message, resetting to 1.0 ${NC}"
			flag=y
			value=1
			sleep 2
			tput cup 10 0
			tput el
		fi
	fi
	if [[ "$left" == "0" ]] && [[ "$right" -lt "5" ]];then
		message="Value is below limit of 0.5"
		flag=y
		if [[ "$param" != "y" ]];then
			tput cup 10 0
			printf "${BIWhite%%m*};${On_Red##*;}${message}, resetting to 1.0 ${NC}"
			value=1
			sleep 2
			tput cup 10 0
			tput el
		fi
	fi
else
	flag=y
	message="Value should be in tenths, from 0.5 to 3.0"
	if [[ "$param" != "y" ]];then
		tput cup 10 0
		printf "${BIWhite%%m*};${On_Red##*;}$message, from 0.5 to 3.0 ${NC}"
		value=1
		flag=y
		sleep 3
		tput cup 10 0
		tput el
	fi
fi

if [[ "$param" == "y" ]];then								# is parameter driven, no interaction
	if [[ "$flag" != "y" ]];then							# no errors
		xgamma -gamma $(bc <<< "$value") > /dev/null 2>&1	# set gamma value
		clear
		tput cup  0
		printf "${BIWhite%%m*};${On_Green##*;}Brightness is set at $value ${NC}"
		echo;echo
		exit
	else
		tput cup 11 0
		printf "${BIWhite%%m*};${On_Red##*;}Brightness parameter value of $left.$right failed. Try again without a parameter to find an appropriate value./n $message${NC}"
		sleep 5
		tput cup 11 0
		tput el
		exec bash brightness   # Restarting the menu will kill off any former menu process

	fi
fi

	}



############ Main Script ##########
clear
lowval=.4
param=n
isnew=1

trap 'gracefulexit' SIGINT SIGQUIT SIGTERM

if [[ "$1" != "" ]];then
	valu=$1
	param=y
else
	value=1
	valu=1
	param=n
fi

#valu=$value
tput cup 1 3
printf "${BIWhite%%m*};${On_Blue##*;}   SET BRIGHTNESS [gamma] FOR X11 WINDOWING SYSTEM   ${NC}"

while :
do
left="1";right="1"

tput cup 3 4
printf "Current brightness [gamma]: "
tput el
tput cup 3 32
printf "$value"
tput el
tput cup 3 41
printf "Default is 1.0"
tput cup 5 0
tput el
printf "Press Q to set current value as session brightness, and quit"
tput cup 7 0
printf "  ${IYellow}Press enter to increment gamma values from 0.5 to 3.0${NC}\n"
tput el

if [[ "$param" == "y" ]];then								# is parameter driven, no interaction
	echo | read val											# pipe a newline (enter) to read val
else
	tput cup 8 0
tput el
printf "${IYellow}or enter a gamma value from 0.5 to 3.0:${NC} "
	read val
fi

if [[ "$val" == [Qq]* ]];then
	gracefulexit
fi

if [[ "$val" == "" ]];then
	lowval=$(bc <<< "$lowval + .1")
	value=$lowval
elif [[ "isnew" == "1" ]];then
	val=1.0
	isnew=0
else
	value=$val
fi

if [[ "$param" == "y" ]];then
	value=$1
fi

testing

xgamma -gamma $(bc <<< "$value") > /dev/null 2>&1
lowval=$value										# Increment lowval

done
