Mac-like-automatic-brightness/setup.sh
2025-03-17 20:20:13 +00:00

60 lines
1.9 KiB
Bash
Executable file

#!/bin/bash
#
case $1 in
-u) echo "Updating Mac-like-automatic-brightness..."
echo "Stopping AB service..."
sudo systemctl kill AB
echo "Updating AutomaticBrightness.sh..."
echo "Cloning AutomaticBrightness.sh..."
sudo cp AutomaticBrightness.sh /usr/local/bin/AutomaticBrightness.sh
echo "Updating AB.service for systemD..."
echo "Cloning AB.service for systemD..."
sudo cp AB.service /etc/systemd/system/AB.service
echo "Restarting AB service..."
systemctl daemon-reload
sudo systemctl start AB
exit;;
esac
echo "Setting up AutomaticBrightness.sh as a service..."
echo "Calibrating Light Sensor Scale..."
LSensorPath=$(find -L /sys/bus/iio/devices -maxdepth 2 -name "in_illuminance_raw" 2>/dev/null | grep "in_illuminance_raw")
MaxScreenBrightness=$(find -L /sys/class/backlight -maxdepth 2 -name "max_brightness" 2>/dev/null | grep "max_brightness" | xargs cat)
echo "Put your sensor in a bright light (outside works best)"
read -p "Press Enter to continue..."
Smax=$(cat $LSensorPath)
Scale=$(echo "scale=2; $MaxScreenBrightness / $Smax" | bc)
Final="SensorToDisplayScale=$Scale"
awk -v new_phrase="$Final" '/SensorToDisplayScale=/{ print new_phrase; next } 1' AutomaticBrightness.sh > temp && mv temp AutomaticBrightness.sh
TempSteps=($MaxScreenBrightness / 60)
if [[ TempSteps -lt 17 ]]
then
Steps=$($MaxScreenBrightness / 16)
NewStep="LevelSteps=$Steps"
awk -v new_phrase="$NewStep" '/LevelSteps=/{ print new_phrase; next } 1' AutomaticBrightness.sh > temp && mv temp AutomaticBrightness.sh
fi
echo "Cloning AutomaticBrightness.sh..."
sudo cp AutomaticBrightness.sh /usr/local/bin/AutomaticBrightness.sh
sudo chmod u+x /usr/local/bin/AutomaticBrightness.sh
echo "Cloning AB.service for systemD..."
sudo cp AB.service /etc/systemd/system/AB.service
echo "Starting Service..."
sudo systemctl enable AB
sudo systemctl start AB