#! /bin/bash
#
# laps: latex to Postscript/PDF
#
# Options: --help for usage, --version for version and license
#
# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
# Andrew D. Hwang   <For address, do "laps -V">
# 

. "@pkglibdir@/epix-lib.sh"

declare -a DVIPS_OPTS
declare -a LAPS_FILE_LIST
declare -i LAPS_FILE_COUNT

PS2PDF="ps2pdf"
LATEX="latex"
LAPS_PDF=

LATEX_INTERACT="batchmode"

LAPS_EXTENSIONS="tex dtx ltx" # or NULL
LAPS_DOT_EXT=".${LAPS_EXTENSIONS// /|.}" # for usage message
LAPS_DEFAULT_EXT="tex"

declare -a TMP_CMD


function laps_help ()
{
cat <<"HELP"
Options:
    -h, --help
      Show this message and exit

    -V, --version, --gpl
      Show version and license

    -v, --verbose
      Scroll output/error messages

    -H, --huge
      Use hugelatex (if available)

    -i, -int, --interactive
      Run (La)TeX interactively (implies --verbose)

    -n, --no-config
      Ignore $(HOME)/.dvipsrc

    --pdf
      Post-process ps file with ps2pdf

    --pdf(la)tex
      Use pdf(la)tex instead of latex.

    -ps, --ps, --pslatex
      Use pslatex instead of latex.

    -P<printer>
      dvips printer options, e.g. "-Pamz", may be given on the command 
      line. They are also read from ~/.dvipsrc if this file exists.

    -t, --tex
      Use tex instead of latex

HELP
ePiX_bugreport

} # End of laps_help


function laps_parse_options {
# Command-line options start with "-"
while [ "$1" != "${1#-}" ]; do
    case "$1" in
 
        -h|--help)
	    ePiX_usage laps $LAPS_DOT_EXT
	    laps_help
	    exit 0
	    ;;

        -H|--huge)
            if [ $(which hugelatex 2> /dev/null) ]; then
                LATEX="hugelatex"
            else
                ePiX_warn "  hugelatex not available, ignoring \"$1\" option"
            fi
            shift
            ;;

        -i|-int|--interactive)
            EPIX_STDOUT="$SYSTEM_STDOUT"
            LATEX_INTERACT="scrollmode"
            shift
            ;;

        -n|--no-config) # do not use ~/.dvipsrc
            export DVIPSRC="$SYSTEM_DEVNULL"
            shift
            ;;

        -pdf|--pdf)
            LATEX="${LATEX##pdf}"
            LAPS_PDF="yes"
            shift
            ;;

        --pdflatex)
            LATEX="pdflatex"
            LAPS_PDF=""
            shift
            ;;

        --pdftex)
            LATEX="pdftex"
            LAPS_PDF=""
            shift
            ;;

        -ps|--ps|--pslatex)
            LATEX="pslatex"
            shift
            ;;

        -P)
            DVIPS_OPTS=("${DVIPS_OPTS[@]}" "-P$2")
            shift 2
            ;;

        -P*)
            DVIPS_OPTS=("${DVIPS_OPTS[@]}" "$1")
            shift
            ;;

        -t|--tex)
            LATEX="tex"
            shift
            ;;

        -V|--version|--gpl)
            ePiX_version laps
            ePiX_license
            exit 0
	    ;;

        -v|--verbose)
            if [ -z "$2" ]; then 
                echo "Please use -V for version"
                exit 0
            fi
            EPIX_STDOUT="$SYSTEM_STDOUT"
            shift
            ;;

        -vv)
            echo "Discontinued option -vv; please use -v for verbose output"
            exit 0
            ;;

        *)
            ePiX_warn "Ignoring unknown option \"$1\""
            shift
            ;;
    esac
done

LAPS_FILE_LIST=("$@")
} # End of laps_parse_options


function laps_compile_files()
{
    if [ -z "$1" ]; then ePiX_die "No input file specified"; fi

    # file counts
    local processed=0
    local success=0
    local failure=0

    for EPIX_INFILE in "${LAPS_FILE_LIST[@]}"; do

        # sets EPIX_INROOT, EPIX_SUFFIX, EPIX_NOTFOUND, touches EPIX_LOGFILE
        epix_parse_filename "$EPIX_INFILE" "$LAPS_EXTENSIONS"

        let processed=processed+1

        if [ "$EPIX_NOTFOUND" = "yes" ]; then
            let failure=failure+1
            continue
        fi

        TMP_CMD=($LATEX -interaction=$LATEX_INTERACT "$EPIX_INROOT.$EPIX_SUFFIX")
        TEMPFILES=("{TEMPFILES[@]}" "${EPIX_INROOT}.dvi" "${EPIX_INROOT}.log")

        # aux file out of date
        if [ "$EPIX_INROOT.$EPIX_SUFFIX" -nt "$EPIX_INROOT.aux" ]; then
            ePiX_msg "aux file out of date"

            ePiX_command "${TMP_CMD[@]}"
        fi

        ePiX_command "${TMP_CMD[@]}"

        if [ "${EPIX_INROOT}.$EPIX_SUFFIX" -nt "$EPIX_INROOT.dvi" ]; then
            ePiX_warn "\"${TMP_CMD[*]}\" failed"
            let failure=failure+1
            continue
        fi

        if [ "$LATEX" = "latex" -o "$LATEX" = "tex" ]; then
            TMP_CMD=(dvips "${DVIPS_OPTS[@]}" -f -o "$EPIX_INROOT.ps" "$EPIX_INROOT.dvi")

            ePiX_command "${TMP_CMD[@]}"

            if [ "$EPIX_INROOT.dvi" -nt "$EPIX_INROOT.ps" ]; then
                ePiX_warn "${TMP_CMD[*]} failed"
                let failure=failure+1
                continue
            fi
        fi

        if [ "$LAPS_PDF" = "yes" ]; then
            TMP_CMD=($PS2PDF "$EPIX_INROOT.ps")

            ePiX_command "${TMP_CMD[@]}"

            if [ "$EPIX_INROOT.ps" -nt "$EPIX_INROOT.pdf" ]; then
                ePiX_warn "${TMP_CMD[*]} failed"
                let failure=failure+1
                continue
            fi
        fi

        let success=success+1
        echo -e "\nTranscript written on $EPIX_LOGFILE" > "$EPIX_STDOUT"

    done # for EPIX_INFILE in $LAPS_INFILE_LIST

    if [ $processed -gt 1 ]; then
        echo "$processed files processed: $success successfully, $failure failed" > "$EPIX_STDERR"
    fi
}

### main ###

if [ $# -eq 0 ]; then
    ePiX_usage laps $LAPS_DOT_EXT
    laps_help
    exit 0
fi

laps_parse_options "$@"

laps_compile_files "${LAPS_FILE_LIST[@]}"

exit 0
