#!/usr/bin/env bash

ovpn_directory="/etc/openvpn/client/ovpn-enabled"
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.
# TODO: Remove the current config from the list of choices.
# Choose a random VPN configuration inside /etc/openvpn/ovpn-enabled
ovpn_file=$(shuf -n1 -e ${ovpn_directory}/*)

# TODO: Also echo previous configuration's name.
echo "Using OpenVPN configuration $(basename "${ovpn_file}" .ovpn)"

ln -fs "${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.
