#!/bin/bash

# Usage: ./map.sh [directory]
TARGET_DIR="${1:-.}"  # Defaults to current directory if none specified

# Function to recursively build JSON
function generate_json() {
    local DIR="$1"
    echo -n "{"
    local FIRST=true

    for ITEM in "$DIR"/*; do
        # Skip if item doesn't exist (for empty directories)
        [ -e "$ITEM" ] || continue
        [ "$FIRST" = true ] && FIRST=false || echo -n ","

        NAME=$(basename "$ITEM" | sed 's/"/\\"/g') # Escape quotes

        if [ -d "$ITEM" ]; then
            echo -n "\"$NAME\":"
            generate_json "$ITEM"
        else
            echo -n "\"$NAME\":\"file\""
        fi
    done

    echo -n "}"
}

# Generate JSON and save to file
generate_json "$TARGET_DIR" > tgustatic.json
echo "JSON saved to tgustatic.json"
