#!/bin/sh opener=xdg-open show_help() { printf 'usage: %s [OPTIONS] [FILE] ...\n' "${0##*/}" printf 'compiles each FILE to pdf. Supported input file types are:\n' printf ' - latex (extension .tex) including dependency recursion\n' printf ' - gnuplot (extension .gp)\n' printf ' - mandoc (extension .[0-9])\n' printf ' - groff/mom (extension .mom)\n' printf 'If no FILE is specified, compile all above in the present directory.\n' printf 'OPTIONS are one or more of the following:\n' printf ' -h show this message\n' printf ' -c continuously recompile upon file changes (requires entr(1))\n' printf ' -o open output file after compiling (requires %s(1))\n' "$opener" printf ' -- do not consider any following args as options\n' } die() { printf '%s: error: %s\n' "${0##*/}" "$1" >&2 exit 1 } regeximatch() { printf '%s' "$1" | grep -iEq "$2" } extract_tex_includes() { if ! test -r "$1" then return fi awk ' function firstval(s) { if(!match(s, /%.*\{/) && match(s, /\{.*\}/)) return substr(s, RSTART+1, RLENGTH-2) } $0 ~ /\\input\{/ { print firstval($0) } $0 ~ /\\include\{/ { print firstval($0) } $0 ~ /\\includegraphics\[*.*\]*\{/ { print firstval($0) } $0 ~ /\\addbibresource\{/ { print firstval($0) } $0 ~ /\\bibliography\{/ && $0 !~ /%.*\{/ { m = firstval($0) if (m !~ /\.bib$/) printf("%s.bib", m) else print m } ' "$1" } find_dependencies() { case "$1" in *.tex) printf '%s\n' "$1" extract_tex_includes "$1" ;; *) die "cannot find dependencies in unknown file type $1";; esac } run_compile() { if test "$continuous" = 1 then printf '%s\n' "$2" | entr -s "$1" else eval "$1" fi } handle_target() { base="$(basename "${1%.*}")" if test -e "$1" then case "$1" in *.tex) deps="$1" deps="$(find_dependencies "$1")" for d in $deps; do if [ "${d##*.}" = "tex" ]; then deps="$(printf '%s\n%s\n' "${deps}" "$(find_dependencies "$d")")" fi done deps="$(printf '%s\n' "$deps" | sort | uniq)" printf 'deps=%s\n' "$deps" if regeximatch "$deps" '\.bib'; then compile_string="pdflatex '$1'; bibtex '${1%.tex}'; pdflatex '$1'; pdflatex '$1'" else compile_string="pdflatex '$1' && pdflatex '$1'" fi run_compile "$compile_string" "$deps" if [ "$open" = 1 ]; then $opener "${1%.tex}.pdf" fi ;; *.gp) run_compile "gnuplot '$1'" "$1";; *.[0-9]) out="$(basename "${base}").pdf" if [ "$open" = 1 ]; then run_compile "refer -PS -e '$1' | groff -mandoc -T pdf > '$out' && $opener '$out'" "$1" else run_compile "refer -PS -e '$1' | groff -mandoc -T pdf > '$out'" "$1" fi;; *.mom) out="$(basename "${base}").pdf" if [ "$open" = 1 ]; then run_compile "pdfmom '$1' > '$out' && $opener '$out'" "$1" else run_compile "pdfmom '$1' > '$out'" "$1" fi;; *) die "File type ${1##*.} not supported";; esac else die "cannot read $1" fi } continuous=0 open=0 while :; do case "$1" in -h|-\?) show_help exit 0;; -c) continuous=1;; -o) open=1;; --) # end all options shift break;; -?*) die 'unknown option specified';; *) # No more options break esac shift done if test $# -lt 1 then for f in *.tex *.gp *.[0-9] *.mom do handle_target "$f" done else for f in "$@" do handle_target "$f" done fi