#!/usr/bin/env bash
# gniza4cp/lib/pkgacct.sh — pkgacct execution, .sql gzipping, temp cleanup

run_pkgacct() {
    local user="$1"
    local temp_dir="${TEMP_DIR:-$DEFAULT_TEMP_DIR}"
    local output_dir="$temp_dir/$user"

    mkdir -p "$temp_dir" || {
        log_error "Failed to create temp directory: $temp_dir"
        return 1
    }

    # Clean any previous attempt
    [[ -d "$output_dir" ]] && rm -rf "$output_dir"

    log_info "Running pkgacct for $user..."
    log_debug "CMD: /usr/local/cpanel/bin/pkgacct --incremental --nocompress --backup --skiphomedir $user $temp_dir"
    if ! /usr/local/cpanel/bin/pkgacct --incremental --nocompress --backup --skiphomedir "$user" "$temp_dir"; then
        log_error "pkgacct failed for $user"
        return 1
    fi

    if [[ ! -d "$output_dir" ]]; then
        log_error "pkgacct output directory not found: $output_dir"
        return 1
    fi

    log_info "pkgacct completed for $user"
    return 0
}

gzip_sql_files() {
    local user="$1"
    local temp_dir="${TEMP_DIR:-$DEFAULT_TEMP_DIR}"
    local mysql_dir="$temp_dir/$user/mysql"

    if [[ ! -d "$mysql_dir" ]]; then
        log_debug "No mysql directory for $user, skipping gzip"
        return 0
    fi

    local count=0
    while IFS= read -r -d '' sql_file; do
        log_debug "Compressing: $sql_file"
        if gzip -f "$sql_file"; then
            ((count++)) || true
        else
            log_warn "Failed to gzip: $sql_file"
        fi
    done < <(find "$mysql_dir" -name "*.sql" -print0)

    log_info "Compressed $count SQL file(s) for $user"
    return 0
}

cleanup_pkgacct() {
    local user="$1"
    local temp_dir="${TEMP_DIR:-$DEFAULT_TEMP_DIR}"
    local output_dir="$temp_dir/$user"

    if [[ -d "$output_dir" ]]; then
        rm -rf "$output_dir"
        log_debug "Cleaned up pkgacct temp for $user"
    fi
}

cleanup_all_temp() {
    local temp_dir="${TEMP_DIR:-$DEFAULT_TEMP_DIR}"
    if [[ -d "$temp_dir" ]]; then
        rm -rf "${temp_dir:?}"/*
        log_debug "Cleaned up all temp contents: $temp_dir"
    fi
}
