# File aliases for common commands
alias w := watch
alias c := compile
alias nw := nix-watch
alias nb := nix-build
alias nd := nix-develop


# Configuration
set shell := ["fish", "-ic"]

file := "src/main.typ"
doc_config := "src/doc.toml"
# out := `cat {{doc_config}} | tomlq '.doctype + (.worknumber | tostring) + "_" + (.authors[0].name | split(" ")[0]) + "_" + (.authors[0].edu + "-" + .authors[0].group) + "_" + .subject + ".pdf"'`
# out := 'bla'

# Default target - show available recipes
default:
    @just --list

# Validate required files exist
_check-files:
    #!/usr/bin/env bash
    set -euo pipefail
    if [[ ! -f "{{file}}" ]]; then
        echo "Error: Source file {{file}} not found"
        exit 1
    fi
    if [[ ! -f "{{doc_config}}" ]]; then
        echo "Error: Config file {{doc_config}} not found"
        exit 1
    fi

# Watch file for changes and recompile automatically
[group('typst')]
watch: _check-files
    typst w {{file}}

# Compile the document once
[group('typst')]
compile: _check-files
    typst c {{file}}

# Compile and copy to parent directory
[group('typst')]
copy: compile
    #!/usr/bin/env bash
    # set -euo pipefail
    # if [[ ! -f "{{out}}" ]]; then
    #     echo "Error: Output file {{out}} not found after compilation"
    #     exit 1
    # fi
    local final_name = $(cat {{doc_config}} | tomlq '.doctype + (.worknumber | tostring) + "_" + (.authors[0].name | split(" ")[0]) + "_" + (.authors[0].edu + "-" + .authors[0].group) + "_" + .subject + ".pdf"')
    cp "main.typ" "$final_name"

# Enter nix development environment
[group('nix')]
nix-develop:
    nix develop .

# Watch using nix environment
[group('nix')]
nix-watch:
    nix run .#watch

# Build using nix environment
[group('nix')]
nix-build:
    nix run .#build

# Clean generated PDFs
[group('utils'), confirm]
clean:
    @rm -rvf *.pdf

# Show generated filename
# [group('utils')]
# show-output:
#     @echo "{{out}}"