#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# Rx3 OpenVPN Status
#
# Copyright (C) 2025-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; If not, see
# <https://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------------------------------------------------------------



#-----------------------------------------------------------------------------------------------------------------------------------
# Includes
#-----------------------------------------------------------------------------------------------------------------------------------

: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/base.bash"





#-----------------------------------------------------------------------------------------------------------------------------------
# Global Variables
#-----------------------------------------------------------------------------------------------------------------------------------

declare -g   VERSION="1.2.4"
declare -g      NAME="openvpn-status"
declare -g      HELP="usage: [-h | --help] | [-V | --version] | [-v | --verbose] [dev]"

declare -g EXIT_CODE=0
declare -g      MODE="DEFAULT"
declare -g   VERBOSE="FALSE"
declare -g   DRY_RUN="FALSE"

declare -g     DEBUG=""
#declare -g     DEBUG="echo"
#declare -g     DEBUG=":"

declare -g       LOG=""
#declare -g       LOG=":"
#declare -g       LOG="echo"

declare -g    STATUS_DIR="/var/lib/openvpn"
declare -g           DEV=""





#-----------------------------------------------------------------------------------------------------------------------------------
# Version Print
#-----------------------------------------------------------------------------------------------------------------------------------

ovs_version_print()
{
    version_print
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Help Print
#-----------------------------------------------------------------------------------------------------------------------------------

ovs_help_print()
{
    ovs_version_print
    help_print
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Arg Parse
#-----------------------------------------------------------------------------------------------------------------------------------

ovs_args_parse()
{
    tmp_args=$(getopt -o hvV --long help,verbose,version -n "${NAME}" -- "$@")

    if [ $? != 0 ]; then echo "Terminating..." >&2; exit 1; fi

    eval set -- "${tmp_args}"

    while true
    do
        case "$1" in
	    # Mode switches
	    -h|--help)                                         MODE="HELP";                       shift;;
	    -V|--version)                                      MODE="VERSION";                    shift;;

	    # Global options
            -v|--verbose)                                   VERBOSE="TRUE";                       shift;;

            # End of options
            --)                                                                                   shift;  break;;
            *) echo "args_parse internal error [$1]!";                                                    exit 1;;
        esac
    done

    if [[ "${MODE}" == "DEFAULT" ]]
    then
        if [[ "${#}" -ge "1" ]]
        then
            MODE="DEVICE"
             DEV="$1"
        else
            MODE="ALL"
        fi
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Status Device
#-----------------------------------------------------------------------------------------------------------------------------------

ovs_status_device()
{
    local dev="$1"

    cat "${STATUS_DIR}/${dev}.status"
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Status All
#-----------------------------------------------------------------------------------------------------------------------------------

ovs_status_all()
{
    awk '{print FILENAME ": " $0}' "${STATUS_DIR}"/*.status
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------------------------------------------------------------------

vsl_add

ovs_args_parse "$@"


case "${MODE}" in
    "EXIT")
        exit ${EXIT_CODE}
    ;;
    
    "HELP")
        ovs_help_print
        exit ${EXIT_CODE}
    ;;

    "VERSION")
        ovs_version_print
        exit ${EXIT_CODE}
    ;;
esac


case "${MODE}" in
    DEVICE)
        ovs_status_device "${DEV}"
    ;;

    ALL)
        ovs_status_all
    ;;

    *)
        echo "Usage: $0 [dev]"
        EXIT_CODE=1
    ;;
esac


exit ${EXIT_CODE}
