#!/bin/bash
set -e

REPO_URL="https://download.managemate.fr/mslm"

exec </dev/tty

if ! command -v whiptail &> /dev/null; then
    apt-get install -y whiptail > /dev/null 2>&1
fi

if [ "$EUID" -ne 0 ]; then
    whiptail --title "Error" --msgbox "This script must be run as root" 8 40
    exit 1
fi

whiptail --title "MSLM Installer" --msgbox "Welcome to MSLM Installer

ManageMate Server License Manager" 10 50

if command -v mslm &> /dev/null; then
    if ! whiptail --title "MSLM Installer" --yesno "MSLM is already installed.

Reinstall?" 10 50; then
        exit 0
    fi
fi

PORT=$(whiptail --title "MSLM Configuration" --inputbox "Server port:" 10 50 "8090" 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then exit 1; fi

ADMIN_USER=$(whiptail --title "MSLM Configuration" --inputbox "Admin username:" 10 50 "admin" 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then exit 1; fi

ADMIN_PASS=$(whiptail --title "MSLM Configuration" --passwordbox "Admin password:" 10 50 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then exit 1; fi

if [ -z "$ADMIN_PASS" ]; then
    whiptail --title "Error" --msgbox "Password cannot be empty" 8 40
    exit 1
fi

ORG_NAME=$(whiptail --title "MSLM Configuration" --inputbox "Organization name (optional):" 10 50 "" 3>&1 1>&2 2>&3)

{
    echo 10
    echo "XXX"
    echo "Adding repository..."
    echo "XXX"
    rm -f /etc/apt/sources.list.d/mslm.list /etc/apt/sources.list.d/managemate*.list
    echo "deb [trusted=yes] ${REPO_URL} stable main" > /etc/apt/sources.list.d/mslm.list
    sleep 1

    echo 30
    echo "XXX"
    echo "Updating package list..."
    echo "XXX"
    apt-get update -qq 2>/dev/null

    echo 50
    echo "XXX"
    echo "Installing MSLM..."
    echo "XXX"
    apt-get install -y mslm > /dev/null 2>&1

    echo 70
    echo "XXX"
    echo "Configuring MSLM..."
    echo "XXX"
    
    mkdir -p /var/lib/mslm/.mslm
    
    # Config sans mot de passe (stocke dans la DB)
    cat > /var/lib/mslm/.mslm/config.json << EOFCONFIG
{
  "server_port": ${PORT},
  "server_host": "0.0.0.0",
  "master_license_key": "",
  "database_path": "/var/lib/mslm/mslm.db",
  "organization_name": "${ORG_NAME}"
}
EOFCONFIG

    if ! id -u mslm &>/dev/null; then
        useradd --system --no-create-home --shell /usr/sbin/nologin mslm
    fi
    
    chown -R mslm:mslm /var/lib/mslm
    chmod 750 /var/lib/mslm/.mslm
    chmod 600 /var/lib/mslm/.mslm/config.json
    
    echo 85
    echo "XXX"
    echo "Creating admin user..."
    echo "XXX"
    # Creer l'admin via CLI (mot de passe hashe dans la DB)
    mslm-cli user create "${ADMIN_USER}" --password "${ADMIN_PASS}" --role admin > /dev/null 2>&1 || true
    
    echo 95
    echo "XXX"
    echo "Starting service..."
    echo "XXX"
    systemctl daemon-reload
    systemctl enable mslm.service > /dev/null 2>&1
    systemctl restart mslm.service
    sleep 2

    echo 100
} | whiptail --title "MSLM Installer" --gauge "Installing..." 8 60 0

SERVER_IP=$(ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0.0.1 | head -1)

if systemctl is-active --quiet mslm.service; then
    whiptail --title "MSLM Installer" --msgbox "Installation complete!

Web UI: http://${SERVER_IP}:${PORT}
Username: ${ADMIN_USER}

Commands:
  systemctl status mslm
  journalctl -u mslm -f" 14 50
else
    whiptail --title "MSLM Installer" --msgbox "Installation complete but service failed.

Check: journalctl -u mslm -n 50" 10 50
fi
