#!/bin/sh
# Nico Schottelius
# 2020-06-14

set -e
set -x

if [ $# -ne 2 ]; then
    echo "$0 viirb-ip-address viirb-id"
    echo "    viirb-ip-address: where to find the viirb"
    echo "    viirb-id: number in decimal format"
    exit 1
fi

viirb_ip=$1; shift

id=$1; shift
viirb_hostname=viirb${id}

root_password=$(pwgen -1 32)

# Save for sending to user
# FIXME: future make this more easy / better to transfer
echo $root_password > ${viirb_hostname}.rootpw

cat <<EOF | ssh -t "root@${viirb_ip}"
# Remove temporary IP
uci delete network.lanv4temp

# Correct test SSID to final one
uci set wireless.default_radio0.ssid='IPv6 everywhere'
uci commit

# Remove our ssh keys
rm -f /etc/dropbear/authorized_keys

# Setup root password
printf "${root_password}\n${root_password}\n" | passwd
EOF

echo "Submit to user the root password = ${root_password}"