#!/bin/bash

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to print status messages
print_status() {
    echo -e "${GREEN}[*] $1${NC}"
}

print_error() {
    echo -e "${RED}[!] $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}[!] $1${NC}"
}

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    print_error "Please run this script as root (use sudo)"
    exit 1
fi

# Detect distribution
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$NAME
    VER=$VERSION_ID
else
    print_error "Could not detect distribution"
    exit 1
fi

print_status "Detected OS: $OS $VER"

# Define packages based on distribution
case $OS in
    "Ubuntu"|"Debian GNU/Linux")
        # Common packages for both Ubuntu and Debian
        PACKAGES="cmake make g++ devscripts debhelper"
        ;;
    *)
        print_error "Unsupported distribution: $OS"
        exit 1
        ;;
esac

# Function to check if a package is installed
is_package_installed() {
    dpkg -l "$1" 2>/dev/null | grep -q "^ii"
}

# Update package lists
print_status "Updating package lists..."
apt-get update

# Install missing packages
print_status "Checking and installing required packages..."
for pkg in $PACKAGES; do
    if ! is_package_installed "$pkg"; then
        print_status "Installing $pkg..."
        apt-get install -y "$pkg"
        if [ $? -ne 0 ]; then
            print_error "Failed to install $pkg"
            exit 1
        fi
    else
        print_status "$pkg is already installed"
    fi
done

# Verify all required tools are installed
print_status "Verifying installation..."
for tool in cmake make g++; do
    if ! command -v "$tool" &> /dev/null; then
        print_error "$tool is not installed properly"
        exit 1
    fi
done



# Install other required packages
apt install -y musl-tools wget tar

# Set install directory
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
    USER_HOME=$(eval echo ~$SUDO_USER)
else
    USER_HOME="$HOME"
fi
INSTALL_DIR="$USER_HOME/.musl-cross"
mkdir -p "$INSTALL_DIR"

MUSL_CC_URL="https://musl.cc"
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT

# x86_64
if [ ! -d "$INSTALL_DIR/x86_64-linux-musl-cross" ]; then
    echo "Downloading x86_64 musl cross toolchain..."
    wget -nc -O "$TMPDIR/x86_64-linux-musl-cross.tgz" $MUSL_CC_URL/x86_64-linux-musl-cross.tgz
    tar -C "$INSTALL_DIR" -xvf "$TMPDIR/x86_64-linux-musl-cross.tgz"
fi

# aarch64
if [ ! -d "$INSTALL_DIR/aarch64-linux-musl-cross" ]; then
    echo "Downloading aarch64 musl cross toolchain..."
    wget -nc -O "$TMPDIR/aarch64-linux-musl-cross.tgz" $MUSL_CC_URL/aarch64-linux-musl-cross.tgz
    tar -C "$INSTALL_DIR" -xvf "$TMPDIR/aarch64-linux-musl-cross.tgz"
fi

# Print instructions for adding to PATH
# cat <<EOF

# To use the musl cross compilers, add the following to your shell:
# export PATH="$INSTALL_DIR/x86_64-linux-musl-cross/bin:$INSTALL_DIR/aarch64-linux-musl-cross/bin:$PATH"

# Or run:
#     export PATH="$INSTALL_DIR/x86_64-linux-musl-cross/bin:$INSTALL_DIR/aarch64-linux-musl-cross/bin:\$PATH"

# EOF

# Clean up
rm -rf "$TMPDIR"

# If run with sudo, add to invoking user's ~/.bashrc
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
    BASHRC="$USER_HOME/.bashrc"
    EXPORT_LINE="export PATH=\"$INSTALL_DIR/x86_64-linux-musl-cross/bin:$INSTALL_DIR/aarch64-linux-musl-cross/bin:\$PATH\""
    if ! grep -Fxq "$EXPORT_LINE" "$BASHRC"; then
        echo "" >> "$BASHRC"
        echo "# Add musl cross compilers to PATH for bb64" >> "$BASHRC"
        echo "$EXPORT_LINE" >> "$BASHRC"
        echo "Added musl cross compilers to $BASHRC"
    else
        echo "musl cross compiler PATH already present in $BASHRC"
    fi
fi





print_status "All dependencies installed successfully!"
print_status "You can now run ./build.sh to build the project"