Skip to content

Instantly share code, notes, and snippets.

@RichardHladik
Last active March 30, 2021 13:14
Show Gist options
  • Select an option

  • Save RichardHladik/1b0bf9e993bb2c8caa88411ec3e6d136 to your computer and use it in GitHub Desktop.

Select an option

Save RichardHladik/1b0bf9e993bb2c8caa88411ec3e6d136 to your computer and use it in GitHub Desktop.
Build a TeX file on a remote server
#!/bin/bash
set -e
REMOTE=gimli
CMD=pdfcsplain
trap_codes="0 1 2 3 13 15"
die() {
echo "$@"
exit 1
}
cleanup() {
# return # uncomment for debugging
trap - $trap_codes
[ $# -gt 0 ] && echo "$@"
ssh $REMOTE "rm -rf $tmp"
[ $# -gt 0 ] && exit 1
}
usage() {
echo "Usage: $0 src.tex [macros_directory | macros_file] … "
echo "Copies src.tex and all macro files and directories to \$REMOTE (= $REMOTE) and calls \$CMD (= $CMD) on src.tex"
exit 1
}
[ $# -lt 1 -o "$1" = -h -o "$1" = --help ] && usage
declare -a real
for fn in "$@"; do
real+=("$(realpath -s -- "$fn")")
[ -e "$real" ] || die "File does not exist: $fn"
done
PWD="$(realpath -s . | tail -c+2)"
TEX="${real[0]}"
PDF="$(echo "$TEX" | sed -re 's/(.tex|)$/.pdf/')"
trap cleanup $trap_codes
tmp="$(tar hc "${real[@]}" | gzip -9 | ssh $REMOTE 'tmp=$(mktemp -d remotetex-XXXXXXXXXXXX); cd $tmp; gunzip | tar xv > /dev/stderr; echo $tmp')"
REMOTETEX="$(echo "$TEX" | tail -c+2)"
REMOTEPDF="$(basename "$TEX" | sed -re 's/.tex$/.pdf/')"
ssh $REMOTE "a=\`realpath $tmp\`; cd $tmp || exit 1; mkdir -p $PWD; cd $PWD; TEXINPUTS=.//:\$a//: $CMD \$a/$REMOTETEX" || cleanup "Non-zero return code, aborting."
ssh $REMOTE "cat $tmp/$PWD/$REMOTEPDF | gzip -9" | gunzip > $PDF
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment