#!/bin/bash

echo "======================================"
echo "Cavisson Server Installation Started"
echo "======================================"

############################################
# Check Root User
############################################

if [ "$EUID" -ne 0 ]; then
  echo "Please run this script as root or using sudo"
  exit 1
fi

############################################
# Ask for data directory
############################################

read -p "Enter the data directory (example: /data): " DATA_DIR

if [ ! -d "$DATA_DIR" ]; then
  echo "Directory $DATA_DIR does not exist. Exiting..."
  exit 1
fi

echo "Data directory selected: $DATA_DIR"

############################################
# Create cavisson user
############################################
if id cavisson &>/dev/null; then
    echo "User cavisson already exists"
else
    useradd -m -s /bin/bash cavisson

    echo "Set password for cavisson user"

    while true; do
        read -s -p "Enter password: " PASS1
        echo
        read -s -p "Confirm password: " PASS2
        echo

        if [ "$PASS1" = "$PASS2" ]; then
            echo "cavisson:$PASS1" | sudo chpasswd
            echo "Password set successfully"
            break
        else
            echo "Passwords do not match. Try again."
        fi
    done
fi
############################################
# Move cavisson home to data directory
############################################

echo "Moving cavisson directory to $DATA_DIR"

if [ -d "/home/cavisson" ]; then
    mv /home/cavisson "$DATA_DIR/" 2>/dev/null

    if [ $? -ne 0 ]; then
        echo "ERROR: Failed to move /home/cavisson to $DATA_DIR"
        exit 1
    else
        echo "Directory moved successfully"
    fi
fi

############################################
# Remove old directory and create symlink
############################################

rm -rf /home/cavisson 2>/dev/null

ln -s "$DATA_DIR/cavisson" /home/cavisson

if [ $? -ne 0 ]; then
    echo "ERROR: Failed to create symbolic link"
    exit 1
else
    echo "Symlink created successfully"
fi

chown -R cavisson:cavisson "$DATA_DIR/cavisson"

echo "/home/cavisson is now linked to $DATA_DIR/cavisson"

############################################
# Go to cavisson home
############################################

cd /home/cavisson || exit

############################################
# Create cavisson_libs
############################################

mkdir -p cavisson_libs
cd cavisson_libs || exit

############################################
# Download Required Files
############################################

echo "Downloading Cavisson installation files..."

wget https://nde.cav-test.com/HUB/RHEL9/ServerInstaller.sh
wget https://nde.cav-test.com/HUB/RHEL9/cav_licence
wget https://nde.cav-test.com/HUB/RHEL9/Cav.4.15.0.67.Redhat94_64.bin

############################################
# Give Permissions
############################################

chmod +x cav_licence
chmod +x ServerInstaller.sh
chmod +x Cav.4.15.0.67.Redhat94_64.bin

############################################
# Run Installer
############################################

echo "Starting Cavisson installer..."

bash ServerInstaller.sh --offline-mode --no-reboot

if [ $? -ne 0 ]; then
    echo "ERROR: ServerInstaller failed"
    exit 1
else
    echo "ServerInstaller executed successfully"
fi

############################################
# Check Tomcat Process
############################################

echo "Checking Tomcat process..."

if ps -ef | grep -v grep | grep -q "/home/cavisson/work/webapps/.tomcat/nsi_check_tomcat"
then
    echo "Tomcat process contains nsi_check_tomcat ✔"
else
    echo "Tomcat process does NOT contain nsi_check_tomcat ✘"
fi

############################################
# Download Unified Dashboard Page
############################################
echo "Downloading Unified Dashboard page..."

wget https://127.0.0.1:7899/UnifiedDashboard/#/index.html --no-check-certificate

if [ $? -eq 0 ]
then
    echo "Unified Dashboard page downloaded successfully ✔"
else
    echo "Dashboard download failed ✘"
    exit 1
fi

############################################
# Move to Upgrade Directory
############################################

echo "Moving to upgrade directory..."

cd /home/cavisson/work/upgrade || { echo "Upgrade directory not found"; exit 1; }

echo "Current directory: $(pwd)"

############################################
# Download RBU
############################################
echo "Downloading RBU..."

if wget https://nde.cav-test.com/HUB/RHEL9/rbu_setup.4.15.0.67_Redhat94_64.bin
then
    echo "RBU is downloaded"
    echo "Running RBU installer..."

    if bash rbu_setup.4.15.0.67_Redhat94_64.bin
    then
        echo "RBU installed successfully"
    else
        echo "RBU installation failed"
    fi

else
    echo "RBU download failed"
fi
############################################
# Download Appium
############################################

echo "Downloading Appium..."

wget https://nde.cav-test.com/HUB/RHEL9/Appium_appimage_4.15.0.67_Redhat94_64.bin

echo "Running Appium installer..."

bash Appium_appimage_4.15.0.67_Redhat94_64.bin

echo "======================================"
echo "Cavisson Installation and Upgrade Completed"
echo "======================================"

############################################
# Reboot System
############################################

echo "======================================"
echo "Cavisson Installation Completed"
echo "System will reboot in 10 seconds..."
echo "======================================"

sleep 10

sudo reboot