#!/command/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Home Assistant Community Add-on: Ps3NetSrv
# Runs the ps3netsrv
# ==============================================================================

if bashio::config.has_value 'networkdisks'; then

    # Define variables
    MOREDISKS=$(bashio::config 'networkdisks')

    # Clean data
    MOREDISKS=${MOREDISKS// \/\//,\/\/}
    MOREDISKS=${MOREDISKS//, /,}
    MOREDISKS=${MOREDISKS// /"\040"}

    # Mounting disks
    # shellcheck disable=SC2086
    for disk in ${MOREDISKS//,/ }; do # Separate comma separated values

        # Clean name of network share
        # shellcheck disable=SC2116,SC2001
        disk=$(echo $disk | sed "s,/$,,") # Remove / at end of name
        disk="${disk//"\040"/ }"            #replace \040 with
        diskname="${disk//\\//}"            #replace \ with /
        diskname="${diskname##*/}"          # Get only last part of the name

        bashio::log.info "Starting Ps3NetSrv.... ($diskname)"
        exec ps3netsrv /mnt/"$diskname"
        break
    done
fi
