#!/bin/bash
clear
export LC_ALL=C
export DEBIAN_FRONTEND=noninteractive

# Script designed to upgrade dependencies in PNETLab UBUNTU 18.04
# Requirement: You need to have UBUNTU 18.04
# Mirror: pnetlab-mirrors.labhub.eu.org

GREEN='\033[32m'
RED='\033[31m'
BLUE='\033[34m'
NO_COLOR='\033[0m'

MIRROR_URL="https://pnetlab-mirrors.labhub.eu.org"

rm /var/lib/dpkg/lock* &>/dev/null
dpkg --configure -a &>/dev/null

# --- VERSION SELECTION ---
VERSIONS=(
    "5.5.18.zip" "5.5.17.zip" "5.5.15.zip" "5.5.14.zip" "5.5.12.zip" "5.5.11.zip" "5.5.10.zip" "5.5.8.zip"
    "5.4.6.zip" "5.3.45.zip" "5.3.11.zip" "5.3.10.zip" "5.3.8.zip" "5.3.7.zip" "5.3.5.zip" "5.3.4.zip"
    "5.3.2.zip" "5.3.0.zip" "5.2.9.zip" "5.2.8.zip" "5.2.7.zip" "5.0.1.zip"
)

SELECTED_ZIP=""

# Check if version passed as parameter
if [ ! -z "$1" ]; then
    INPUT_VER="$1"
    # Append .zip if missing
    [[ "$INPUT_VER" != *.zip ]] && INPUT_VER="${INPUT_VER}.zip"
    
    # Validate against known list
    for v in "${VERSIONS[@]}"; do
        if [ "$v" == "$INPUT_VER" ]; then
            SELECTED_ZIP="$v"
            break
        fi
    done
    
    if [ -z "$SELECTED_ZIP" ]; then
        echo -e "${RED}Error: Version '$1' not found in testing repository.${NO_COLOR}"
        echo "Available versions: ${VERSIONS[*]}"
        exit 1
    fi
else
    # Interactive menu
    echo -e "${BLUE}=== PNetLab v5 Testing Version Selection ===${NO_COLOR}"
    echo "Please choose a version to install:"
    for i in "${!VERSIONS[@]}"; do
        printf " [%2d] %s\n" $((i+1)) "${VERSIONS[$i]}"
    done

    read -p "Enter choice [1-${#VERSIONS[@]}]: " choice
    if [[ "$choice" -lt 1 || "$choice" -gt "${#VERSIONS[@]}" ]]; then
        echo -e "${RED}Invalid choice. Exiting.${NO_COLOR}"
        exit 1
    fi
    SELECTED_ZIP="${VERSIONS[$((choice-1))]}"
fi

echo -e "${GREEN}Selected: $SELECTED_ZIP${NO_COLOR}"

# --- CONFIGURATION ---
pnetlab=$SELECTED_ZIP
pnetlab_docker=pnetlab-docker_4.0.0-30_amd64.deb
pnetlab_wireshark=pnetlab-wireshark_4.0.0-30_amd64.deb
pnetlab_qemu=pnetlab-qemu_4.0.0-30_amd64.deb
pnetlab_guacamole=pnetlab-guacamole_4.0.0-6_amd64.deb

URL_pnetlab_wireshark=$MIRROR_URL/bionic/$pnetlab_wireshark
URL_PNET_PNETLAB=$MIRROR_URL/testing/$pnetlab
URL_DOCKER=$MIRROR_URL/testing/docker-ce.zip
URL_pnetlab_Docker=$MIRROR_URL/bionic/$pnetlab_docker
URL_pnetlab_qemu=$MIRROR_URL/bionic/$pnetlab_qemu
URL_pnetlab_guacamole=$MIRROR_URL/bionic/$pnetlab_guacamole
URL_pnetlab_TPM=$MIRROR_URL/testing/swtpm-bionic.zip

