#!/bin/bash

# Define the file path
FILE="/etc/udev/rules.d/70-persistent-net.rules"

# Check if the file does not exist
if [ ! -f "$FILE" ]; then
    # Create the file and write the initial contents
    echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"12:33:a6:90:04:73\", ATTR{type}==\"1\", NAME=\"ens18\"" > $FILE
    echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"26:f7:9e:38:88:98\", ATTR{type}==\"1\", NAME=\"ens19\"" >> $FILE
else
    echo "File $FILE already exists. Preparing to update MAC addresses."
fi

# Read the MAC addresses of eth0 and eth1
MAC_ENS18=$(cat /sys/class/net/eth0/address)
MAC_ENS19=$(cat /sys/class/net/eth1/address)

# Backup the original file
cp $FILE "${FILE}.bak"

# Replace the MAC addresses in the file
sed -i "s/ATTR{address}==\".*\", ATTR{type}==\"1\", NAME=\"ens18\"/ATTR{address}==\"$MAC_ENS18\", ATTR{type}==\"1\", NAME=\"ens18\"/g" $FILE
if [ $? -ne 0 ]; then
    echo "Error: Failed to update MAC address for ens18."
    exit 1
fi

sed -i "s/ATTR{address}==\".*\", ATTR{type}==\"1\", NAME=\"ens19\"/ATTR{address}==\"$MAC_ENS19\", ATTR{type}==\"1\", NAME=\"ens19\"/g" $FILE
if [ $? -ne 0 ]; then
    echo "Error: Failed to update MAC address for ens19."
    exit 1
fi

echo "MAC addresses updated successfully." 
