Files
hassio-addons/mealie/rootfs/run.txt

138 lines
4.3 KiB
Plaintext

#!/usr/bin/env bashio
# shellcheck shell=bash
set -e
if [ ! -f /started ]; then
touch /started
####################
# Starting scripts #
####################
for SCRIPTS in /etc/cont-init.d/*; do
[ -e "$SCRIPTS" ] || continue
echo "$SCRIPTS: executing"
chown "$(id -u)":"$(id -g)" "$SCRIPTS"
chmod a+x "$SCRIPTS"
# Change shebang if no s6 supervision
sed -i 's|/usr/bin/with-contenv bashio|/usr/bin/env bashio|g' "$SCRIPTS"
/."$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?"
rm "$SCRIPTS"
done
####################
# Export variables #
####################
bashio::log.info "Exporting variables"
for k in $(bashio::jq "/data/options.json" 'keys | .[]'); do
bashio::log.blue "$k"="$(bashio::config "$k")"
export "$k"="$(bashio::config "$k")"
done
####################
# MIGRATE DATA DIR #
####################
# Migrate files
if [ -d /homeassistant/addons_config/mealie_data ] && [ ! -f /homeassistant/addons_config/mealie_data/migrated ]; then
bashio::log.warning "Migrating data, current data will not be touched"
cp -rnf /homeassistant/addons_config/mealie_data/* /config/ || true
touch /homeassistant/addons_config/mealie_data/migrated
bashio::addon.option "DATA_DIR" "/config"
fi
if [ -f /homeassistant/addons_config/mealie/config.yaml ] && [ ! -f /homeassistant/addons_config/mealie/migrated ]; then
bashio::log.warning "Migrating config.yaml, current data will not be touched"
cp -nf /homeassistant/addons_config/mealie/config.yaml /config/ || true
touch /homeassistant/addons_config/mealie/migrated
bashio::addon.option "DATA_DIR" "/config"
fi
# Solve issues in migration
if [ -d /config/recipes ] && [ -d /config/backups ]; then
[ -d /config/addons_config ] && rm -r /config/addons_config && bashio::log.warning "Deleted /config/addons_config, it shouldn't be there"
[ -d /config/addons_autoscripts ] && rm -r /config/addons_autoscripts && bashio::log.warning "Deleted /config/addons_autoscripts, it shouldn't be there"
fi
if [[ "$(bashio::config "DATA_DIR")" == "/config/addons_config/mealie_data" ]]; then
bashio::addon.option "DATA_DIR" "/config"
bashio::addon.restart
fi
###############
# CONFIG YAML #
###############
CONFIGSOURCE="/config/config.yaml"
if [ -f "$CONFIGSOURCE" ]; then
bashio::log.info "config.yaml found in $CONFIGSOURCE, exporting variables"
# Helper function
function parse_yaml {
local prefix=$2 || local prefix=""
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @ | tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s| #.*$||g" \
-e "s|#.*$||g" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
# Get list of parameters in a file
parse_yaml "$CONFIGSOURCE" "" >/tmpfile
while IFS= read -r line; do
# Clean output
line="${line//[\"\']/}"
# Check if secret
if [[ "${line}" == *'!secret '* ]]; then
echo "secret detected"
secret=${line#*secret }
# Check if single match
secretnum=$(sed -n "/$secret:/=" /config/secrets.yaml)
[[ $(echo $secretnum) == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
# Get text
secret=$(sed -n "/$secret:/p" /config/secrets.yaml)
secret=${secret#*: }
line="${line%%=*}='$secret'"
fi
# Data validation
if [[ "$line" =~ ^.+[=].+$ ]]; then
export "$line"
# Show in log
if ! bashio::config.false "verbose"; then bashio::log.blue "$line"; fi
else
bashio::exit.nok "$line does not follow the correct structure. Please check your yaml file."
fi
done <"/tmpfile"
else
bashio::log.info "No config.yaml found in $CONFIGSOURCE, using default parameters"
fi
###############
# PERMISSIONS #
###############
chmod -R 777 /data
mkdir -p "$DATA_DIR"
cd "$DATA_DIR" || true
chown -R "$PUID:$PGID" .
echo "Permissions adapted"
bashio::log.info "Starting nginx"
nginx & true
bashio::log.info "Starting app"
fi