#!/usr/bin/env bash
source "$GIT_CONFIG/git.paths.conf"
set -euo pipefail

OUT_DIR="git/data/chunks64"
mkdir -p "$OUT_DIR"

make_bundle() {
    local name="$1"
    local tmp="tmp_${name}.txt"

    # Write the correct content into the temp file
    cat > "$tmp"

    # Encode: gzip → base64
    gzip -c "$tmp" | base64 > "$OUT_DIR/${name}.64"

    rm "$tmp"
    echo "[OK] Created $OUT_DIR/${name}.64"
}

# -----------------------------
# DOCS
# -----------------------------
make_bundle "docs" << 'EOF'
Git Utility Documentation
=========================

Overview
--------
This utility provides a structured installer system for Git-related
resources. It supports external bundle decoding, architecture notes,
and code samples that can be deployed across environments.

Contents
--------
1. Installation Process
2. Bundle Structure
3. Decoding Pipeline
4. Architecture Overview
5. Code Module Summary

Installation Process
--------------------
The installer reads three bundles:
- docs.64
- code.64
- arch.64

Each bundle is encoded using gzip + base64 and validated with SHA256
checksums. The installer extracts each bundle into its respective
directory under the git/ root.

Bundle Structure
----------------
Bundles are stored in:
git/data/chunks64/

Decoded output is written to:
git/docs/
git/code/
git/arch/

Decoding Pipeline
-----------------
1. base64 decode
2. gzip decompress
3. checksum verification
4. write output file

This ensures integrity and portability across systems.

End of Documentation
EOF

# -----------------------------
# CODE
# -----------------------------
make_bundle "code" << 'EOF'
Git Utility Code Samples
========================

Sample Bash Function
--------------------
# Validate a directory exists
ensure_dir() {
    local d="$1"
    if [[ ! -d "$d" ]]; then
        echo "[WARN] Creating directory: $d"
        mkdir -p "$d"
    fi
}

Sample Git Commands
-------------------
git init
git add .
git commit -m "Initial commit"
git log --oneline

Sample JSON Output
------------------
{
    "utility": "git-installer",
    "version": "1.0.0",
    "status": "ready"
}

End of Code Samples
EOF

# -----------------------------
# ARCH
# -----------------------------
make_bundle "arch" << 'EOF'
Git Installer Architecture Notes
================================

System Overview
---------------
The Git Installer System is composed of three major layers:

1. Bundle Layer
   - Encoded resources stored externally
   - Uses gzip + base64 for portability
   - SHA256 checksum for integrity

2. Installer Layer
   - Decodes bundles
   - Writes output files
   - Provides commit prompt
   - Ensures directory structure consistency

3. Output Layer
   - docs/ contains documentation
   - code/ contains code samples
   - arch/ contains architecture notes

Data Flow
---------
User → Installer → Bundle Decode → Output Directories → Git Commit

Design Goals
------------
- Portability
- Simplicity
- Predictable directory structure
- External bundle storage
- Minimal dependencies

End of Architecture Notes
EOF

echo "[DONE] All bundles created successfully."

