#!/bin/bash

#       -------------------------------------------------------------------
#
#       Shell program to start an instance of "acroread" on localhost via
#       "pdfopen" and call "pdfopen" again every time when catching the SIGUSR1
#       signal. This provides the same behaviour as "xdvi" for reloading changed
#       files. Acrobat Reader doesn't come with a "watch file" option, hence
#       this workaround, sigh...
#
#       Copyright 2005, Thorsten Bonow
#                         ( thorsten.bonow at post.rwth-aachen.de ).
#
#       This program is free software; you can redistribute it and/or
#       modify it under the terms of the GNU General Public License as
#       published by the Free Software Foundation; either version 2 of the
#       License, or (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful, but
#       WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#       General Public License for more details.
#
#       Description:
#
#
#
#       Usage:
#
#               startacrobat [ -h | --help ] [PDF document]
#
#       Options:
#
#               -h, --help      Display this help message and exit.
#
#       Known Bugs:
#
#       No other instance of Acrobat Reader should be running: this script kills
#       all of them on exit!
#
#       Revision History:
#
#       01/05/2005      Initial version, adapted from "startxpdf"
#       28/08/2005      Adapted to "pdfopen" and "acrobat"
#       01/14/2006      Use HUP or USR1 signal for update to be fully
#                           compatible with latexmk 3.07 and 3.08
#                       (modification by John Collins (collins at phys.psu.edu))
#
#
#       -------------------------------------------------------------------


#       -------------------------------------------------------------------
#       Constants
#       -------------------------------------------------------------------

       PROGNAME=$(basename $0)
       VERSION="0.95A"
       ACRO_RELOAD_EXEC="pdfopen --file"
       ACROBAT_EXEC="acroread"

#       -------------------------------------------------------------------
#       Functions
#       -------------------------------------------------------------------


function clean_up
{

#       -----------------------------------------------------------------------
#       Function to kill remote instance of acrobat
#               No arguments
#       -----------------------------------------------------------------------

       return
}


function error_exit
{

#       -----------------------------------------------------------------------
#       Function for exit due to fatal program error
#               Accepts 1 argument:
#                       string containing descriptive error message
#       -----------------------------------------------------------------------


       echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
       clean_up
       exit 1
}


function graceful_exit
{

#       -----------------------------------------------------------------------
#       Function called for a graceful exit
#               No arguments
#       -----------------------------------------------------------------------

       clean_up
       exit
}


function usage
{

#       -----------------------------------------------------------------------
#       Function to display usage message (does not exit)
#               No arguments
#       -----------------------------------------------------------------------

       echo "Usage: ${PROGNAME} [-h | --help]  [PDF document]"
}


function helptext
{

#       -----------------------------------------------------------------------
#       Function to display help message for program
#               No arguments
#       -----------------------------------------------------------------------

       local tab=$(echo -en "\t\t")

       cat <<- -EOF-

       ${PROGNAME} ver. ${VERSION}
       Shell program to start an instance of "acroread" on localhost via
       "acro-reload" and call "acro-reload" again every time when catching the
       SIGUSR1 signal. This provides the same behaviour as "xdvi" for reloading
       changed files. Acrobat Reader doesn't come with a "watch file" option,
       hence this workaround, sigh...

       $(usage)

       Options:

       -h, --help      Display this help message and exit.




-EOF-
}


function signal_handle
{

#       -----------------------------------------------------------------------
#       Function to handle signals
#               Accepts 1 argument:
#                       signal_spec
#       -----------------------------------------------------------------------

       case $1 in
               INT)    echo "$PROGNAME: Program aborted by user" >&2
                       clean_up
                       exit
                       ;;
               TERM)   echo "$PROGNAME: Program terminated" >&2
                       clean_up
                       exit
                       ;;
               USR1)   echo "$PROGNAME: Reloading..." >&2
                       $ACRO_RELOAD_EXEC $PDF_FILE
                       ;;
               *)      error_exit "$PROGNAME: Terminating on unknown signal"
                       ;;
       esac
}


#       -------------------------------------------------------------------
#       Program starts here
#       -------------------------------------------------------------------


##### Command Line Processing #####

if [ "$1" = "--help" ]; then
       helptext
       graceful_exit
fi

while getopts ":h" opt; do
       case $opt in

               h )     helptext
                       graceful_exit ;;
               * )     usage
                       clean_up
                       exit 1
       esac
done

PDF_FILE="$1"

##### Initialization And Setup #####

# Trap TERM and INT signals and properly exit

trap "signal_handle TERM" TERM
trap "signal_handle INT"  INT

# Trap HUP and USR1 signals for reloading the PDF document
# Convert to USR1
# (Note HUP is used by gv, and USR1 by xdvi, so both signals have precedent)

trap "signal_handle USR1" HUP USR1


##### Main Logic #####

$ACRO_RELOAD_EXEC $PDF_FILE

while [ 1 ];
 do
 # sleeps again after being interrupted by SIGUSR1, breaks if Acrobat Reader is
 # killed
 sleep 1
 eval pgrep "-f" "$ACROBAT_EXEC" ">/dev/null" || break
done

graceful_exit