#!/bin/bash
# @font{Courier-Bold10}
# @font{default}
# Check if the correct number of arguments is provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <filename>"
    exit 1
fi

# Get the argument
INPUT_FILE=$1
WIDTH=45
DOUBLE_WIDTH=$((WIDTH * 2))

# Output filenames
OUT_TXT="${INPUT_FILE%.txt}_formatted.txt"
OUT_PS="${INPUT_FILE%.txt}.ps"
OUT_PDF="${INPUT_FILE%.txt}.pdf"

# Step 1: Format and add headers with `pr`
fmt -$WIDTH "$INPUT_FILE" | pr -2 -w $DOUBLE_WIDTH -h "$INPUT_FILE" --form-feed > "$OUT_TXT"

# Step 2: Convert to PostScript with `enscript`
enscript --no-header -e@ "$OUT_TXT" -p "$OUT_PS"

# Step 3: Convert PostScript to PDF
ps2pdf "$OUT_PS" "$OUT_PDF"

# Notify the user
echo "Formatted PDF created: $OUT_PDF"

