# the init script the image runs (read inside the image)
#!/usr/bin/with-contenv bash
# shellcheck shell=bash

# copy config
if [[ ! -f /config/settings.json ]]; then
    cp /defaults/settings.json /config/settings.json
fi

if [[ -n "${USER}" ]] && [[ -n "${PASS}" ]]; then
    echo -E "$(jq -r '.["rpc-authentication-required"] = true' /config/settings.json)" >/config/settings.json
    echo -E "$(jq -r --arg user "${USER}" '.["rpc-username"] = $user' /config/settings.json)" >/config/settings.json
    echo -E "$(jq -r --arg pass "${PASS}" '.["rpc-password"] = $pass' /config/settings.json)" >/config/settings.json
else
    echo -E "$(jq -r '.["rpc-authentication-required"] = false' /config/settings.json)" >/config/settings.json
fi

if [[ -n "${WHITELIST}" ]]; then
    echo -E "$(jq -r '.["rpc-whitelist-enabled"] = true' /config/settings.json)" >/config/settings.json
    echo -E "$(jq -r --arg whitelist "${WHITELIST}" '.["rpc-whitelist"] = $whitelist' /config/settings.json)" >/config/settings.json
else
    echo -E "$(jq -r '.["rpc-whitelist-enabled"] = false' /config/settings.json)" >/config/settings.json
fi

if [[ -n "${HOST_WHITELIST}" ]]; then
    echo -E "$(jq -r '.["rpc-host-whitelist-enabled"] = true' /config/settings.json)" >/config/settings.json
    echo -E "$(jq -r --arg host_whitelist "${HOST_WHITELIST}" '.["rpc-host-whitelist"] = $host_whitelist' /config/settings.json)" >/config/settings.json
else
    echo -E "$(jq -r '.["rpc-host-whitelist-enabled"] = false' /config/settings.json)" >/config/settings.json
fi

if [[ -n "${PEERPORT}" ]]; then
    echo -E "$(jq -r --argjson peerport "${PEERPORT}" '.["peer-port"] = $peerport' /config/settings.json)" >/config/settings.json
    echo -E "$(jq -r '.["peer-port-random-on-start"] = false' /config/settings.json)" >/config/settings.json
fi

if ! test -f /proc/net/if_inet6; then
    echo -E "$(jq -r '.["rpc-bind-address"] = "0.0.0.0"' /config/settings.json)" > /config/settings.json
fi

if [[ -n "${UMASK}" ]]; then
    echo -E "$(jq -r --arg umask "${UMASK}" '.["umask"] = $umask' /config/settings.json)" >/config/settings.json
fi

if [[ -z ${LSIO_NON_ROOT_USER} ]] && [[ -z ${LSIO_READ_ONLY_FS} ]]; then
    # Handle old theme locations
    mkdir -p {/transmissionic,/combustion-release,/flood-for-transmission,/kettu,/transmission-web-control}
    echo /transmissionic /combustion-release /flood-for-transmission /kettu /transmission-web-control | xargs -n1 ln -s /defaults/index.html
fi

if [[ -z ${LSIO_NON_ROOT_USER} ]]; then
    lsiown -R abc:abc \
        /config

    if grep -qe ' /downloads ' /proc/mounts; then
        if [[ "$(stat -c '%U' /downloads)" != "abc" ]]; then
            lsiown abc:abc /downloads
        fi

        if [[ "$(stat -c '%U' /downloads/complete)" != "abc" ]]; then
            lsiown abc:abc /downloads/complete
        fi

        if [[ "$(stat -c '%U' /downloads/incomplete)" != "abc" ]]; then
            lsiown abc:abc /downloads/incomplete
        fi
    fi

    if grep -qe ' /watch ' /proc/mounts; then
        if [[ "$(stat -c '%U' /watch)" != "abc" ]]; then
            lsiown abc:abc /watch
        fi
    fi
fi
