diff --git a/.templates/01-custom_script.sh b/.templates/01-custom_script.sh index 16ed5d252..f1e349a2b 100755 --- a/.templates/01-custom_script.sh +++ b/.templates/01-custom_script.sh @@ -2,39 +2,46 @@ # shellcheck shell=bash set -e -# Define slug if needed -slug="${HOSTNAME#*-}" +################## +# INITIALIZATION # +################## + +# Exit if /config is not mounted +if [ ! -d /config ]; then + exit 0 +fi + +# Define slug +slug="${HOSTNAME}" # Check type of config folder if [ ! -f /config/configuration.yaml ] && [ ! -f /config/configuration.json ]; then - # Migrate previous script - if [ -f /homeassistant/addons_autoscripts/"${slug}".sh ]; then - echo "Migrating scripts to new config location" - mv -f /homeassistant/addons_autoscripts/"${slug}".sh /config/"${slug}".sh - fi # New config location CONFIGLOCATION="/config" - CONFIGFILEBROWSER="/config/*-$slug" + CONFIGFILEBROWSER="/addon_configs/$slug/${HOSTNAME#*-}.sh" else # Legacy config location + slug="${HOSTNAME#*-}" CONFIGLOCATION="/config/addons_autoscripts" - CONFIGFILEBROWSER="/config/addons_autoscripts" - mkdir -p /config/addons_autoscripts + CONFIGFILEBROWSER="/homeassistant/addons_config/${slug}/${slug}.sh" fi -bashio::log.green "Execute $CONFIGFILEBROWSER/${slug}.sh if existing" +# Default location +CONFIGSOURCE="$CONFIGLOCATION/${HOSTNAME#*-}.sh" + +bashio::log.green "Execute $CONFIGFILEBROWSER if existing" bashio::log.green "Wiki here : github.com/alexbelgium/hassio-addons/wiki/Add-ons-feature-:-customisation" # Download template if no script found and exit -if [ ! -f "$CONFIGLOCATION/${slug}".sh ]; then +if [ ! -f "$CONFIGSOURCE" ]; then TEMPLATESOURCE="https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/script.template" - curl -f -L -s -S "$TEMPLATESOURCE" --output "$CONFIGLOCATION/${slug}".sh || true + curl -f -L -s -S "$TEMPLATESOURCE" --output "$CONFIGSOURCE" || true exit 0 fi # Convert scripts to linux -dos2unix "$CONFIGLOCATION/${slug}".sh &>/dev/null || true -chmod +x "$CONFIGLOCATION/${slug}".sh +dos2unix "$CONFIGSOURCE" &>/dev/null || true +chmod +x "$CONFIGSOURCE" # Check if there is actual commands while IFS= read -r line @@ -45,7 +52,7 @@ do # Check if line is not empty and does not start with # if [[ -n "$line" ]] && [[ ! "$line" =~ ^# ]]; then bashio::log.green "... script found, executing" - /."$CONFIGLOCATION/${slug}".sh + /."$CONFIGSOURCE" exit 0 fi -done < "$CONFIGLOCATION/${slug}".sh +done < "$CONFIGSOURCE"