#!/bin/bash

# Huawei Interface Configuration Generator
# This script reads interface data and generates Huawei interface config commands

INPUT_FILE="interface-description.txt"
OUTPUT_FILE="huawei_interface_config.txt"

echo "Generating Huawei Interface configuration..." > "$OUTPUT_FILE"

# Process the file and generate interface configs
awk '
{
    if(NF>=2) {
        interface = $1;
        # Get all remaining fields as description
        desc = "";
        for(i=2; i<=NF; i++) {
            desc = desc " " $i;
        }
        gsub(/^ /, "", desc);
        
        # Convert interface names to Huawei format
        if(interface ~ /^TenGigabitEthernet/) {
            # Convert TenGigabitEthernet 1/1/1 to 10GE 0/0/0
            split(interface, parts, " ");
            if(parts[2] ~ /^[0-9]+\/[0-9]+\/[0-9]+$/) {
                split(parts[2], slot_parts, "/");
                huawei_interface = "10GE " (slot_parts[1]-1) "/" (slot_parts[2]-1) "/" (slot_parts[3]-1);
            } else {
                huawei_interface = "10GE " parts[2];
            }
        }
        else if(interface ~ /^hundredGigE/) {
            # Convert hundredGigE 1/6 to 100GE 0/5
            split(interface, parts, " ");
            if(parts[2] ~ /^[0-9]+$/) {
                huawei_interface = "100GE 0/" (parts[2]-1);
            } else {
                huawei_interface = "100GE " parts[2];
            }
        }
        else if(interface ~ /^fortyGigE/) {
            # Convert fortyGigE 1/8/1 to 40GE 0/7/0
            split(interface, parts, " ");
            if(parts[2] ~ /^[0-9]+\/[0-9]+$/) {
                split(parts[2], slot_parts, "/");
                huawei_interface = "40GE " (slot_parts[1]-1) "/" (slot_parts[2]-1);
            } else {
                huawei_interface = "40GE " parts[2];
            }
        }
        else if(interface ~ /^Port-channel/) {
            # Convert Port-channel 3 to Eth-Trunk3
            split(interface, parts, " ");
            huawei_interface = "Eth-Trunk" parts[2];
        }
        else {
            # For other interface types, keep as is
            huawei_interface = interface;
        }
        
        # Generate the configuration
        printf "interface %s\n", huawei_interface;
        printf " description %s\n", desc;
        printf "#\n";
        printf "\n";
    }
}' "$INPUT_FILE" >> "$OUTPUT_FILE"

echo "Configuration saved to: $OUTPUT_FILE"
echo "Total interfaces processed: $(grep -c '^interface ' "$OUTPUT_FILE")"
