#!/bin/bash
#
# System dependencies installation
#

install_system_dependencies() {
    log_info "Installing system dependencies..."
    
    # Update package lists
    log_info "Updating package lists..."
    apt-get update -qq
    
    # Install base tools
    log_info "Installing base build tools..."
    apt-get install -y \
        build-essential \
        curl \
        wget \
        git \
        ca-certificates \
        gnupg \
        lsb-release \
        jq \
        uuid-runtime \
        net-tools \
        iproute2 \
        systemd \
        chrony \
        ufw \
        sudo
    
    # Install Go
    install_go
    
    # Install Node.js
    install_nodejs
    
    # Install PostgreSQL
    install_postgresql
    
    # Install storage tools
    install_storage_tools
    
    # Install tape tools
    install_tape_tools
    
    # Install SCST prerequisites
    install_scst_prerequisites
    
    # Install file sharing services
    install_file_sharing_services
    
    # Install antivirus
    install_antivirus
    
    log_info "✓ System dependencies installed"
}

install_go() {
    if command_exists go; then
        local installed_ver=$(get_installed_version go)
        log_info "Go already installed: $installed_ver"
        return 0
    fi
    
    log_info "Installing Go 1.22..."
    local GO_VERSION="1.22.0"
    local GO_ARCH="linux-amd64"
    
    cd /tmp
    wget -q "https://go.dev/dl/go${GO_VERSION}.${GO_ARCH}.tar.gz"
    rm -rf /usr/local/go
    tar -C /usr/local -xzf "go${GO_VERSION}.${GO_ARCH}.tar.gz"
    rm "go${GO_VERSION}.${GO_ARCH}.tar.gz"
    
    # Add to PATH
    if ! grep -q "/usr/local/go/bin" /etc/profile; then
        echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile
    fi
    export PATH=$PATH:/usr/local/go/bin
    
    log_info "✓ Go installed"
}

install_nodejs() {
    if command_exists node; then
        local installed_ver=$(get_installed_version node)
        log_info "Node.js already installed: $installed_ver"
        return 0
    fi
    
    log_info "Installing Node.js 20.x LTS..."
    curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
    apt-get install -y nodejs
    
    # Install pnpm
    if ! command_exists pnpm; then
        npm install -g pnpm
    fi
    
    log_info "✓ Node.js and pnpm installed"
}

install_postgresql() {
    if command_exists psql; then
        local installed_ver=$(get_installed_version psql)
        log_info "PostgreSQL already installed: $installed_ver"
        systemctl start postgresql || true
        return 0
    fi
    
    log_info "Installing PostgreSQL..."
    apt-get install -y \
        postgresql \
        postgresql-contrib \
        libpq-dev
    
    systemctl enable postgresql
    systemctl start postgresql
    
    wait_for_service postgresql
    
    log_info "✓ PostgreSQL installed and started"
}

install_storage_tools() {
    log_info "Installing storage tools..."
    apt-get install -y \
        lvm2 \
        xfsprogs \
        thin-provisioning-tools \
        smartmontools \
        nvme-cli \
        parted \
        gdisk
    
    log_info "✓ Storage tools installed"
}

install_tape_tools() {
    log_info "Installing tape tools..."
    apt-get install -y \
        lsscsi \
        sg3-utils \
        mt-st \
        mtx
    
    log_info "✓ Tape tools installed"
}

install_scst_prerequisites() {
    log_info "Installing SCST prerequisites..."
    apt-get install -y \
        dkms \
        linux-headers-$(uname -r) \
        build-essential
    
    log_info "✓ SCST prerequisites installed"
}

install_file_sharing_services() {
    log_info "Installing file sharing services (NFS and SMB)..."
    
    # Install NFS server
    if ! systemctl is-active --quiet nfs-server 2>/dev/null; then
        log_info "Installing NFS server..."
        apt-get install -y \
            nfs-kernel-server \
            nfs-common
        
        # Enable NFS services
        systemctl enable nfs-server || true
        systemctl enable rpcbind || true
        
        log_info "✓ NFS server installed"
    else
        log_info "NFS server already installed"
    fi
    
    # Install Samba (SMB/CIFS)
    if ! systemctl is-active --quiet smbd 2>/dev/null; then
        log_info "Installing Samba (SMB/CIFS)..."
        apt-get install -y \
            samba \
            samba-common-bin
        
        # Enable Samba services
        systemctl enable smbd || true
        systemctl enable nmbd || true
        
        log_info "✓ Samba installed"
    else
        log_info "Samba already installed"
    fi
}

install_antivirus() {
    log_info "Installing ClamAV antivirus..."
    
    if ! command_exists clamscan; then
        apt-get install -y \
            clamav \
            clamav-daemon \
            clamav-freshclam \
            clamav-unofficial-sigs || {
            log_warn "ClamAV installation failed"
            return 1
        }
        
        # Update virus definitions
        log_info "Updating ClamAV virus definitions (this may take a while)..."
        freshclam || log_warn "Virus definition update failed, will update on first service start"
        
        # Enable ClamAV daemon
        systemctl enable clamav-daemon || true
        systemctl enable clamav-freshclam || true
        
        log_info "✓ ClamAV installed"
    else
        log_info "ClamAV already installed"
    fi
}

