#!/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 
    local ppp_dev=${1}
    local eth_dev=${2}
    local speed=${3}
    local ip=${4}
    local peer_ip=${5}

    local pppoe_context

    if test -f /var/run/${DYNAMIC_IP_BOOTING} ; then
	pppoe_context=boot
    else
	pppoe_context=run
    fi

    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.${$}
    local now=$(date +%s 2>/dev/null)

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

    echo 'working' > ${RUN_DIR}/${EXTERNAL_PPPOE_DONE}

    if test -f ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.old ; then

	local lines
	declare -a lines

	mapfile -t lines < ${RUN_DIR}/${EXTERNAL_PPPOE_IP_CONFIGURATION}.old

	local old_ip=${lines[2]}
	local old_peer_ip=${lines[3]}

	if test "${ip}" == "${old_ip}" -a "${peer_ip}" == "${old_peer_ip}" ; then
	    echo 'finished' > ${RUN_DIR}/${EXTERNAL_PPPOE_DONE}
	    return 0
	fi
    fi

    case ${pppoe_context} in
	run)
	    echo 'finished' > ${RUN_DIR}/${EXTERNAL_PPPOE_DONE}
	    test ! -f ${LOCK_DIR}/apl_apply || return 0
	    su - ${ADMIN_NAME} --command 'cancel' 2> /dev/null
	    PPPOE_HOOK_EVENT='modified' apl_apply > /dev/null 2>&1 &
	    ;;
	boot)
	    rm -f /var/run/${DYNAMIC_IP_BOOTING}
	    apl_network_dependent_configure
	    echo 'finished' > ${RUN_DIR}/${EXTERNAL_PPPOE_DONE}
	    ;;
	*)
	    return 255
	    ;;
    esac
}

apl-main "${@}"
