From c1ce391313dffba5544b7afed3b920d97fa8d095 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Sat, 5 Oct 2024 10:23:09 +0200 Subject: [PATCH] Leaner code --- .../rootfs/etc/cont-init.d/31-checks.sh | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/birdnet-pi/rootfs/etc/cont-init.d/31-checks.sh b/birdnet-pi/rootfs/etc/cont-init.d/31-checks.sh index 2330c6956..a84b28bef 100755 --- a/birdnet-pi/rootfs/etc/cont-init.d/31-checks.sh +++ b/birdnet-pi/rootfs/etc/cont-init.d/31-checks.sh @@ -7,24 +7,29 @@ set -e ###################### echo " " -bashio::log.info "Checking your birndet.conf file integrity" +bashio::log.info "Checking your birdnet.conf file integrity" # Set variables configcurrent="$HOME"/BirdNET-Pi/birdnet.conf configtemplate="$HOME"/BirdNET-Pi/birdnet.bak +# Ensure both files exist before proceeding +if [ ! -f "$configcurrent" ] || [ ! -f "$configtemplate" ]; then + bashio::log.fatal "Missing required birdnet.conf or birdnet.bak file. Please ensure both are present." + exit 1 +fi + # Extract variable names from config template and read each one grep -o '^[^#=]*=' "$configtemplate" | sed 's/=//' | while read -r var; do # Check if the variable is in configcurrent, if not, append it if ! grep -q "^$var=" "$configcurrent"; then - # At which line was the variable in the initial file - bashio::log.yellow "...$var was missing from your birdnet.conf file, it was re-added" - grep "^$var=" "$configtemplate" >> "$configcurrent" + bashio::log.warning "...$var was missing from your birdnet.conf file, it was re-added" + echo "$(grep "^$var=" "$configtemplate")" >> "$configcurrent" fi # Check for duplicates if [ "$(grep -c "^$var=" "$configcurrent")" -gt 1 ]; then - bashio::log.error "Duplicate variable $var found in $configcurrent, all were commented out expect for the first one" - awk -v var="$var" '{ if ($0 ~ "^[[:blank:]]*"var && c++ > 0) print "#" $0; else print $0; }' "$configcurrent" > temp && mv temp "$configcurrent" + bashio::log.error "Duplicate variable $var found in $configcurrent, all were commented out except for the first one" + sed -i "0,/^$var=/!s/^$var=/#$var=/" "$configcurrent" fi done @@ -33,8 +38,8 @@ done ############## if [[ "$(bashio::addon.port "80")" == 3000 ]]; then - bashio::log.fatal "This is crazy but your port is set to 3000 and streamlit doesn't accept this port! You need to change it from the addon options and restart. Thanks" - sleep infinity + bashio::log.fatal "This is crazy but your port is set to 3000 and streamlit doesn't accept this port! You need to change it from the addon options and restart. Thanks" + sleep infinity fi ################## @@ -53,6 +58,6 @@ sed -i "/restart_services/d" "$HOME"/BirdNET-Pi/scripts/update_birdnet_snippets. sed -i "s|/etc/birdnet/birdnet.conf|/config/birdnet.conf|g" "$HOME"/BirdNET-Pi/scripts/update_birdnet_snippets.sh # Execute update_birdnet_snippets -/."$HOME"/BirdNET-Pi/scripts/update_birdnet_snippets.sh +"$HOME"/BirdNET-Pi/scripts/update_birdnet_snippets.sh echo " "