#!/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/>.
#
###########################################################################

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

    local i n range j=0
    local vlan ip netmask

    if gui-contextual-is-allowed ; then

	local ip_external_mode=${VALUES[0]}

	case ${ip_external_mode} in
	    static)
		echo "ip external ${ip_external_mode} ${VALUES[1]} ${VALUES[2]}" > ${transaction}
		echo "ip internal ${VALUES[3]} ${VALUES[4]}"  >> ${transaction}
		echo "ip auxiliary ${VALUES[5]} ${VALUES[6]}" >> ${transaction}
		n=7
		;;
	    dhcp)
		echo "ip external ${ip_external_mode}" > ${transaction}
		echo "ip internal ${VALUES[1]} ${VALUES[2]}"  >> ${transaction}
		echo "ip auxiliary ${VALUES[3]} ${VALUES[4]}" >> ${transaction}
		n=5
		;;
	    pppoe)
		echo "ip external ${ip_external_mode} '${VALUES[1]}' '${VALUES[2]}' '${VALUES[3]}'" > ${transaction}
		echo "ip internal ${VALUES[4]} ${VALUES[5]}"  >> ${transaction}
		echo "ip auxiliary ${VALUES[6]} ${VALUES[7]}" >> ${transaction}
		n=8
		;;
	    *)
		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)
		    netmask=${VALUES[${i}]}
		    echo "ip internal.${vlan} ${ip} ${netmask}" >> ${transaction}
		    ;;
		*)
		    return 1
		    ;;
	    esac
	    ((j++))
	done
    else
	echo "ip internal ${VALUES[0]} ${VALUES[1]}" > ${transaction}
    fi

    execute-transaction ${transaction}
}

set-ip
