#!/bin/sh

# This script is run by the pppd after the link is established.
# It should be used to add routes, set IP address, run the mailq, etc.

# Check if the required number of arguments is provided
if [ $# -ne 5 ]; then
    echo "Usage: $0 <interface> <tty> <speed> <local_ip> <peer_ip>" >&2
    exit 1
fi

# Reset the PATH variable
PATH=/usr/sbin:/sbin:/usr/bin:/bin
export PATH

# Log the start of the script
logger "Starting ip-up script for interface $1 with local IP $4 and peer IP $5"

# Define the directory containing additional scripts
IP_UP_D_DIR="/etc/ppp/ip-up.d"

# Check if the directory exists
if [ ! -d "$IP_UP_D_DIR" ]; then
    echo "Directory $IP_UP_D_DIR does not exist." >&2
    exit 1
fi

# Loop through all files in the ip-up.d directory and execute them if they are executable
for script in "$IP_UP_D_DIR"/*; do
    if [ -x "$script" ]; then
        "$script" "$@" || {
            echo "Error executing $script" >&2
            exit 1
        }
    fi
done

# Log the successful completion of the script
logger "Completed ip-up script for interface $1"

exit 0
