# crontab.sh — Project crontab processing and cron daemon startup.
# shellcheck shell=bash
# Requires: _strip_crlf, die, warn (from helpers.sh),
#           _collect_project_files, _confirm_project_files (from approval.sh).
# Project-level crontab requires confirmation (reuses SANDBOX_YOLO_CONFIG).
# Cron daemon is started only if there are actual (non-comment) entries.
# Copyright (c) 2026 Progralink. Licensed under the DevCage License — see LICENSE.

# _process_crontab: collect and install crontab entries from /sandbox-crontab.d/.
_process_crontab() {
    if [ ! -d /sandbox-crontab.d ]; then return; fi

    local CRONTAB_APPROVED=0
    declare -A PROJECT_CRONTAB_COPIES=()

    # First pass: collect project-scope crontab files (TOCTOU protection)
    local _project_crontabs=()
    _collect_project_files /sandbox-crontab.d "*.crontab" "03-project.crontab|03p*" ".crontab" \
        PROJECT_CRONTAB_COPIES _project_crontabs

    # Batch confirmation for project-scope crontab files
    if [ ${#_project_crontabs[@]} -gt 0 ]; then
        _confirm_project_files "crontab file" "Install these crontab entries?" \
            PROJECT_CRONTAB_COPIES _project_crontabs CRONTAB_APPROVED
    fi

    # Second pass: concatenate all approved crontab files
    local _merged_crontab
    _merged_crontab="$(mktemp /tmp/.devcage-merged-crontab-XXXXXXXX)" || \
        die "failed to create temp file for merged crontab"
    local _cronf _cronbase _clean_cron
    for _cronf in /sandbox-crontab.d/*.crontab; do
        [ -f "$_cronf" ] || continue
        _cronbase="$(basename "$_cronf")"
        case "$_cronbase" in
            03-project.crontab|03p*)
                if [ "$CRONTAB_APPROVED" = "1" ] && [ "${PROJECT_CRONTAB_COPIES[$_cronbase]:-}" != "" ]; then
                    printf '# — %s —\n' "$_cronbase" >> "$_merged_crontab"
                    _strip_crlf "${PROJECT_CRONTAB_COPIES[$_cronbase]}"
                    cat "${PROJECT_CRONTAB_COPIES[$_cronbase]}" >> "$_merged_crontab"
                    printf '\n' >> "$_merged_crontab"
                fi
                ;;
            *)
                _clean_cron="/tmp/.devcage-clean-$(basename "$_cronf")"
                sed 's/\r$//' "$_cronf" > "$_clean_cron"
                printf '# — %s —\n' "$_cronbase" >> "$_merged_crontab"
                cat "$_clean_cron" >> "$_merged_crontab"
                printf '\n' >> "$_merged_crontab"
                rm -f "$_clean_cron"
                ;;
        esac
    done

    # Install crontab and start cron if there are actual entries
    if grep -qE '^[^#[:space:]]' "$_merged_crontab" 2>/dev/null; then
        local _cron_entry_count
        _cron_entry_count="$(grep -cE '^[^#[:space:]]' "$_merged_crontab")"
        crontab -u devcage "$_merged_crontab"
        if command -v cron >/dev/null 2>&1; then
            cron
            echo "entrypoint: installed ${_cron_entry_count} crontab entry/entries and started cron daemon" >&2
        else
            warn "cron package not available — crontab installed but daemon not started"
        fi
    fi
    rm -f "$_merged_crontab"
}
