#!/bin/bash

###########################################################################
#
# MODULE:       GUI
# COPYRIGHT:    (C) 2009-2025 by CacheGuard Technologies Ltd (UK)
# COPYRIGHT:    (C) 2026-2026 by CacheGuard Technologies SAS (FR)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

get-ip-mk()
{
    local ip=${1}
    local mk=${2}

    if test -n "${ip}" ; then
	test -n "${mk}" || mk='255.255.255.0'
    else
	ip='0.0.0.0'
	mk='0.0.0.0'
    fi

    echo "${ip} ${mk}"
}

set-ip()
{
    local transaction=/tmp/${TRANSACTION_FILE}.wadmin.${REMOTE_USER}.${$}

    local i n range j=0
    local vlan ip mk ip_mk

    if gui-contextual-is-allowed ; then

	local external_vlan=${VALUES[0]}
	test -n "${external_vlan}" || external_vlan='none'

	echo "vlan external ${external_vlan}" > ${transaction}

	local ip_external_mode=${VALUES[1]}

	case ${ip_external_mode} in
	    static)
		ip_mk=$(get-ip-mk "${VALUES[2]}" "${VALUES[3]}")
		echo "ip external ${ip_external_mode} ${ip_mk}" >> ${transaction}

		ip_mk=$(get-ip-mk "${VALUES[4]}" "${VALUES[5]}")
		echo "ip internal ${ip_mk}"  >> ${transaction}

		ip_mk=$(get-ip-mk "${VALUES[6]}" "${VALUES[7]}")
		echo "ip auxiliary ${ip_mk}" >> ${transaction}
		n=8
		;;
	    dhcp)
		echo "ip external ${ip_external_mode}" >> ${transaction}

		ip_mk=$(get-ip-mk "${VALUES[2]}" "${VALUES[3]}")
		echo "ip internal ${ip_mk}"  >> ${transaction}

		ip_mk=$(get-ip-mk "${VALUES[4]}" "${VALUES[5]}")
		echo "ip auxiliary ${ip_mk}" >> ${transaction}
		n=6
		;;
	    pppoe)
		echo "ip external ${ip_external_mode} '${VALUES[2]}' '${VALUES[3]}' '${VALUES[4]}'" >> ${transaction}

		ip_mk=$(get-ip-mk "${VALUES[5]}" "${VALUES[6]}")
		echo "ip internal ${ip_mk}" >> ${transaction}

		ip_mk=$(get-ip-mk "${VALUES[7]}" "${VALUES[8]}")
		echo "ip auxiliary ${ip_mk}" >> ${transaction}
		n=9
		;;
	    *)
		return 1
		;;
	esac

	for((i=n ; i<ATTRIBUTE_NB ; i++))
	do
	    range=$[${j} % 3]
	    case ${range} in
		0)
		    vlan=${VALUES[${i}]}
		    ;;
		1)
		    ip=${VALUES[${i}]}
		    ;;
		2)
		    mk=${VALUES[${i}]}

		    ip_mk=$(get-ip-mk "${ip}" "${mk}")
		    echo "ip internal.${vlan} ${ip_mk}" >> ${transaction}
		    ;;
		*)
		    return 1
		    ;;
	    esac
	    ((j++))
	done
    else
	ip_mk=$(get-ip-mk "${VALUES[0]}" "${VALUES[1]}")
	echo "ip internal ${ip_mk}" > ${transaction}
    fi

    execute-transaction ${transaction}
}

set-ip
