#!/bin/bash set -e [ -n "$DEBUG" ] && set -x ENGINE=${ENGINE:-tectonic} ENGINE_ARGS=${ENGINE_ARGS:-} VIEWER=${VIEWER:-zathura} reexec= nocolor= while getopts 1Rnh opt; do case "$opt" in 1) entr_args='-z' ;; R) reexec=1 ;; n) nocolor=1 ;; h | ?) printf "usage: %s [-h]\n" "$(basename "$0")" printf "\n" printf " -1 \t only run once\n" printf " -n \t no color\n" printf "\n" exit 2 ;; esac done shift $((OPTIND - 1)) # http://stackoverflow.com/a/18443300/441757 _realpath() { OURPWD=$PWD cd "$(dirname "$1")" LINK=$(readlink "$(basename "$1")") while [ "$LINK" ]; do cd "$(dirname "$LINK")" LINK=$(readlink "$(basename "$1")") done REALPATH="$PWD/$(basename "$1")" cd "$OURPWD" echo "$REALPATH" } if [ -n "${reexec}" ]; then pdf=$(_realpath "$(printf "%s" "$1" | sed 's/\.tex/\.pdf/')") log() { if [ -n "${nocolor}" ]; then printf "%s!==>%s %s\n" "$(tput setaf 2)" "$(tput sgr0)" "$1" else printf "==> %s\n" "$1" fi } # compile the document log "running ${ENGINE}" ${ENGINE} ${ENGINE_ARGS} "$1" if ! pgrep -fx "${VIEWER} $pdf" >/dev/null; then # spawn the viewer log "running ${VIEWER}" (${VIEWER} "$pdf" >/dev/null 2>&1) & fi exit 0 fi if [ -z "$1" ]; then ls *.tex else [ -f "$1" ] && printf "$1\n" ls *.tex fi | entr ${entr_args} -ns 'latexmk -R $0'