I am working on a script that changes the led_brightness_schedule of my NAS, based on the sunrise and sunset times at my location, in the synoinfo.conf file, see code below (feel free to use it)
Currently it only makes a copy of the conf file in which the old schedule is replaced with the new sunrise/sunset times. I included some checks (hardcoded DSM version check, api status OK, length of the new schedule should be 168 characters long) to makes sure that the new config that I create is valid.
My question is whether or not I can run such a script safely without corrupting my NAS/Config. Are there things I am overlooking, before taking such a script in production? Of course I am testing it now, to make sure I don't invalidate my config, with the current script.
Code:
#!/bin/bash
#Sunrise and Sunset times by https://sunrise-sunset.org
strindex() {
x="${1%%$2*}"
[[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
}
#Check DSM version
DSMVERSION=`cat "/etc/VERSION"`
PRODUCTVERSION=`grep 'productversion="6.2.3"' <<< "$DSMVERSION"`
if [ -z "${PRODUCTVERSION}" ]; then
>&2 echo 'INVALID DSM VERSION'
exit 1
fi
cp "/etc/synoinfo.conf" "/volume1/Miura/Scripts/synoinfo.conf.daily.bak"
URL='https://api.sunrise-sunset.org/json?lat=52.3745403&lng=4.89797550561798'
JSON=$(curl -s ${URL})
echo "$JSON" > "/volume1/Miura/Scripts/sunrise-sunset_org.txt"
STATUSOK=`grep '"status":"OK"' <<< "$JSON"`
if [ -z "${STATUSOK}" ]; then
>&2 echo 'API STATUS NOT OK'
exit 1
fi
STARTSUNRISE=$(strindex "$JSON" "sunrise")
STARTSUNRISE=$((STARTSUNRISE+10))
SUNRISE=${JSON:STARTSUNRISE:10}
STARTSUNSET=$(strindex "$JSON" "sunset")
STARTSUNSET=$((STARTSUNSET+9))
SUNSET=${JSON:STARTSUNSET:10}
SUNRISE=$(date --date="$SUNRISE" +%H)
SUNSET=$(date --date="$SUNSET" +%H)
MORNING=$(($SUNRISE+1))
EVENING=$((24-$SUNSET-1))
ON=$((24-$MORNING-$EVENING))
for (( i=1; i<=$MORNING; i++ )); do
CONF1=$CONF1"1"
done
for (( i=1; i<=$ON; i++ )); do
CONF1=$CONF1"0"
done
for (( i=1; i<=$EVENING; i++ )); do
CONF1=$CONF1"1"
done
for (( i=1; i<=7; i++ )); do
CONF=$CONF$CONF1
done
# Check created string length (7 x 24)
if [ ${#CONF} != 168 ]; then
>&2 echo 'CONFIG LENGTH FAIL'
exit 1
fi
CONF='led_brightness_schedule="'$CONF'"'
#Currently only testing on a copy of synoinfo.conf
cp "/etc/synoinfo.conf" "/volume1/Miura/Scripts/synoinfo.conf"
sed -i "/led_brightness_schedule/c\\$CONF" '/volume1/Miura/Scripts/synoinfo.conf'
Currently it only makes a copy of the conf file in which the old schedule is replaced with the new sunrise/sunset times. I included some checks (hardcoded DSM version check, api status OK, length of the new schedule should be 168 characters long) to makes sure that the new config that I create is valid.
My question is whether or not I can run such a script safely without corrupting my NAS/Config. Are there things I am overlooking, before taking such a script in production? Of course I am testing it now, to make sure I don't invalidate my config, with the current script.