2021-12-14 23:34:35 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
configFile="/mnt/hdd/raspiblitz.conf"
|
|
|
|
|
|
|
|
# command info
|
|
|
|
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ]; then
|
|
|
|
echo "RaspiBlitz Config Edit - adds value to file & cache and creates entries if needed:"
|
2022-02-15 21:31:44 +00:00
|
|
|
echo "blitz.conf.sh set [key] [value] [?conffile] <noquotes>"
|
2022-01-25 12:07:11 +01:00
|
|
|
echo "blitz.conf.sh delete [key] [?conffile]"
|
2022-02-15 21:31:44 +00:00
|
|
|
echo "note: use quotes and escape special characters for sed"
|
2021-12-14 23:34:35 +01:00
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$1" = "set" ]; then
|
|
|
|
|
|
|
|
# get parameters
|
|
|
|
keystr=$2
|
2022-06-02 17:09:03 +02:00
|
|
|
valuestr=echo $3 | sed 's/\//\\\//g'
|
2022-01-25 12:07:11 +01:00
|
|
|
configfileAlternative=$4
|
2021-12-14 23:34:35 +01:00
|
|
|
|
|
|
|
# check that key & value are given
|
|
|
|
if [ "${keystr}" == "" ] || [ "${valuestr}" == "" ]; then
|
|
|
|
echo "# blitz.conf.sh $@"
|
|
|
|
echo "# FAIL: missing parameter"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-01-25 12:07:11 +01:00
|
|
|
# optional another configfile
|
|
|
|
if [ "${configfileAlternative}" != "" ]; then
|
|
|
|
configFile="${configfileAlternative}"
|
2022-02-15 21:31:44 +00:00
|
|
|
fi
|
2021-12-14 23:34:35 +01:00
|
|
|
|
|
|
|
# update config value in cache
|
|
|
|
/home/admin/_cache.sh set ${keystr} "${valuestr}"
|
|
|
|
|
|
|
|
# check that config file exists
|
|
|
|
raspiblitzConfExists=$(ls ${configFile} 2>/dev/null | grep -c "${configFile}")
|
|
|
|
if [ ${raspiblitzConfExists} -eq 0 ]; then
|
|
|
|
echo "# blitz.conf.sh $@"
|
|
|
|
echo "# FAIL: missing config file: ${configFile}"
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check if key needs to be added (prepare new entry)
|
|
|
|
entryExists=$(grep -c "^${keystr}=" ${configFile})
|
|
|
|
if [ ${entryExists} -eq 0 ]; then
|
2022-01-25 12:07:11 +01:00
|
|
|
echo "${keystr}=" | sudo tee -a ${configFile} 1>/dev/null
|
2021-12-14 23:34:35 +01:00
|
|
|
fi
|
|
|
|
|
2022-02-15 21:31:44 +00:00
|
|
|
# add valuestr in quotes if not standard values and "$5" != "noquotes"
|
|
|
|
if [ "${valuestr}" != "on" ] && [ "${valuestr}" != "off" ] && [ "${valuestr}" != "1" ] && [ "${valuestr}" != "0" ] && [ "$5" != "noquotes" ]; then
|
2021-12-14 23:34:35 +01:00
|
|
|
valuestr="'${valuestr}'"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set value (sed needs sudo to operate when user is not root)
|
|
|
|
sudo sed -i "s/^${keystr}=.*/${keystr}=${valuestr}/g" ${configFile}
|
|
|
|
|
|
|
|
|
|
|
|
elif [ "$1" = "delete" ]; then
|
|
|
|
|
|
|
|
# get parameters
|
|
|
|
keystr=$2
|
2022-01-25 12:07:11 +01:00
|
|
|
configfileAlternative=$3
|
2021-12-14 23:34:35 +01:00
|
|
|
|
|
|
|
# check that key & value are given
|
|
|
|
if [ "${keystr}" == "" ]; then
|
|
|
|
echo "# FAIL: missing parameter"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-01-25 12:07:11 +01:00
|
|
|
# optional another configfile
|
|
|
|
if [ "${configfileAlternative}" != "" ]; then
|
|
|
|
configFile="${configfileAlternative}"
|
2022-02-15 21:31:44 +00:00
|
|
|
fi
|
2022-01-25 12:07:11 +01:00
|
|
|
|
2021-12-14 23:34:35 +01:00
|
|
|
# delete value
|
|
|
|
sudo sed -i "/^${keystr}=/d" ${configFile} 2>/dev/null
|
|
|
|
|
|
|
|
else
|
|
|
|
echo "# FAIL: parameter not known - run with -h for help"
|
|
|
|
fi
|