#!/bin/bash
#
# Component installation (ZFS, SCST, MHVTL, Bacula)
#

install_zfs() {
    if command_exists zpool && command_exists zfs; then
        log_info "ZFS already installed"
        return 0
    fi
    
    log_info "Installing ZFS..."
    
    # Check if ZFS module is loaded
    if lsmod | grep -q zfs; then
        log_info "ZFS kernel module already loaded"
        return 0
    fi
    
    # Install ZFS
    apt-get install -y \
        zfsutils-linux \
        zfs-dkms || {
        log_warn "ZFS installation failed. You may need to install manually."
        return 1
    }
    
    # Load ZFS module
    modprobe zfs || true
    
    log_info "✓ ZFS installed"
    return 0
}

install_scst() {
    if [[ -f /etc/scst.conf ]] || lsmod | grep -q scst; then
        log_info "SCST appears to be installed"
        return 0
    fi
    
    log_info "Installing SCST..."
    log_warn "SCST requires building from source. This may take a while..."
    
    # Check if SCST source is available
    if [[ -d "$PROJECT_ROOT/src/scst" ]] || [[ -d "/usr/src/scst" ]]; then
        log_info "SCST source found, building..."
        # SCST build would go here
        # This is a placeholder - actual SCST installation is complex
        log_warn "SCST installation requires manual steps. See documentation."
        return 1
    else
        log_warn "SCST source not found. Please install SCST manually."
        log_info "See: docs/on-progress/scst-installation.md"
        return 1
    fi
}

install_mhvtl() {
    if command_exists vtlcmd || systemctl is-active --quiet mhvtl 2>/dev/null; then
        log_info "MHVTL already installed"
        return 0
    fi
    
    log_info "Installing MHVTL..."
    
    # Install MHVTL from package or source
    if apt-cache show mhvtl &>/dev/null; then
        apt-get install -y mhvtl mhvtl-utils || {
            log_warn "MHVTL package installation failed"
            return 1
        }
    else
        log_warn "MHVTL package not available. Building from source..."
        # MHVTL build from source would go here
        log_warn "MHVTL installation requires manual steps. See documentation."
        return 1
    fi
    
    # Enable and start MHVTL service
    systemctl enable mhvtl || true
    systemctl start mhvtl || true
    
    log_info "✓ MHVTL installed"
    return 0
}

install_bacula() {
    log_info "Installing Bacula (optional)..."
    
    # Check if Bacula is already installed
    if command_exists bconsole || systemctl is-active --quiet bacula-sd 2>/dev/null; then
        log_info "Bacula already installed"
        return 0
    fi
    
    # Install Bacula packages
    apt-get install -y \
        bacula-common \
        bacula-sd \
        bacula-client \
        bacula-console || {
        log_warn "Bacula installation failed or packages not available"
        log_info "Bacula can be installed separately if needed"
        return 1
    }
    
    log_info "✓ Bacula installed (configuration required separately)"
    return 0
}

