#!/bin/bash

# TSID Client Installation Script for Ubuntu/Debian

set -e

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

# Functions
print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

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

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

# Detect Ubuntu/Debian version
detect_ubuntu_version() {
    if [ -f /etc/lsb-release ]; then
        . /etc/lsb-release
        print_status "Detected $DISTRIB_ID $DISTRIB_RELEASE"
        echo "$DISTRIB_ID:$DISTRIB_RELEASE"
    elif [ -f /etc/debian_version ]; then
        local DEBIAN_VERSION=$(cat /etc/debian_version)
        print_status "Detected Debian $DEBIAN_VERSION"
        echo "Debian:$DEBIAN_VERSION"
    else
        print_error "Ubuntu/Debian not detected"
        exit 1
    fi
}

# Install for Ubuntu/Debian
install_ubuntu_debian() {
    print_status "Installing TSID Client for Ubuntu/Debian..."
    
    # 1. Update package lists
    print_status "Updating package lists..."
    apt-get update
    
    # 2. Install dependencies
    print_status "Installing dependencies..."
    apt-get install -y curl wget gnupg2 software-properties-common
    
    # 3. Add GPG key
    print_status "Adding TSID GPG key..."
    curl -fsSL https://tsid-pcguard.t5online.com/tsid-repo.gpg.key | gpg --dearmor -o /usr/share/keyrings/tsid-repo.gpg
    
    # 4. Add repository
    print_status "Adding TSID repository..."
    echo "deb [signed-by=/usr/share/keyrings/tsid-repo.gpg] https://tsid-pcguard.t5online.com stable main" | tee /etc/apt/sources.list.d/tsid.list
    
    # 5. Update package lists again
    print_status "Updating package lists with new repository..."
    apt-get update
    
    # 6. Install tsid-client
    print_status "Installing tsid-client..."
    if apt-get install -y tsid-client; then
        print_status "TSID Client installed successfully!"
    else
        print_error "Installation failed"
        exit 1
    fi
    
    # 7. Configure TSID (run tsid-setup)
    print_status "Configuring TSID client..."
    if /usr/bin/tsid-setup; then
        print_status "TSID configuration completed!"
    else
        print_error "TSID configuration failed"
        exit 1
    fi
}

# Verify installation
verify_installation() {
    print_status "Verifying installation..."
    
    if dpkg -l | grep -q tsid-client; then
        local INSTALLED_VERSION=$(dpkg -l | grep tsid-client | awk '{print $3}')
        print_status "TSID Client installed: $INSTALLED_VERSION"
        
        # Check if services are available
        if systemctl list-unit-files | grep -q tsid; then
            print_status "TSID services are available"
        else
            print_warning "TSID services not found in systemd"
        fi
        
        return 0
    else
        print_error "TSID Client not found in dpkg database"
        return 1
    fi
}

# Main installation logic
main() {
    print_status "TSID Client Installation Script for Ubuntu/Debian"
    print_status "==============================================="
    
    # Check if running as root
    if [ "$EUID" -ne 0 ]; then
        print_error "Please run as root or with sudo"
        exit 1
    fi
    
    # Detect version
    UBUNTU_VERSION=$(detect_ubuntu_version)
    
    # Install
    install_ubuntu_debian
    
    # Verify installation
    if verify_installation; then
        print_status "Installation and configuration completed successfully!"
        print_status ""
        print_status "Next steps:"
        print_status "1. Check service status: systemctl status tsid-client"
        print_status "2. Enable service: systemctl enable tsid-client"
    else
        print_error "Installation verification failed"
        exit 1
    fi
}

# Run main function
main "$@"
