
#!/usr/bin/env bash
unset CDPATH

# Determine script directory safely
FILE_DIR_PATH="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
ROOTPATH="$(dirname "$FILE_DIR_PATH")"

CONFIG="$ROOTPATH/config/git.paths.conf"

# Load SSOT
source "$CONFIG" "$ROOTPATH"

set -euo pipefail

#!/usr/bin/env bash
#VERSION tag

# 1. Unset CDPATH to prevent 'cd' from printing the target directory
unset CDPATH

# 2. Redirect cd's stdout to /dev/null so only 'pwd' feeds the variable
FILE_DIR_PATH="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null && pwd)"

# 3. Strip any accidental trailing newlines or spaces using parameter expansion
FILE_DIR_PATH="${FILE_DIR_PATH%%$'\n'*}"
ROOTPATH="$(dirname "$FILE_DIR_PATH")"

echo "FILE_DIR_PATH=$FILE_DIR_PATH"
echo "ROOTPATH=$ROOTPATH"
CONFIG="$ROOTPATH/config/git.paths.conf"

source "$CONFIG" $ROOTPATH
set -euo pipefail

# Load config
SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
#source "$SCRIPT/../config/git-util.conf"
echo "CONFIG=$CONFIG";
source "$CONFIG" "$ROOTPATH"

log() {
    local msg="$1"
    printf "[%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$msg" | tee -a "$GIT_LOG_FILE"
}

ensure_dirs() {
    mkdir -p "$GIT_DOCS" "$GIT_CODE" "$GIT_ARCH" "$GIT_CHUNKS"
}

show_main_menu() {
    cat << EOF
Git Utility App
===============

1) Show documentation
2) Show code samples
3) Show architecture notes
4) List bundles
5) Verify decoded files
6) Check bundle integrity (SHA256)
7) Rebuild decoded files from bundles
8) Export decoded files back into bundles
9) Show Git status
10) Show Git log (oneline)
11) Show metadata/version
12) Update metadata/version
13) Exit

EOF
}

show_file_or_error() {
    local label="$1"
    local f="$2"
    if [[ -f "$f" ]]; then
        log "Showing $label: $f"
        less "$f"
    else
        log "$label file missing: $f"
        echo "[ERROR] $label file not found: $f"
    fi
}

show_docs()  { show_file_or_error "docs"  "$GIT_DOCS/docs.txt"; }
show_code()  { show_file_or_error "code"  "$GIT_CODE/code.txt"; }
show_arch()  { show_file_or_error "arch"  "$GIT_ARCH/arch.txt"; }

list_bundles() {
    log "Listing bundles in $GIT_CHUNKS"
    if [[ -d "$GIT_CHUNKS" ]]; then
        ls -l "$GIT_CHUNKS"
    else
        echo "[ERROR] Bundle directory not found: $GIT_CHUNKS"
    fi
}

verify_decoded_files() {
    log "Verifying decoded files"
    for entry in "${GIT_BUNDLES[@]}"; do
        IFS=":" read -r name bundle decoded <<< "$entry"
        if [[ -f "$decoded" ]]; then
            echo "[OK] $name → $decoded"
        else
            echo "[MISSING] $name → $decoded"
        fi
    done
}

decode_bundle_mac() {
    local bundle_file="$1"
    local output_file="$2"

    if [[ ! -f "$bundle_file" ]]; then
        echo "[ERROR] Bundle not found: $bundle_file"
        return 1
    fi

    base64 -D < "$bundle_file" | gzip -d > "$output_file"
}

encode_bundle_mac() {
    local input_file="$1"
    local bundle_file="$2"

    if [[ ! -f "$input_file" ]]; then
        echo "[ERROR] Input file not found: $input_file"
        return 1
    fi

    gzip -c "$input_file" | base64 > "$bundle_file"
}

check_bundle_integrity() {
    log "Checking bundle integrity (decode + SHA256)"
    for entry in "${GIT_BUNDLES[@]}"; do
        IFS=":" read -r name bundle decoded <<< "$entry"
        echo "Bundle: $name"
        if [[ ! -f "$bundle" ]]; then
            echo "  [MISSING] bundle: $bundle"
            continue
        fi

        local tmp
        tmp="$(mktemp)"
        if decode_bundle_mac "$bundle" "$tmp"; then
            local sum
            sum=$(shasum -a 256 "$tmp" | awk '{print $1}')
            echo "  [OK] decoded, SHA256: $sum"
        else
            echo "  [ERROR] failed to decode: $bundle"
        fi
        rm -f "$tmp"
    done
}

rebuild_decoded_from_bundles() {
    log "Rebuilding decoded files from bundles"
    ensure_dirs
    for entry in "${GIT_BUNDLES[@]}"; do
        IFS=":" read -r name bundle decoded <<< "$entry"
        echo "Rebuild: $name"
        if [[ ! -f "$bundle" ]]; then
            echo "  [MISSING] bundle: $bundle"
            continue
        fi
        decode_bundle_mac "$bundle" "$decoded" \
            && echo "  [OK] wrote: $decoded" \
            || echo "  [ERROR] failed to decode: $bundle"
    done
}

export_decoded_to_bundles() {
    log "Exporting decoded files back into bundles"
    ensure_dirs
    for entry in "${GIT_BUNDLES[@]}"; do
        IFS=":" read -r name bundle decoded <<< "$entry"
        echo "Export: $name"
        if [[ ! -f "$decoded" ]]; then
            echo "  [MISSING] decoded file: $decoded"
            continue
        fi
        encode_bundle_mac "$decoded" "$bundle" \
            && echo "  [OK] wrote bundle: $bundle" \
            || echo "  [ERROR] failed to encode: $decoded"
    done
}

git_status() {
    log "Running git status"
    (cd "$GIT_ROOT" && git status)
}

git_log_oneline() {
    log "Running git log --oneline"
    (cd "$GIT_ROOT" && git log --oneline || echo "[WARN] No commits yet.")
}

show_metadata() {
    log "Showing metadata/version"
    if [[ -f "$GIT_META_FILE" ]]; then
        cat "$GIT_META_FILE"
    else
        echo "[INFO] No metadata file yet: $GIT_META_FILE"
    fi
}

update_metadata() {
    log "Updating metadata/version"
    read -rp "Version tag (e.g., v1.0.0): " ver
    read -rp "Description: " desc

    cat > "$GIT_META_FILE" << EOF
Git Utility Metadata
====================

Version: $ver
Description: $desc
Updated: $(date '+%Y-%m-%d %H:%M:%S')

EOF

    echo "[OK] Metadata updated: $GIT_META_FILE"
}

main_loop() {
    ensure_dirs
    while true; do
        show_main_menu
        read -rp "Select option [1-13]: " choice
        case "$choice" in
            1) show_docs ;;
            2) show_code ;;
            3) show_arch ;;
            4) list_bundles ;;
            5) verify_decoded_files ;;
            6) check_bundle_integrity ;;
            7) rebuild_decoded_from_bundles ;;
            8) export_decoded_to_bundles ;;
            9) git_status ;;
            10) git_log_oneline ;;
            11) show_metadata ;;
            12) update_metadata ;;
            13) log "Exiting Git Utility App"; exit 0 ;;
            *) echo "[ERROR] Invalid choice: $choice" ;;
        esac
        echo ""
    done
}

main_loop

