#!/bin/bash

# Function to parse netstat output and format results into arrays
parse_netstat() {
    # Run netstat and capture the output
    netstat_output=$(netstat -s)

    tcp_array=()
    udp_array=()

    # Extract Tcp statistics
    tcp_array+=($(echo "$netstat_output" | grep -A 8 "Tcp:" | tail -n +2 | head -n 8 | awk '{print $1}'))

    # Extract Udp statistics
    udp_array+=($(echo "$netstat_output" | grep -A 6 "Udp:" | tail -n +2 | head -n 6 | awk '{print $1}'))

    # Output the arrays
    echo "Tcp Array: ${tcp_array[@]}"
    echo "Udp Array: ${udp_array[@]}"
}

# Function to handle Windows (PowerShell)
#parse_netstat_windows() {
    # Run netstat and capture the output
 #   netstat_output=$(netstat -s)

    # Arrays to store the desired values
  #  tcp_array=()
 #   udp_array=()
#
    # Extract Tcp statistics
  #  tcp_array+=($(echo "$netstat_output" | Select-String -Pattern "Tcp:" -Context 0,8 | Select-Object -Skip 1 | ForEach-Object { $_.Line.Split()[0] }))

    # Extract Udp statistics
   # udp_array+=($(echo "$netstat_output" | Select-String -Pattern "Udp:" -Context 0,6 | Select-Object -Skip 1 | ForEach-Object { $_.Line.Split()[0] }))

    # Output the arrays
    #echo "Tcp Array: ${tcp_array[@]}"
    #echo "Udp Array: ${udp_array[@]}"
#}

# Initialize a counter
counter=1

# Detect OS and call appropriate function in a loop
while true; do
    # Print the iteration number as First, Second, etc.
    if [ $counter -eq 1 ]; then
        echo "First:"
    elif [ $counter -eq 2 ]; then
        echo "Second:"
    elif [ $counter -eq 3 ]; then
        echo "Third:"
    else
        # For all other counts
        echo "$counter th:"
    fi

    # Check OS type and call the appropriate function
    if [[ "$OSTYPE" == "linux-gnu"* ]] || [[ "$OSTYPE" == "darwin"* ]]; then
        parse_netstat
    elif [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
        parse_netstat_windows
    else
        echo "Unsupported OS"
    fi
    
    # Increment the counter for the next iteration
    ((counter++))
    
    # Wait for 12 seconds before repeating
    sleep 12
done

