#!/bin/bash

# Check if correct number of arguments is provided
if [ "$#" -ne 4 ]; then
    echo "Usage: $0 <template_vmid> <start_vmid> <end_vmid> <vlan_id>"
    exit 1
fi

template_vmid=$1
start_vmid=$2
end_vmid=$3
vlan_id=$4
bridge="vmbr1"
firewall=0

# Clone LXC containers
for (( vmid=start_vmid; vmid<=end_vmid; vmid++ )); do
    hostname="kt-compute-$vmid"
    echo "Cloning template $template_vmid to new VMID $vmid with hostname $hostname..."
    pct clone "$template_vmid" "$vmid" --hostname "$hostname" --full false
    if [ $? -ne 0 ]; then
        echo "Failed to clone $template_vmid to $vmid"
        continue
    fi

done

# Configure network for all cloned containers
echo "Configuring network for cloned containers..."
for (( vmid=start_vmid; vmid<=end_vmid; vmid++ )); do
    echo "Setting network for VMID $vmid: $bridge, VLAN $vlan_id, firewall=$firewall"
    pct set "$vmid" -net0 name=eth0,bridge=$bridge,firewall=$firewall,tag=$vlan_id

done

echo "All containers cloned and network configured successfully!"