lsb_release -r -s | grep -q 18.04
if [ $? -ne 0 ]; then
    echo -e "${RED}Upgrade has been rejected. You need to have UBUNTU 18.04 to use this script${NO_COLOR}"
    exit 0
fi

# On Azure attach data disk
azure_disk_tune() {
    ls -l /dev/disk/by-id/ | grep -q sdc && (
        echo o 
        echo n 
        echo p 
        echo 1 
        echo   
        echo   
        echo w 
    ) | sudo fdisk /dev/sdc && (
        mke2fs -F /dev/sdc1
        echo "/dev/sdc1 /opt    ext4    defaults,discard 0 0 " >>/etc/fstab
        mount /opt
    )
}

uname -a | grep -q -- "-azure " && azure_disk_tune

sudo apt-get update
sudo apt-get install -y ovmf
echo -e "${GREEN}backup labs from /opt/unetlab/labs/ to /root ${NO_COLOR}"
tar -czvf /root/labs-$(date +%Y%m%d%H%M).tgz /opt/unetlab/labs/
rm -rf /tmp/* &>/dev/null
cd /tmp 2>&1 >/dev/null

# Install QEMU
dpkg-query -l | grep pnetlab-qemu | grep 4.0.0-30 -q
if [ $? -ne 0 ]; then
    echo -e "${GREEN}Download New Qemu Version ${NO_COLOR}"
    wget --content-disposition -q --show-progress $URL_pnetlab_qemu
    dpkg -i $pnetlab_qemu
fi

# Install Guacamole
dpkg-query -l | grep pnetlab-guacamole | grep 4.0.0-6 -q
if [ $? -ne 0 ]; then
    echo -e "${GREEN}Download New HTML CONSOLE GUACAOLE Version ${NO_COLOR}"
    wget --content-disposition -q --show-progress $URL_pnetlab_guacamole
    dpkg -i $pnetlab_guacamole
    service tomcat8 restart &>/dev/null
    service guacd restart &>/dev/null
fi

# Install Docker
dpkg-query -l | grep docker-ce -q
if [ $? -ne 0 ]; then
    echo -e "${GREEN} Download New Docker Packages ${NO_COLOR}"
    wget --content-disposition -q --show-progress $URL_pnetlab_Docker
    dpkg -i $pnetlab_docker
    wget --content-disposition -q --show-progress $URL_DOCKER
    unzip -o docker-ce.zip -d /tmp &>/dev/null
    dpkg -i --force-all /tmp/dockers/*.deb &>/dev/null
    systemctl unmask docker.socket &>/dev/null
    systemctl unmask docker.service &>/dev/null
    apt-get remove -y -q docker.io containerd runc
    service docker restart &>/dev/null
fi

# Install Wireshark
dpkg-query -l | grep pnetlab-wireshark | grep 4.0.0-30 -q
if [ $? -ne 0 ]; then
    wget --content-disposition -q --show-progress $URL_pnetlab_wireshark
    dpkg -i $pnetlab_wireshark
fi

# Install TPM
dpkg-query -l | grep swtpm -q
if [ $? -ne 0 ]; then
    wget --content-disposition -q --show-progress $URL_pnetlab_TPM
    unzip -o swtpm-bionic.zip -d /tmp &>/dev/null
    dpkg -i /tmp/swtpm/*.deb &>/dev/null
fi

# Perform PNetLab Upgrade
echo -e "${GREEN}Downloading and applying PNetLab Version $pnetlab ${NO_COLOR}"
wget --content-disposition -q --show-progress $URL_PNET_PNETLAB
unzip -o $pnetlab -d ./upgrade >/dev/null 2>&1
chmod 755 -R upgrade
find upgrade -type f -print0 | xargs -0 dos2unix 2>&1 >/dev/null
./upgrade/upgrade

apt autoremove -y -q
apt autoclean -y -q
echo -e "${GREEN}Upgrade to $pnetlab complete!${NO_COLOR}"
