Skip to content

Instantly share code, notes, and snippets.

@otype
Created November 4, 2009 23:41
Show Gist options
  • Save otype/226517 to your computer and use it in GitHub Desktop.
Save otype/226517 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# GLOBALS
#
FILENAME=""
TOOLNAME="`basename $0`"
VERSION="0.1"
USE_BIB=0
#
# TOOLS
#
ECHO="`which echo`"
# #######################################################
# FUNCTIONS
# #######################################################
#
# usage()
#
# Print out help page
#
usage()
{
$ECHO "Usage: $TOOLNAME [OPTIONS] TEXFILE"
$ECHO ""
$ECHO "Options:"
$ECHO " -b, --bibtex Compile with Bibtex"
$ECHO " -h, --help Print out this help menu"
$ECHO " -v, --version Print out version"
$ECHO " "
$ECHO "Information:"
$ECHO " If Acrobat Reader cannot be found, Evince is used instead."
$ECHO " "
}
#
# parse_file()
#
# Checks if file exists and how filename was provided
# (with or without extension .tex)
#
parse_file()
{
CUT="`which cut`"
param="$1"
# Strip off extension
FILENAME="`$ECHO $param | $CUT -d. -f1`"
# check if file even exists
if [ ! -f "${FILENAME}.tex" ]; then
$ECHO "Cannot find ${FILENAME}.tex! Bailing out!"
exit 1
fi
}
#
# compile_tex()
#
# Compile tex file
#
compile_tex()
{
use_bib=$1
BIBTEX="`which bibtex`"
LATEX="`which latex`"
PDFLATEX="`which pdflatex`"
# Check if all binaries exist
for i in $BIBTEX $LATEX $PDFLATEX
do
if [ ! -e "$i" ]; then
$ECHO "ERROR: Cannot find $i!"
exit 1
fi
done
# Start compilation
$ECHO "Starting compilation of $FILENAME"
$ECHO " "
$LATEX ${FILENAME}.tex
if [ $? -ne 0 ]; then
$ECHO "ERROR: Error occured during compilation!"
exit 1
fi
if [ $USE_BIB -eq 1 ]; then
$BIBTEX ${FILENAME}.aux
fi
if [ $? -ne 0 ]; then
$ECHO "ERROR: Error occured during compilation!"
exit 1
fi
$LATEX ${FILENAME}.tex
if [ $? -ne 0 ]; then
$ECHO "ERROR: Error occured during compilation!"
exit 1
fi
$LATEX ${FILENAME}.tex
if [ $? -ne 0 ]; then
$ECHO "ERROR: Error occured during compilation!"
exit 1
fi
$PDFLATEX ${FILENAME}.tex
if [ $? -ne 0 ]; then
$ECHO "ERROR: Error occured during compilation!"
exit 1
fi
$ECHO "Compilation successful!"
unset BIBTEX
unset LATEX
unset PDFLATEX
}
#
# open_pdfreader()
#
# Open PDF in available reader
#
open_pdfreader()
{
acroread="`which acroread`"
evince="`which evince`"
if [ "$acroread" == "" ]; then
reader="$evince"
else
reader="$acroread"
fi
$ECHO "Opening PDF-file ... please wait!"
$reader ${FILENAME}.pdf
unset acroread
unset evince
unset reader
}
# #######################################################
# MAIN
# #######################################################
# Bail out if we don't have at least one parameter
if [ $# -eq 0 ]; then
$ECHO "ERROR: Missing parameter!"
usage
exit 1
fi
# Iterate through params
while [ "$1" != "" ]; do
case $1 in
-b | --bibtex)
USE_BIB=1
;;
-h | --help)
usage
exit 0
;;
-v | --version)
$ECHO "$TOOLNAME v.${VERSION}"
$ECHO " "
$ECHO "Created by Hans-Gunther Schmidt on Wed Feb 18 09:31:10 CET 2009"
$ECHO "Contact via E-Mail: hans@otype.net"
$ECHO "Copyright 2009 Otype.net. All rights reserved."
$ECHO " "
exit 0
;;
*)
parse_file $1
compile_tex $USE_BIB
open_pdfreader
esac
shift
done
#
# Clean up and bail out
#
unset ECHO
unset FILENAME
unset TOOLNAME
unset VERSION
unset USE_BIB
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment