#!/bin/bash
#
# Filesystem structure setup
#

create_filesystem_structure() {
    log_info "Creating filesystem structure..."
    
    # Binary directory structure
    log_info "Creating binary directory structure..."
    mkdir -p "$INSTALL_PREFIX/releases/$CALYPSO_VERSION"/{bin,web,migrations,scripts}
    mkdir -p "$INSTALL_PREFIX/third_party"
    
    # Create symlink for current version
    if [[ -L "$INSTALL_PREFIX/current" ]]; then
        rm "$INSTALL_PREFIX/current"
    fi
    ln -sf "releases/$CALYPSO_VERSION" "$INSTALL_PREFIX/current"
    
    # Configuration directory structure (per architecture spec)
    log_info "Creating configuration directory structure..."
    mkdir -p "$CONFIG_DIR"/{tls,integrations,system,scst,nfs,samba,clamav}
    chmod 755 "$CONFIG_DIR"
    chmod 700 "$CONFIG_DIR/tls" 2>/dev/null || true
    
    # Data directory structure (per architecture spec: /srv/calypso/)
    log_info "Creating data directory structure..."
    mkdir -p "$DATA_DIR"/{db,backups,object,shares,vtl,iscsi,uploads,cache,_system,quarantine}
    chown -R calypso:calypso "$DATA_DIR" 2>/dev/null || chown -R root:root "$DATA_DIR"
    chmod 755 "$DATA_DIR"
    
    # Create quarantine directory for ClamAV
    mkdir -p "$DATA_DIR/quarantine"
    chmod 700 "$DATA_DIR/quarantine"
    
    # Log directory
    log_info "Creating log directory..."
    mkdir -p "$LOG_DIR"
    chmod 755 "$LOG_DIR"
    
    # Runtime directory
    log_info "Creating runtime directory..."
    mkdir -p "$LIB_DIR" "$RUN_DIR"
    chmod 755 "$LIB_DIR"
    chmod 755 "$RUN_DIR"
    
    # Create calypso user if it doesn't exist
    if ! id "calypso" &>/dev/null; then
        log_info "Creating calypso user..."
        useradd -r -s /bin/false -d "$LIB_DIR" -c "Calypso Appliance" calypso || true
    fi
    
    # Set ownership
    chown -R calypso:calypso "$INSTALL_PREFIX" 2>/dev/null || chown -R root:root "$INSTALL_PREFIX"
    chown -R calypso:calypso "$LIB_DIR" 2>/dev/null || chown -R root:root "$LIB_DIR"
    chown -R calypso:calypso "$LOG_DIR" 2>/dev/null || chown -R root:root "$LOG_DIR"
    
    log_info "✓ Filesystem structure created"
}

