#!/bin/bash

###########################################################################
#
# MODULE:       Scripts
# 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/>.
#
###########################################################################

CACHEGUARD_DIR=/etc/sysconfig/cacheguard
source ${CACHEGUARD_DIR}/constant

apl-main()
{
    test -n "${1}" || return 1
    test -n "${2}" || return 2
    test -n "${3}" || return 3
    test -n "${4}" || return 4
    test -n "${5}" || return 5
    local ppp_dev=${1}
    local eth_dev=${2}
    local speed=${3}
    local ip=${4}
    local router=${5}

    rm -f ${RUN_DIR}/${EXTERNAL_PPPOE_DONE}

    if test -f ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.new ; then
	mv -f \
	   ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.new \
	   ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.old
	chmod 644 ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.old
    fi

    local tmp_pppoe_ip_configuration=/tmp/pppoe-ip-configuration.${$}

    cat << EOT > ${tmp_pppoe_ip_configuration}
${ppp_dev}
${eth_dev}
${ip}
${router}
EOT
    mv -f ${tmp_pppoe_ip_configuration} ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.new
    chmod 644 ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.new

    touch ${RUN_DIR}/${EXTERNAL_PPPOE_DONE}

    test ! -f ${LOCK_DIR}/apl_apply || return 0

    if test -f ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.old ; then
	diff --brief \
	     ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.old \
	     ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.new > /dev/null 2>&1
	test ${?} -ne 0 || return 0
    fi

    su - ${ADMIN_NAME} --command 'cancel' 2> /dev/null

    PPPOE_HOOK_EVENT='modified' apl_apply > /dev/null 2>&1 &
}

apl-main "${@}"
