#!/bin/bash

# Simple script to convert markdown files to PDF
# Usage: ./md2pdf.sh input.md [output.pdf]

if [ $# -eq 0 ]; then
    echo "Usage: $0 input.md [output.pdf]"
    echo "Example: $0 docs/index.md output.pdf"
    exit 1
fi

INPUT_FILE="$1"

# Check if input file exists
if [ ! -f "$INPUT_FILE" ]; then
    echo "Error: File '$INPUT_FILE' not found"
    exit 1
fi

# Determine output filename
if [ $# -eq 2 ]; then
    OUTPUT_FILE="$2"
else
    # Use input filename with .pdf extension
    OUTPUT_FILE="${INPUT_FILE%.md}.pdf"
fi

# Check if pandoc is installed
if ! command -v pandoc &> /dev/null; then
    echo "Error: pandoc is not installed"
    echo "Install it using: brew install pandoc (macOS) or apt-get install pandoc (Linux)"
    exit 1
fi

# Convert markdown to PDF
echo "Converting $INPUT_FILE to $OUTPUT_FILE..."
pandoc "$INPUT_FILE" -o "$OUTPUT_FILE" --pdf-engine=pdflatex

if [ $? -eq 0 ]; then
    echo "Successfully created $OUTPUT_FILE"
else
    echo "Error: Conversion failed"
    exit 1
fi
