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

source /lib/lsb/init-functions

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

pppoe-up()
{
    test -n "${1}" || return 1
    local dev=${1}

    local timeout=15000000 gap=500000
    local n=$((timeout / gap)) i=0
    local state

    local args=(nodetach call ${EXTERNAL_PPPOE_PEER_NAME}
		ip-up-script ${LOCAL_DIR}/bin/apl_pppoe_up
		ip-down-script ${LOCAL_DIR}/bin/apl_pppoe_down)

    pppd "${args[@]}" > /dev/null 2>&1 &
    local pid=${!}

    echo ${pid} > /var/run/pppoe.pid

    while ! ip -4 address show dev ${EXTERNAL_PPPOE_IF} > /dev/null 2>&1
    do
	test ${i} -lt ${n} || break
	usleep ${gap}

	if ! kill -0 "${pid}" 2> /dev/null ; then
            wait "${pid}"
            return ${?}
	fi

	((i++))
    done
    test ${i} -ne ${n} || return 11
    test ${PPPOE_CONTEXT} == boot || return 0

    i=0
    while true
    do
	test ! -f ${RUN_DIR}/${EXTERNAL_PPPOE_DONE} || break
	test ${i} -lt ${n} || break
	usleep ${gap}
	((i++))
    done
    test ${i} -ne ${n} || return 13

    i=0
    while true
    do
	read state < ${RUN_DIR}/${EXTERNAL_PPPOE_DONE}
	test ${state} != 'finished' || return 0
	test ${i} -lt ${n} || break
	usleep ${gap}
	((i++))
    done

    return 15
}

pppoe-down()
{
    test -n "${1}" || return 1
    local dev=${1}

    local prog='pppd'
    local timeout=3000000 gap=200000
    local n=$((timeout / gap)) i=0

    local pid=$(cat /var/run/pppoe.pid 2>/dev/null)
    test -n "${pid}" || pid=$(pidof ${prog} 2> /dev/null)

    rm -f /var/run/pppoe.pid

    if test -n "${pid}" && kill -0 "${pid}" 2>/dev/null ; then
        kill -TERM "${pid}" 2>/dev/null

	while kill -0 "${pid}" 2>/dev/null
	do
	    test ${i} -lt ${n} || break
	    usleep ${gap}
	    ((i++))
	done

	if test ${i} -eq ${n} ; then
	    kill -KILL "${pid}" 2>/dev/null
	    usleep ${timeout}
	fi
    else
	killall --quiet -TERM ${prog}
	usleep ${timeout}
	killall --quiet -KILL ${prog}
	usleep ${timeout}
    fi
    
    return 0
}

case "${2}" in
    up)
	log_info_msg "Bringing up PPPoE for ${1}..."
	pppoe-up ${1}
	RETVAL=${?}

	if [ ${RETVAL} -ne 0 ] ; then
	    if [ -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
	    fi

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

	    test ${PPPOE_CONTEXT} != boot || rm -f /var/run/${DYNAMIC_IP_BOOTING}

	    ${LOCAL_DIR}/sbin/apl_network_dependent_configure
	fi

	[ ${RETVAL} -eq 0 ]
	evaluate_retval
	;;
    down)
	log_info_msg "Bringing down PPPoE for ${1}..."
	pppoe-down ${1}
	evaluate_retval
	;;
    *)
	echo "Usage: ${0} [interface] {up|down}"
	exit 1
	;;
esac
