#!/usr/bin/env bash
# typst-print:typst 编译包装脚本,支持 --print / --screen 参数生成不同版本 PDF
# typst-print: wrapper around `typst compile` with --print / --screen flags
#
# 用法 / Usage:
#   ./typst-print [--print|--screen] <input.typ> [output.pdf] [其他 typst 参数...]
#
# 示例 / Examples:
#   ./typst-print 地狱之下.typ                        # 普通编译(双栏、背景图)
#   ./typst-print --print 地狱之下.typ 输出.pdf        # 打印版(省墨、双栏、无背景)
#   ./typst-print --screen 地狱之下.typ 输出.pdf       # 小屏版(单栏、窄边距、保留背景)
#   ./typst-print --print --root .. 地狱之下.typ       # 打印版 + 自定义 root
#
# --print  -> typst --input print=true  (省墨打印:无背景、纯黑标题、宽边距)
# --screen -> typst --input screen=true (小屏阅读:单栏、窄边距、小字号)
# 两者可组合使用,但模板中 print 优先级高于 screen。

set -euo pipefail

PRINT_MODE=false
SCREEN_MODE=false
ARGS=()

# 解析参数:提取 --print / --screen,其余原样传给 typst
while [[ $# -gt 0 ]]; do
  case "$1" in
    --print)
      PRINT_MODE=true
      shift
      ;;
    --screen)
      SCREEN_MODE=true
      shift
      ;;
    *)
      ARGS+=("$1")
      shift
      ;;
  esac
done

INPUTS=()
if [[ "$PRINT_MODE" == "true" ]]; then
  INPUTS+=(--input print=true)
fi
if [[ "$SCREEN_MODE" == "true" ]]; then
  INPUTS+=(--input screen=true)
fi

exec typst compile "${INPUTS[@]}" "${ARGS[@]}"