#!/bin/bash
#
# Service configuration (NFS, SMB, ClamAV)
#

configure_nfs() {
    log_info "Configuring NFS server..."
    
    # Create NFS exports directory if it doesn't exist
    mkdir -p /etc/exports.d
    
    # Create base exports file (empty, will be managed by Calypso)
    if [[ ! -f /etc/exports.d/calypso.exports ]]; then
        touch /etc/exports.d/calypso.exports
        echo "# Calypso managed NFS exports" > /etc/exports.d/calypso.exports
        echo "# This file is managed by Calypso. Manual edits may be overwritten." >> /etc/exports.d/calypso.exports
    fi
    
    # Include calypso exports in main exports file
    if ! grep -q "calypso.exports" /etc/exports 2>/dev/null; then
        echo "" >> /etc/exports
        echo "# Include Calypso managed exports" >> /etc/exports
        echo "/etc/exports.d/calypso.exports" >> /etc/exports || true
    fi
    
    log_info "✓ NFS configured"
}

configure_samba() {
    log_info "Configuring Samba..."
    
    # Backup original smb.conf if it exists and hasn't been backed up
    if [[ -f /etc/samba/smb.conf ]] && [[ ! -f /etc/samba/smb.conf.calypso-backup ]]; then
        cp /etc/samba/smb.conf /etc/samba/smb.conf.calypso-backup
        log_info "Backed up original smb.conf"
    fi
    
    # Create Calypso Samba configuration directory
    mkdir -p "$CONFIG_DIR/samba"
    
    # Create base Samba config (minimal, will be extended by Calypso)
    if [[ ! -f "$CONFIG_DIR/samba/smb.conf.calypso" ]]; then
        cat > "$CONFIG_DIR/samba/smb.conf.calypso" <<EOF
# Calypso managed Samba configuration
# This file is included in main smb.conf
# Manual edits may be overwritten by Calypso

[global]
    workgroup = WORKGROUP
    server string = Calypso File Server
    security = user
    map to guest = Bad User
    dns proxy = no
    
    # Logging
    log file = /var/log/samba/log.%m
    max log size = 1000
    syslog = 0
    
    # Performance
    socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072
    read raw = yes
    write raw = yes
    max xmit = 65536
    dead time = 15
    getwd cache = yes

# Calypso shares will be added here dynamically
EOF
        log_info "Created Calypso Samba configuration"
    fi
    
    # Include Calypso config in main smb.conf if not already included
    if ! grep -q "smb.conf.calypso" /etc/samba/smb.conf 2>/dev/null; then
        echo "" >> /etc/samba/smb.conf
        echo "# Include Calypso managed shares" >> /etc/samba/smb.conf
        echo "include = $CONFIG_DIR/samba/smb.conf.calypso" >> /etc/samba/smb.conf
    fi
    
    # Test Samba configuration
    if command_exists testparm; then
        if testparm -s &>/dev/null; then
            log_info "✓ Samba configuration valid"
        else
            log_warn "Samba configuration test failed, but continuing..."
        fi
    fi
    
    log_info "✓ Samba configured"
}

configure_clamav() {
    log_info "Configuring ClamAV..."
    
    # Create ClamAV configuration directory
    mkdir -p "$CONFIG_DIR/clamav"
    
    # Configure ClamAV daemon
    if [[ -f /etc/clamav/clamd.conf ]]; then
        # Backup original
        if [[ ! -f /etc/clamav/clamd.conf.calypso-backup ]]; then
            cp /etc/clamav/clamd.conf /etc/clamav/clamd.conf.calypso-backup
        fi
        
        # Update configuration for Calypso
        sed -i 's|^#LocalSocket|LocalSocket|' /etc/clamav/clamd.conf || true
        sed -i 's|^LocalSocket.*|LocalSocket /var/run/clamav/clamd.ctl|' /etc/clamav/clamd.conf || true
        
        # Set quarantine directory
        if ! grep -q "QuarantineDir" /etc/clamav/clamd.conf; then
            echo "QuarantineDir $DATA_DIR/quarantine" >> /etc/clamav/clamd.conf
        fi
    fi
    
    # Configure freshclam
    if [[ -f /etc/clamav/freshclam.conf ]]; then
        # Backup original
        if [[ ! -f /etc/clamav/freshclam.conf.calypso-backup ]]; then
            cp /etc/clamav/freshclam.conf /etc/clamav/freshclam.conf.calypso-backup
        fi
        
        # Enable automatic updates
        sed -i 's|^#Checks|Checks|' /etc/clamav/freshclam.conf || true
    fi
    
    # Create quarantine directory
    mkdir -p "$DATA_DIR/quarantine"
    chown clamav:clamav "$DATA_DIR/quarantine" 2>/dev/null || chown root:root "$DATA_DIR/quarantine"
    chmod 755 "$DATA_DIR/quarantine"
    
    log_info "✓ ClamAV configured"
}

configure_all_services() {
    log_info "Configuring file sharing and antivirus services..."
    
    configure_nfs
    configure_samba
    configure_clamav
    
    log_info "✓ All services configured"
}

