#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# MyIP
#
# 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}/network.bash"





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

declare -g      VERSION="1.2.1"
declare -g         NAME="MyIP"
declare -g         HELP="usage: [-h | --help] | [-V | --version] | [-i | --internal] | [-a | --all] [-T | --test] [-v | --verbose] "

declare -g         MODE="EXTERNAL"
declare -g      VERBOSE="FALSE"
declare -g      DRY_RUN="FALSE"
declare -g       RETVAL=0

declare -g WEB_SERVER_NAME="www.rx3"
declare -g   WEB_SERVER_IP="10.0.0.65"
declare -g  WEB_SERVER_URL="http://www.rx3/cgi-bin/myip.cgi"

declare -g   RESOLVER_NAME="resolver1.opendns.com"
declare -g     RESOLVER_IP="208.67.222.222"





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

myip_version_print()
{
    version_print
}





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

myip_help_print()
{
    myip_version_print
    help_print
    echo "  default: external IP"
    echo "       -i: internal IP"
    echo "       -a: internal + external IP"
}





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

myip_args_parse()
{
    tmp_args=$(getopt -o ahiTvV --long all,help,internal,test,verbose,version -n "${NAME}" -- "$@")

    if [ $? != 0 ]; then echo_error "Terminating..."; exit 1; fi

    eval set -- "${tmp_args}"

    while true
    do
        case "$1" in
            # Mode switches
            -a|--all)        MODE="ALL";                          shift;;
            -h|--help)       MODE="EXIT";     myip_help_print;    shift;;
            -i|--internal)   MODE="INTERNAL";                     shift;;
            -V|--version)    MODE="EXIT";     myip_version_print; shift;;

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

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





#-----------------------------------------------------------------------------------------------------------------------------------
# Internal IP Get
#-----------------------------------------------------------------------------------------------------------------------------------

myip_internal_get()
{
    local int_ip
    local int_label
    local web_server_ip

    
    if [[ "${VERBOSE}" == "TRUE" ]]
    then
        int_label="Internal IP: "
    else
        int_label=""
    fi

    
    dns_lookup "A" "${WEB_SERVER_NAME}" "NOCACHE"
    web_server_ip="${dns_value}"

    if [[ "${web_server_ip}" == "" ]]
    then
        if [[ "${VERBOSE}" == "TRUE" ]]
        then
            echo_error "Warning: resolver failed for [${WEB_SERVER_NAME}], using default address: [${WEB_SERVER_IP}]"
        fi

        web_server_ip="${WEB_SERVER_IP}"
    fi

    int_ip="$(cmd_exec curl --resolve "${WEB_SERVER_NAME}:80:${web_server_ip}" -s "${WEB_SERVER_URL}" | ip_ip_filter)"

    echo "${int_label}${int_ip}"
}





#-----------------------------------------------------------------------------------------------------------------------------------
# External IP Get
#-----------------------------------------------------------------------------------------------------------------------------------

myip_external_get()
{
    local ext_ip
    local ext_label
    local resolver_ip

    
    if [[ "${VERBOSE}" == "TRUE" ]]
    then
        ext_label="External IP: "
    else
        ext_label=""
    fi

    dns_lookup "A" "${RESOLVER_NAME}" "NOCACHE"
    resolver_ip="${dns_value}"

    if [[ "${resolver_ip}" == "" ]]
    then
        if [[ "${VERBOSE}" == "TRUE" ]]
        then
            echo_error "Warning: resolver failed for [${RESOLVER_NAME}], using default address: [${RESOLVER_IP}]"
        fi

        resolver_ip="${RESOLVER_IP}"
    fi

    ext_ip="$(cmd_exec dig +short myip.opendns.com "@${resolver_ip}" | ip_ip_filter)"

    echo "${ext_label}${ext_ip}"
}





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

myip_args_parse "$@"



if [[ "${MODE}" == "EXIT" ]]
then
    exit 0
fi

dns_init

case "${MODE}" in
    "INTERNAL")
        myip_internal_get
    ;;

    "EXTERNAL")
        myip_external_get
    ;;

    "ALL")
        myip_internal_get
        myip_external_get
    ;;

    *)
        myip_help_print
        RETVAL=1
    ;;
esac

dns_deinit

exit ${RETVAL}
