#!/bin/bash

# Modify enp68s0f2 interface configuration if necessary
sed -i 's/enp68s0f2/enp68s0f2np2/g' /etc/network/interfaces

# File to modify
INTERFACES_FILE="/etc/network/interfaces"

# Check if enp1s0f0 exists in the file
if grep -q "auto enp1s0f0" "$INTERFACES_FILE"; then
    # Extract enp1s0f0 configuration block
    CONFIG_BLOCK=$(awk '/^auto enp1s0f0/,/^$/ {print}' "$INTERFACES_FILE")

    # Replace enp1s0f0 with enp1s0f0np0 in the configuration block
    CONFIG_BLOCK_UPDATED=$(echo "$CONFIG_BLOCK" | sed 's/enp1s0f0/enp1s0f0np0/g')

    # Check if the block for enp1s0f0np0 already exists to prevent duplication
    if ! grep -q "auto enp1s0f0np0" "$INTERFACES_FILE"; then
        # Append the updated block to the interfaces file
        echo -e "\n# Configuration for enp1s0f0np0\n$CONFIG_BLOCK_UPDATED" >> "$INTERFACES_FILE"
    fi
fi

# Resize the logical volume
lvresize -r -l +100%FREE /dev/mapper/vg00-lv_root

# Update package repository and upgrade system
export DEBIAN_FRONTEND=noninteractive
echo "deb [arch=amd64] http://download.proxmox.com/debian/pbs trixie pbs-no-subscription" > /etc/apt/sources.list.d/pbs-install-repo.list
wget https://enterprise.proxmox.com/debian/proxmox-release-trixie.gpg -O /etc/apt/trusted.gpg.d/proxmox-release-trixie.gpg
apt -y update
apt -y full-upgrade
apt -y install proxmox-backup
apt -y remove linux-image-amd64 'linux-image-6.12*'

# Update GRUB to use the new kernel
update-grub

# Remove os-prober (optional)
apt -y remove os-prober

# Sleep to allow all changes to take effect
sleep 5

# Optional: A ping to ensure connectivity
curl https://listabridge.clouvider.net/ping/mother_is_watching

# Power cycle via IPMI
ipmitool chassis power cycle

# Reboot the system
init 6
exit 0
