#!/bin/bash

set -e

print_help() {
    cat <<HELP
Usage: session-multi-sn-upgrade

Updates Session node configurations to add session-router configurations for any existing multi-SNs.
HELP
}

if [ "$#" -ne 0 ] || [[ "$1" =~ ^- ]]; then
    print_help
    exit 1
fi

if [ "$UID" -ne 0 ]; then
    echo "This script must be run as root (e.g. via sudo)."
    exit 1
fi



srouter_upgrade=()

shopt -s nullglob

echo "Finding service nodes that need session-router:"
for x in /etc/oxen/node-[0-9][0-9].conf; do
    nn=${x/*node-/};
    nn=${nn/.conf/};
    if ! [ -e /etc/session-router/relay-$nn.ini ]; then
        srouter_upgrade+=($nn)
        echo "    - $nn"
    fi
done

if [ "${#srouter_upgrade[@]}" = "0" ]; then
    echo "Nothing to upgrade"
    exit 0
fi

starts=()

if [ "${#srouter_upgrade[@]}" -gt 0 ]; then

    public_ip=""

    for nn in "${srouter_upgrade[@]}"; do
        old_ini="/etc/loki/lokinet-router-$nn.ini"
        if [ -f "$old_ini" ]; then
            p="$(grep '^public-ip=[1-9]' $old_ini)"
            if [ -n "$p" ]; then
                public_ip="${p/public-ip=/}"
                break
            fi
        fi
    done

    echo -e "\n"
    echo "About to set up session-router configs for the multi SNs listed above"
    echo "Each session-router instance will communication on UDP port 119NN where NN is"
    echo "the service number two-digit number (e.g. session-router-relay@42 will be"
    echo "configured to use port 11942)"
    echo ""

    if [ -n "$public_ip" ]; then
        echo "This script found 'public-ip=$public_ip' in your existing lokinet multi-sn"
        echo "config and will replicate that in the generate session-router configs in"
        echo "/etc/session-router/relay-NN.ini"
        echo ""
    fi

    read -p "Press enter to continue, Ctrl-C to abort."

    mkdir -p /etc/session-router
    mkdir -p /var/lib/session-router
    chown _session:_session /var/lib/session-router
    for nn in "${srouter_upgrade[@]}"; do
        mkdir -p /var/lib/session-router/relay-$nn
        chown _session:_session /var/lib/session-router/relay-$nn
        f=/etc/session-router/relay-$nn.ini
        listen_args=
        if [ -n "$public_ip" ]; then
            listen_args="--public-ip=$public_ip --public-port=119$nn"
        else
            listen_args="--listen=:119$nn"
        fi

        /usr/bin/session-router-config --relay --data-dir=/var/lib/session-router/relay-$nn \
            $listen_args --oxend-rpc=ipc:///var/lib/oxen/node-$nn/oxend.sock \
            $f
        chmod 644 $f

        systemctl enable session-router-relay@$nn.service
        starts+=(session-router-relay@$nn.service)
    done
fi

echo -e "\nAll done."
if [ "${#starts[*]}" -gt 0 ]; then
    echo -e "\nThe following services have been configured and enabled, and need to be started:\n    ${starts[*]}\n"

    echo "Press enter to start them now, or Ctrl-C if you want to start them yourself later."

    read

    for s in "${starts[@]}"; do
        echo "Starting $s"
        systemctl start $s
        sleep 0.5
    done
fi
