#!/usr/bin/env bash
# pdfprint -- print-ready PDF with proper margins, headers, Shunn-style
set -euo pipefail

usage() {
    echo "Usage: pdfprint <input.md> [--format shunn|book|essay] [--output file.pdf]"
    exit 1
}

[ $# -lt 1 ] && usage

input="$1"; shift
format="essay" output=""

while [ $# -gt 0 ]; do
    case "$1" in
        --format|-f) format="$2"; shift 2 ;;
        --output|-o) output="$2"; shift 2 ;;
        *) echo "Unknown option: $1"; usage ;;
    esac
done

[ -z "$output" ] && output="${input%.md}.pdf"

case "$format" in
    shunn)
        # Standard manuscript format (Shunn)
        pandoc "$input" --pdf-engine=xelatex \
            -V geometry:margin=1in \
            -V fontsize:12pt \
            -V linestretch:2 \
            -V mainfont:"Courier New" \
            -V header-includes:'\usepackage{fancyhdr}\pagestyle{fancy}\fancyhead[R]{\thepage}' \
            -o "$output"
        ;;
    book)
        # Book interior format (6x9, tighter margins)
        pandoc "$input" --pdf-engine=xelatex \
            -V geometry:'paperwidth=6in,paperheight=9in,margin=0.75in,inner=0.875in' \
            -V fontsize:11pt \
            -V linestretch:1.3 \
            -V mainfont:"Linux Libertine" \
            --toc \
            -o "$output"
        ;;
    essay)
        # Clean essay format
        pandoc "$input" --pdf-engine=xelatex \
            -V geometry:margin=1in \
            -V fontsize:12pt \
            -V linestretch:1.5 \
            -V mainfont:"Linux Libertine" \
            -o "$output"
        ;;
    *)
        echo "Unknown format: $format (use shunn, book, or essay)"
        exit 1
        ;;
esac

echo "Created: $output ($format format, $(du -h "$output" | cut -f1))"
