#!/command/with-contenv bashio
# ==============================================================================
# Home Assistant Community Add-on: text-generation-webui
# Runs the webui
# ==============================================================================
bashio::log.info "Starting Text Generation Webui..."

APP_DIR="/app"
DEFAULT_MODELS_DIR="/config/models"

if bashio::config.has_value "models_directory" && ! bashio::config.is_empty "models_directory"; then
    MODELS_DIR=$(bashio::config 'models_directory')
    if ! bashio::fs.directory_exists "$MODELS_DIR"; then
        MODELS_DIR=$DEFAULT_MODELS_DIR
        mkdir -p $MODELS_DIR
        bashio::log.warning "The provided models directory '$MODELS_DIR' does not exist! Defaulting to '$DEFAULT_MODELS_DIR'"
    else
        bashio::log.info "Using chosen storage for models: '$MODELS_DIR'"
    fi
else
    MODELS_DIR=$DEFAULT_MODELS_DIR
    mkdir -p $MODELS_DIR
    bashio::log.info "Using default local storage for models."
fi

# ensure we can access the folder
chmod 0777 $MODELS_DIR

export GRADIO_ROOT_PATH=$(bashio::addon.ingress_entry)
bashio::log.info "Serving app from $GRADIO_ROOT_PATH"

cd $APP_DIR
exec python3 server.py --listen --verbose --api --model-dir $MODELS_DIR
