#!/bin/bash

if [ "$EUID" -ne 0 ]; then
    echo "Please run this script as root."
    exit 1
fi

# Verify we are in the correct unpacked package directory
if [ ! -d "opt/AstroDMx-Capture" ] || [ ! -d "etc/udev/rules.d" ] || [ ! -d "usr/share/applications" ]; then
    echo "Error: This script must be run from the root of the unpacked AstroDMx Capture package."
    echo "Missing required directories: opt/, etc/, or usr/"
    exit 1
fi

clear
printf "This script will install AstroDMx Capture\n"
printf "This script should be run as root\n"
printf "Don't run this script if you have installed via RPM or DEB\n"
read -p "Press enter to continue"


# Ensure required directories exist
mkdir -p /etc/xdg/menus/applications-merged/
mkdir -p /usr/share/astrodmx_capture/
cp etc/xdg/menus/applications-merged/astrodmx.menu /etc/xdg/menus/applications-merged/
cp usr/share/applications/astrodmx_capture.desktop /usr/share/applications/
cp usr/share/astrodmx_capture/astrodmx.png /usr/share/astrodmx_capture/
cp usr/share/desktop-directories/astrodmx.directory /usr/share/desktop-directories/
cp etc/udev/rules.d/* /etc/udev/rules.d/

# Define installation path
INSTALL_DIR="/opt/AstroDMx-Capture"

printf "Removing any previous installation at %s\n" "$INSTALL_DIR"
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"

# Copy new files
cp -r opt/AstroDMx-Capture/* "$INSTALL_DIR"

# Reload udev rules
udevadm control --reload-rules

# Detect system glibc version
SYSTEM_GLIBC=$(ldd --version | head -n 1 | awk '{print $NF}')
printf "Detected system glibc version: $SYSTEM_GLIBC\n"

# Define threshold glibc version
THRESHOLD_GLIBC="2.40"

# Paths to bundled libraries
BUNDLED_LIBSTDC="$INSTALL_DIR/lib/libstdc++.so.6"
BUNDLED_LIBGCC="$INSTALL_DIR/lib/libgcc_s.so.1"
BUNDLED_LIBZ="$INSTALL_DIR/lib/libz.so.1"
BUNDLED_LIBPNG="$INSTALL_DIR/lib/libpng16.so.16"
BUNDLED_LIBATOMIC="$INSTALL_DIR/lib/libatomic.so.1"

remove_if_exists() {
    if [ -f "$1" ]; then
        rm -f "$1"
        printf "Removed %s\n" "$1"
    else
        printf "No bundled %s found. Nothing to remove.\n" "$1"
    fi
}

# Check if system provides libatomic.so.1
SYSTEM_LIBATOMIC=$(ldconfig -p | grep libatomic.so.1 | awk '{print $NF}' | head -n 1)

# Remove unnecessary libraries if system glibc is modern
if [ "$(printf '%s\n' "$THRESHOLD_GLIBC" "$SYSTEM_GLIBC" | sort -V | tail -1)" = "$SYSTEM_GLIBC" ]; then
    printf "System glibc is new enough. Removing bundled libraries.\n"

    remove_if_exists "$BUNDLED_LIBSTDC"
    remove_if_exists "$BUNDLED_LIBGCC"
    remove_if_exists "$BUNDLED_LIBZ"
    remove_if_exists "$BUNDLED_LIBPNG"

    if [ -n "$SYSTEM_LIBATOMIC" ]; then
        printf "System provides libatomic.so.1. Removing bundled version.\n"
        remove_if_exists "$BUNDLED_LIBATOMIC"
    else
        printf "System does NOT provide libatomic.so.1. Keeping bundled version.\n"
    fi
else
    printf "System glibc is too old. Keeping bundled libraries.\n"
fi

printf "\n\nAstroDMx Capture is now installed. See the readme for more information.\n"
printf "If AstroDMx Capture does not show in the menus then logout and log back in.\n\n"

