#!/usr/bin/env bash

OVPN_DIRECTORY="/etc/openvpn/client/ovpn"
CURRENT_OVPN_PATH="/etc/openvpn/client/current.ovpn"

# TODO: Investigate better ways to check for rootness. This is just a courtesy message, though.
#   Non-root users will not be able to do anything with this anyway.
if [ "$(whoami)" != "root" ]; then
    echo "This script needs root privileges to interact with the openvpn service."
    exit
fi

# TODO: Add the ability to pass arbitrary ovpn files.

# Choose a random VPN configuration inside /etc/openvpn/ovpn
ovpn_file=$(shuf -n1 -e ${OVPN_DIRECTORY}/*)

# TODO: Check that ovpn_file exists.
echo "Using OpenVPN configuration $(basename "${ovpn_file}" .ovpn)"

rm "${CURRENT_OVPN_PATH}"
ln -s "${ovpn_file}" "${CURRENT_OVPN_PATH}"

echo "Restarting OpenVPN service."
systemctl restart openvpn

# TODO: Add a way to report any errors. Look at systemctl is-active and is-failed.
