#!/bin/bash

# Function to print usage information
usage() {
    echo "Usage: $0 [-i interface_file]"
    echo "Options:"
    echo "  -i   Specify a file containing a list of interfaces (optional)"
    exit 1
}

# Function to check if an interface is a loopback interface
is_loopback() {
    local interface=$1
    [[ $interface == lo* ]]
}

# Function to get DHCP information for a specific interface
get_dhcp_info() {
    local interface=$1
    if is_loopback "$interface"; then
        echo "Skipping loopback interface: $interface"
    else
        echo "Getting DHCP information for interface: $interface"
        dhcpcd -T "$interface"
    fi
}

# Function to get base interface name for VLAN interfaces
get_base_interface() {
    local interface=$1
    # Extract base interface name (before @ symbol for VLAN interfaces)
    echo "$interface" | awk -F'@' '{print $1}'
}

# Function to get DHCP information for all interfaces
get_all_dhcp_info() {
    local interfaces=($(ip -o link show | awk -F': ' '{print $2}'))
    for interface in "${interfaces[@]}"; do
        get_dhcp_info "$interface"
    done
}

# Parse command-line options
while getopts "i:" opt; do
    case $opt in
        i)
            interface_file="$OPTARG"
            if [ ! -f "$interface_file" ]; then
                echo "Error: Interface file '$interface_file' not found."
                exit 1
            fi
            interfaces=($(cat "$interface_file"))
            ;;
        *)
            usage
            ;;
    esac
done

# If no interface file is provided, get DHCP information for all interfaces
if [ -z "$interface_file" ]; then
    get_all_dhcp_info
else
    # Get DHCP information for interfaces specified in the file
    for interface in "${interfaces[@]}"; do
        base_interface=$(get_base_interface "$interface")
        get_dhcp_info "$base_interface"
    done
fi

