#!/usr/bin/with-contenv bashio
# ==============================================================================
# Start angryipscanner service
# ==============================================================================
TMP_FOLDER=$(mktemp -d)

# VNC is not enabled as a separate service, as it cannot handle multiple
# session when running in the foreground.
VNC_PORT="$(bashio::addon.port 5900)"
ARCH="$(bashio::info.arch)"

# Fix tigervnc for 32 bits ARM
if [[ "armhf armv7" = *"${ARCH}"* ]]; then
    export LD_PRELOAD=/lib/arm-linux-gnueabihf/libgcc_s.so.1
fi

# Fix tigervnc for 64 bits ARM
if [[ "aarch64" = "${ARCH}" ]]; then
    export LD_PRELOAD=/lib/aarch64-linux-gnu/libgcc_s.so.1
fi

# Run it only on localhost if not expose
if bashio::var.has_value "${VNC_PORT}"; then
    bashio::log.warning "Your direct VNC access is not protected!"
    LOCAL_ONLY=no
else
    LOCAL_ONLY=yes
fi

export XDG_RUNTIME_DIR="${TMP_FOLDER}"
export DISPLAY=":0"

bashio::log.info "Starting VNC server (local/${LOCAL_ONLY})..."
tigervncserver \
    -name "Home Assistant - Angry IP Scanner" \
    -geometry 1920x1080 \
    -depth 16 \
    -localhost ${LOCAL_ONLY} \
    -SecurityTypes None \
    --I-KNOW-THIS-IS-INSECURE \
    "${DISPLAY}" \
    &> /dev/null

# Wait for VNC server to start before continuing
bashio::log.info "Angry IP Scanner waiting for VNC to start"
bashio::net.wait_for 5900


# Start Angry IP Scanner
bashio::log.info "Starting the Angry IP Scanner gateway..."
exec ipscan