#!/usr/bin/env bash
# 自动生成 HELP.md 文档
# 用法：./scripts/gen-help.sh

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
GMS="$PROJECT_ROOT/target/debug/gms"

# 确保 gms 已编译
if [ ! -f "$GMS" ]; then
    echo "Building gms..."
    cd "$PROJECT_ROOT" && cargo build
fi

OUTPUT="$PROJECT_ROOT/HELP.md"
IGNORE_FILE="$PROJECT_ROOT/.gmsignore"

echo "Generating HELP.md..."

cat > "$OUTPUT" << 'HEADER'
# gen-mdbook-summary Help

> This document is auto-generated by `scripts/gen-help.sh`.
> Do not edit manually.

## Quick Start

```bash
# Generate SUMMARY.md in src directory
gms -d src -o src/SUMMARY.md

# Or use default output location
gms
```

---

HEADER

# Main help
echo "## Commands" >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo "### Main Command" >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo '```' >> "$OUTPUT"
$GMS --help 2>&1 | sed 's/^/  /' >> "$OUTPUT"
echo '```' >> "$OUTPUT"
echo "" >> "$OUTPUT"

# Init help
echo "### \`gms init\`" >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo "Initialize the \`.gmsignore\` file with default ignore patterns." >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo '```' >> "$OUTPUT"
$GMS init --help 2>&1 | sed 's/^/  /' >> "$OUTPUT"
echo '```' >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo "**Default ignore patterns:**" >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo '```' >> "$OUTPUT"
if [ -f "$IGNORE_FILE" ]; then
    cat "$IGNORE_FILE" >> "$OUTPUT"
else
    echo "book.toml" >> "$OUTPUT"
    echo ".gitignore" >> "$OUTPUT"
    echo "book" >> "$OUTPUT"
    echo "*.doc" >> "$OUTPUT"
    echo "*.pdf" >> "$OUTPUT"
    echo "*.png" >> "$OUTPUT"
    echo "*.xlsx" >> "$OUTPUT"
    echo "*.pptx" >> "$OUTPUT"
    echo "*.jpg" >> "$OUTPUT"
fi
echo '```' >> "$OUTPUT"
echo "" >> "$OUTPUT"

# Gen help
echo "### \`gms gen\`" >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo "Generate the \`SUMMARY.md\` file." >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo '```' >> "$OUTPUT"
$GMS gen --help 2>&1 | sed 's/^/  /' >> "$OUTPUT"
echo '```' >> "$OUTPUT"
echo "" >> "$OUTPUT"

# Usage examples
cat >> "$OUTPUT" << 'EXAMPLES'
## Usage Examples

### Basic Usage

```bash
# Generate SUMMARY.md in src directory
gms -d src -o src/SUMMARY.md

# Output to current directory
gms -d src -o SUMMARY.md

# Output to docs directory
gms -d src -o docs/SUMMARY.md
```

### With Custom Ignore File

```bash
# Use custom ignore file
gms -d src -o src/SUMMARY.md -i .myignore

# Use mdbook.ignore file
gms -d src -o src/SUMMARY.md -i mdbook.ignore
```

### Disable Sorting

```bash
# Generate without sorting
gms -d src -o src/SUMMARY.md --sort=false
```

## Output File Location

The tool supports outputting `SUMMARY.md` to any location:

| Output Location | Command |
|----------------|---------|
| `src/SUMMARY.md` | `gms -d src -o src/SUMMARY.md` |
| `./SUMMARY.md` | `gms -d src -o SUMMARY.md` |
| `docs/SUMMARY.md` | `gms -d src -o docs/SUMMARY.md` |

**Note:** The output file is automatically excluded from the generated summary.

## Ignore File

### Default Ignore File: `.gmsignore`

Create default ignore file:
```bash
gms init
```

### Ignore File Syntax

Uses `.gitignore` pattern syntax:

```
# Ignore specific files
*.pdf
*.png

# Ignore directories
book/

# Ignore by name
mdbook.ignore
```

### Common Patterns

```
# Book configuration
book.toml

# Build output
book/

# Images
*.png
*.jpg
*.gif

# Documents
*.pdf
*.doc
*.docx
*.xlsx
*.pptx
```

## Path Handling

### Relative Paths

Generated `SUMMARY.md` uses **relative paths** (relative to the `-d` directory):

```markdown
# Summary

- [Chapter 1](chapter1.md)
- [Chapter 2](chapter2.md)
```

### Special Characters

- **Spaces**: Automatically encoded as `%20`
- **Unicode**: Supported (Chinese, Japanese, etc.)

Example:
```
file with space.md  →  file%20with%20space.md
```

## Troubleshooting

### SUMMARY.md appears in output

The output file is automatically excluded. If you see it in the summary, make sure you specified the correct output path with `-o`.

### Files not ignored

Check your ignore file:
1. File exists at the specified location
2. Patterns use correct `.gitignore` syntax
3. File path is relative to the source directory

### mdbook fails to parse SUMMARY.md

Check for:
1. Correct Markdown link syntax: `- [Name](path.md)`
2. File paths are relative to the `SUMMARY.md` location
3. No special characters in paths (or properly encoded)

---

EXAMPLES

# Add generated timestamp
echo "*Generated on: $(date -u +"%Y-%m-%d %H:%M:%S UTC")*" >> "$OUTPUT"

echo "✓ HELP.md generated at $OUTPUT"
