#############################################
# 
# For an overview of the availble commands, see the "help" message below (the
# "help" rule).

#############################################
# define variables

# VARS_DESCRIPTION: This will contain a string which will describe all the vars
# used. I rather use a semi-automated description to keep the descriptions and
# my remarks in sync.
VARS_DESCRIPTION=\
"\#\#\#\#\#\#\#\#\#\#\#\#\#"\
"\n\# Configuration Variables"\
"\n\# "\
"\n"

VARS_DESCRIPTION+=\
"\n\# DOCUMENT: the document name (without the .tex extention)"\
"\n\#           This is probably the only non-optional variable."\
"\nDOCUMENT=change_this_to_the_name_of_your_document"\
"\n"

ifndef DOCUMENT
# TODO: document this
  -include $(PWD)/Makefile
  ifndef DOCUMENT
    # Attempting to run any command (except "help") without it defined looks
    # pointless to me, but I can't think of a better way to report this error.
    DOCUMENT="YOU_MUST_DEFINE_THE_VARIABLE_DOCUMENT"
  endif
endif

#
# Guarantee that DOCUMENT has no trailing spaces:
#
DOCUMENT:=$(strip $(DOCUMENT))

VARS_DESCRIPTION+=\
"\n\# INP_TEX_FILES: A white-space seperated list of files."\
"\n\#                Files are assumed to reside in the current directory."\
"\n\#                Adding a file here has two effects:"\
"\n\#                1. the dvi document depends on it as well. Latex "\
"\n\#                   will be re-run if any of those file changes."\
"\n\#                2. It will be added to the source tarball."\
"\n\#INP_TEX_FILES=additional.tex extra.sty"\
"\n"

VARS_DESCRIPTION+=\
"\n\# PRINT_CMD: A command to print your postscript output."\
"\n\#            The command should be able to process postscript files from"\
"\n\#            the standard input."\
"\n\#            You probably don't need to change this in the local config"\
"\n\#            file."\
"\n\#PRINT_CMD=lpr"\
"\n"

ifndef PRINT_CMD
  # on a normal system you would have:
  #PRINT_COMMAND=lpr
  # but on my system:
  PRINT_CMD=ssh csd lp
endif

VARS_DESCRIPTION+=\
"\n\# TARGET_DIR: Default target for file copying. "\
"\n\#             Assuming you use rcp or scp, the syntax should be: "\
"\n\#              [<hostname>:]<path_to_target_dir>/ "\
"\n\#             (is having a '/' in the end a safeguard against some "\
"\n\#             typos?) "\
"\n\#TARGET_DIR=remote_server:docs_dir/ "\
"\n"

ifndef TARGET_DIR 
  # This is my default. But this is one of the things I change for every
  # project
  TARGET_DIR=csd:tmp/
endif

VARS_DESCRIPTION+=\
"\n\# ALWAYS_RUN_LATEX_ONCE: If you use any cross-references, table-of-"\
"\n\#                        content and similar fetures, latex needs to go"\
"\n\#                        over the text more than once. The information"\
"\n\#                        is preserved mainly in the auxilary (.aux) file."\
"\n\#                        Thus if the information in the auxilary file is"\
"\n\#                        not up-to-date, a second run of latex is needed."\
"\n\#                        Therefore it is a custom to always run latex twice."\
"\n\#                        However, you can do without the seond run, and run "\
"\n\#                        latex manually ('make latex') when needed. "\
"\n\#ALWAYS_RUN_LATEX_ONCE=1"\
"\n"

VARS_DESCRIPTION+=\
"\n\# ALL_TARGET: this is the file or target that gets made when you run"\
"\n\#             simply 'make (with no argument) or 'make all'."\
"\n\#             "\
"\n\#             The default target is 'dvi': update the dvi file."\
"\n\#             This is useful if you use xdvi or any other dvi viewer"\
"\n\#             as your standard previewr."\
"\n\#             If you use pdflatex to create a pdf document from the latex"\
"\n\#             source you may wish to change this to 'pdf'. If youuse gv"\
"\n\#             as your standard previwer you may wish to change it to 'ps'."\
"\n\#             "\
"\n\#             Also note that you may list here multiple targets, seperated"\
"\n\#             by spaces "\
"\n\#ALL_TARGET=pdf"\
"\n\#ALL_TARGET=ps"\
"\n"
# ALL_TARGET: The target of the default rule. These outputs will be produced
# by the default "make" commands.
ifndef ALL_TARGET
  ALL_TARGET=dvi
  #ALL_TARGET=src tgz ps pdf
endif


VARS_DESCRIPTION+=\
"\n\# COPY_TYPES: "\
"\n\# TO_COPY:    "\
"\n\#             These two variables can be used to describe what files are "\
"\n\#             copied in the 'copy' target. If TO_COPY is defined, it "\
"\n\#             overrides the definition of COPY_TYPES."\
"\n\#             "\
"\n\#             TO_COPY holds a list of files that will be copied."\
"\n\#             If TO_COPY is not definedm, then its value is set to "\
"\n\#             'DOCUMENT.' with all the word from COPY_TYPES as default."\
"\n\#             "\
"\n\#             The default value of COPY_TYPES is 'src.tgz ps pdf'"\
"\n\#             This means that by default the files to be copied will be "\
"\n\#             DOCUMENT.src.tgz (the source archive), DOCUMENT.ps and "\
"\n\#             DOCUMENT.pdf ."\
"\n\#             "\
"\n\#             Generally the source archive should contain all the various"\
"\n\#             figures, biblliography files and othersources, besides the"\
"\n\#             .tex document itself."\
"\n\#COPY_TYPES=src.tgz ps"\
"\n\#\# after the definition of DOCUMENT:"\
"\n\#\#TO_COPY=\$$ (DOCUMENT).ps other_notes.ps"\
"\n"
# can anybody tell me why in "\$$(DOCUMENT) the '$' is not escaped?
# I added <space><^H> to seperate the two but avoid this showing up in print
ifndef TO_COPY
  # list of files to copy to target dir
  ifdef COPY_TYPES
    # This one is premetive:
    TO_COPY=$(COPY_TYPES:%=$(DOCUMENT).%)
  else # TO_COPY and COPY_TYPES are not defined. Use default:
    TO_COPY=$(DOCUMENT).src.tgz $(DOCUMENT).ps $(DOCUMENT).pdf
  endif
endif

VARS_DESCRIPTION+=\
"\n\# SEND_TYPES: "\
"\n\# TO_SEND:    "\
"\n\#             These two variables can be used to describe what files are "\
"\n\#             copied in the 'copy' target. If TO_COPY is defined, it "\
"\n\#             overrides the definition of COPY_TYPES."\
"\n\#             "\
"\n\#             TO_COPY holds a list of files that will be copied."\
"\n\#             If TO_COPY is not definedm, then its value is set to "\
"\n\#             'DOCUMENT.' with all the word from COPY_TYPES as default."\
"\n\#             "\
"\n\#             The default value of COPY_TYPES is 'src.tgz ps pdf'"\
"\n\#             This means that by default the files to be copied will be "\
"\n\#             DOCUMENT.src.tgz (the source archive), DOCUMENT.ps and "\
"\n\#             DOCUMENT.pdf ."\
"\n\#             "\
"\n\#             Generally the source archive should contain all the various"\
"\n\#             figures, biblliography files and othersources, besides the"\
"\n\#             .tex document itself."\
"\n\#COPY_TYPES=src.tgz ps"\
"\n\#\# after the definition of DOCUMENT:"\
"\n\#\#TO_COPY=\$$ (DOCUMENT).ps other_notes.ps"\
"\n"
# can anybody tell me why in "\$$(DOCUMENT) the '$' is not escaped?
# I added <space><^H> to seperate the two but avoid this showing up in print
ifndef TO_SEND
  # list of files to send by email
  ifdef SEND_TYPES
    # This one is premetive:
    TO_SEND=$(SEND_TYPES:%=$(DOCUMENT).%)
  else # TO_SEND and SEND_TYPES are not defined. Use default:
    TO_SEND=$(DOCUMENT).src.tgz $(DOCUMENT).ps
  endif
endif
#ifndef TO_SEND
#  # A list of files to be sent by mail
#  TO_SEND=$(TO_COPY)
#endif


# if USE_BIBTEX isa non-null string, then bibtex will be run on the output,
# and the bibtex file will be added to the source tarball
# The name of the bibtex file can be overidden with BIBTEX_FILE .
# Maybe try to figure this automatically from the document? Grep for \bib ?
VARS_DESCRIPTION+=\
"\n\# USE_BIBTEX: Set this to a non-empty value if you wish to use a bibtex"\
"\n\#             bibliography (rather than 'manual' \\bibitem-s)."\
"\n\#             You should prepare a bibliography database (e.g."\
"\n\#             $(DOCUMENT).bib) and add a bibliography section in the  "\
"\n\#             document, with, e.g. '\\bibliography{$(DOCUMENT)}'. "\
"\n\#             "\
"\n\#             If you wish to use mybib.bib instead of $(DOCUMENT).bib"\
"\n\#             you should set BIBTEX_FILE to 'mybib' _and_ use "\
"\n\#             '\\bibliography{mybib}' in your document "\
"\n\#USE_BIBTEX=true"\
"\n\#\#BIBTEX_FILE=mybib"\
"\n"
ifdef USE_BIBTEX
  ifndef BIBTEX_FILE
    BIBTEX_FILE=$(DOCUMENT)
  endif
  BIBITEMS_FILE=$(DOCUMENT).bbl
  BIBTEX_BIB_FILE=$(BIBTEX_FILE:%=%.bib)
#else
#  BIBTEX_FILE=
endif

VARS_DESCRIPTION+=\
"\n\# BIBTEX_STYLE_FILE: Set this if you use a custom .bst file "\
"\n\#                    and it is in the current directory. "\
"\n\#                    If this is set then the bibtex target will depend "\
"\n\#                    on this file, and it will also be added to the "\
"\n\#                    sources archive. "\
"\n\#BIBTEX_STYLE_FILE=custom.bst"\
"\n"
ifndef BIBTEX_STYLE_FILE
  BIBTEX_ENV=
endif

VARS_DESCRIPTION+=\
"\n\# USE_INDEX: Set this to a non-empty value if you wish to use an index."\
"\n\#            You should also add '\\usepackage{makeidx}' and '\\makeindex' "\
"\n\#            in the header, and '\\printindex' in the body of the "\
"\n\#            document. "\
"\n\#USE_INDEX=true"\
"\n"
ifdef USE_INDEX
  INDEX_FILE=$(DOCUMENT).ind
else
  INDEX_FILE=
endif

VARS_DESCRIPTION+=\
"\n\# PS_PAGES: A comma-seperated list of pages and pages ranges"\
"\n\#           (e.g.:6-8 or 6-7,9,11-13). Used when converting dvi"\
"\n\#           to postscript."\
"\n\#           This one is probably only useful from the command-line"\
"\n\#PS_PAGES="\
"\n"
ifdef PS_PAGES
  # A list of files to be sent by mail
  TOPS_PAGES_SWITCH=-pp $(PS_PAGES)
endif

# VARS_DESCRIPTION+=\
# "\n\# LATEX_ENV: environment to be passed be used with the latex"\
# "\n\#            command. Here you can set TEXINPUTS, TEXMFOUTPUTS,"\
# "\n\#            TEXEDIT and some others"\
# "\n\#LATEX_ENV=TEXINPUTS=\".:$$HOME/LatexPackages:\""\
# "\n"
# ifndef LATEX_ENV
#   LATEX_ENV=
# endif
# 
# VARS_DESCRIPTION+=\
# "\n\# BIBTEX_ENV: environment to be passed be used with the bibtex"\
# "\n\#             command. Here you can set BIBINPUTS, BSTINPUTS, "\
# "\n\#             TEXMFOUTPUTS and some more ."\
# "\n\#BIBTEX_ENV=BIBINPUTS=\".:..\" BSTINPUTS=\".:$$HOME/styles:\""\
# "\n"
# ifndef BIBTEX_ENV
#   BIBTEX_ENV=
# endif
VARS_DESCRIPTION+=\
"\n\# EXTRA_INPUTS_DIR: A ':' seperated list of directories with some extra"\
"\n\#                   Tex/LaTeX files."\
"\n\#                   If you want a directory and all of its subdirs"\
"\n\#                   add a '//' to the end of its name. You can also prefix"\
"\n\#                   a directory with '!!' (watch the quoting!) to make the"\
"\n\#                   programs use a ls-R file instead of searching the "\
"\n\#                   whole subtree."\
"\n\#                   "\
"\n\#                   Technically, Setting this will set TEXINPUTS to:"\
"\n\#                   '.:EXTRA_INPUTS_DIR::' "\
"\n\#EXTRA_INPUTS_DIR=/home/tzafrir/tex/heblatex"\
"\n\#EXTRA_INPUTS_DIR=/home/tzafrir/tex/heblatex:/home/tzafrir/tex/prosper"\
"\n\#"\
"\n\# Alternatively:"\
"\n\#EXTRA_INPUTS_DIR=/home/tzafrir/tex//"\
"\n\#"\
"\n\# or the following, which is faster, but requires extra setup:"\
"\n\#EXTRA_INPUTS_DIR=!!/home/tzafrir/tex//"\
"\n"
ifdef EXTRA_INPUTS_DIR
  # note, this sets TEX_INPUTS, not TEXINPUTS
  TEX_INPUTS=TEXINPUTS='.:$(EXTRA_INPUTS_DIR)::'
endif


VARS_DESCRIPTION+=\
"\n\# LYX_SRC: Define this variable if your original document is a LyX"\
"\n\#          document."\
"\n\#          DOCUMENT.lyx willbe added to the source archive, and you will"\
"\n\#          be able to create latex (and hence postscript and PDF  "\
"\n\#          without launching the LyX GUI (if you have LyX version >="\
"\n\#          1.1.6)"\
"\n\#          "\
"\n\#          Note that LyX can of many of the things this makefile can do "\
"\n\#          (and maybe even better ;-) )"\
"\n\#LYX_SRC=1"\
"\n"
# if the source of the latex file is a lyx document, then LYX_INPUT will be
# that document. Otherwise it will be an empty string
ifdef LYX_SRC
  LYX_INPUT=$(DOCUMENT).lyx
else
  LYX_INPUT=
endif

VARS_DESCRIPTION+=\
"\n\# FIGS/PICS: A list of XFig figure files."\
"\n\#            They will be converted to EPS (encapsualted postscript)"\
"\n\#            files and included in the document. The original .fig "\
"\n\#            files will be included in the tarbal."\
"\n\#            "\
"\n\#            Note that currently those files have to have be"\
"\n\#            something.fig, and included as something.eps"\
"\n\#            in order for this makefile to work. See also PICS_FORMAT"\
"\n\#            "\
"\n\#            For example, if you have:"\
"\n\#PICS=fig1     something     else"\
"\n\#FIGS=fig1.fig something.fig else.fig"\
"\n\#            you could use fig1.eps, something.eps and else.eps in your"\
"\n\#            document."\
"\n"
ifndef FIGS
  FIGS=$(PICS:%=%.fig)
endif
EEPICS=$(patsubst %,%.eepic,$(PICS))
EPSPICS=$(patsubst %,%.eps,$(PICS))
PSTEXPICS=$(FIGS:%.fig=%.pstex)
PSTEXTPICS=$(FIGS:%.fig=%.pstex_t)
PSTEXALLPICS=$(FIGS:%.fig=%.pstex_t)

VARS_DESCRIPTION+=\
"\n\# PICS_FORMAT: latex_make supports two alternatives to standard EPS "\
"\n\#              pictures: EEPIC and PSTEX. Both formats, like EPS, are"\
"\n\#              generated from .fig xfig files."\
"\n\#              "\
"\n\#              In those two formats, any text field marked as \"special\""\
"\n\#              in XFig will be interpeted as-is in latex, and thus can"\
"\n\#              include subscripts, formulas, etc."\
"\n\#              "\
"\n\#              EEPIC is a file with some special picture-making macros. "\
"\n\#                    Some of XFig's features are not supported (e.g:"\
"\n\#                    colors) but the files are smaller. "\
"\n\#                    To use it: \input{file.eepic} "\
"\n\#              PSTEX This is EPS with a twist: \"special\" text fields "\
"\n\#                    are ommited from the postscript, and are added to"\
"\n\#                    a seperate latex macros file. "\
"\n\#                    You get file.pstex , which is the EPS file, and "\
"\n\#                    file.pstex_t, which is a latex file that includes "\
"\n\#                    file.pstex, and adds the required text fields. "\
"\n\#                    To use it: \input{file.pstex_t}"\
"\n\#              "\
"\n\#PICS_FORMAT=EEPIC"\
"\n\#PICS_FORMAT=PSTEX"\
"\n"
# by default use .eps, unless PICS_FORMAT==EEPIC
ifeq "$(PICS_FORMAT)" "EEPIC"
  PIC_FILES=$(EEPICS)
else
  ifeq "$(PICS_FORMAT)" "PSTEX"
    PIC_FILES=$(PSTEXALLPICS)
  else
    PIC_FILES=$(EPSPICS)
  endif
endif

VARS_DESCRIPTION+=\
"\n\# FIGMAG: Maginification for eepic pictures."\
"\n\#         By setting FIGMAG you set a default value."\
"\n\#FIGMAG=1.2"\
"\n\#         "\
"\n\#         If you want to use different magnifications for different"\
"\n\#         eepic pictures, you can set this value on a per-target basis."\
"\n\#         E.g.: if you want to set magnification of 0.8 (80%%) for"\
"\n\#         'myfig', and you use eepic for figures (PICS_FORMAT=EEPIC)"\
"\n\#         you can use:"\
"\n\#myfig.eepic: FIGMAG=0.80"\
"\n\#          "\
"\n\#         Note that you'll have to add a catch-all target somewhere before"\
"\n\#         this line. something like: '.phoney:all' . This keeps 'all' as"\
"\n\#         the default target. Otherwise you may find that a 'make' command"\
"\n\#         does nothing after creating myfig.eepic ."\
"\n"
ifndef FIGMAG
  FIGMAG=1.0
endif

VARS_DESCRIPTION+=\
"\n\# USE_SEMINAR: Special treatment of seminar slides using"\
"\n\#              \LatexMakeOptions"\
"\n\#USE_SEMINAR=true"\
"\n\#\#SLIDES_POSTFIX=-sld"\
"\n"
ifdef USE_SEMINAR
  ifndef SLIDES_POSTFIX
    SLIDES_POSTFIX=-slides
  endif
  SLIDES_DOCUMENT=$(DOCUMENT)$(SLIDES_POSTFIX)
  LANDSCAPE=-t landscapea
  # a comma-seperated list of options
  SLIDES_OPTIONS=slidesonly
  #SLIDES_OPTIONS=
  # The macro to which the value $(SLIDES_OPTIONS) will be added:
  SLIDES_OPTIONS_MACRO=LatexMakeOptions
  # Another macro to the value "slides"
  SLIDES_MAKE_SWITCH_MACRO=LatexMakeSwitch
#else
#  BIBTEX_FILE=
endif

VARS_DESCRIPTION+=\
"\n\# LATEX_EDITOR: Your text editor of choice for editing latex files"\
"\n\#               (with the \"edit\" target). My default is gvim"\
"\n\#LATEX_EDITOR=vim -c \\\"source mylatex.vimrc\\\""\
"\n\#LATEX_EDITOR=xemacs"\
"\n\#LATEX_EDITOR=emacs"\
"\n\#LATEX_EDITOR=he2"\
"\n"
ifndef LATEX_EDITOR
  LATEX_EDITOR=gvim
endif 

MAKEFILE_EDITOR=$(LATEX_EDITOR)

# any way of making this more generic?
ifndef CP
  CP=scp
endif
DEL=rm -fv
LATEX=elatex
BIBTEX=bibtex
MAKEINDEX=makeindex
XDVI=xdvi
DVI2PS=dvi2ps
PS_VIEWER=gv
TAR=tar
LYX=lyx
CAT=cat
PS2PDF=ps2pdf
VARS_DESCRIPTION+=\
"\n\# SLIDES_PS2PDF: a different command to create PDF from ps for the slides"\
"\n\#                copy. Should support the syntax:"\
"\n\#                command input_ps_file output_pdf_file"\
"\n\#                "\
"\n\#                (The default is to use the standard ps2pdf"\
"\n\#SLIDES_PS2PDF=distill"\
"\n"
ifndef SLIDES_PS2PDF
  SLIDES_PS2PDF=PS2PDF
endif

ifndef LANDSCAPE
  LANDSCAPE=
endif

# 
# LaTeX to HTML definitions
#
HTML_PROGRAM=latex2html

HTML_OPTIONS=--style hebrew

# I assume that the html output will be created in a subdirectory
HTML_SUBDIR=$(DOCUMENT)

# A file that can be used by the makefile to see if the html copy is
# up-to-date:
HTML_MARKER_FILE=$(HTML_SUBDIR)/index.html

# An archive to which the html subdirectory will be packed (in the mail
# directory!)
HTML_ARCHIVE=$(HTML_SUBDIR).html.tgz

LATEX_TO_HTML_COMMAND=$(HTML_PROGRAM) $(HTML_OPTIONS) $(LATEX_MAIN_FILE)

# TODO: add exclusion lists
HTML_PACK_COMMAND=$(TAR) -cvzf $(HTML_ARCHIVE) $(HTML_SUBDIR)

# I want to be a bit careful when running rm -rf
HTML_CLEAN_COMMAND=if [ -d $(HTML_SUBDIR) ]; then \
                      echo "removing html directory $(HTML_SUBDIR);" \
		      $(RM) -rf $(HTML_SUBDIR);\
		   fi

#
# Mail sendingg definitions
#

# MAIL_SUBJECT can be overriden from the commandline:
# make MAIL_SUBJECT="A subject of this mail message" mail
MAIL_SUBJECT="The Files of project $(DOCUMENT)"

MAIL_FILES=$(TO_SEND:%=-a %)

MUTT=mutt

# Override this to write the message content and send it manually
MESSAGE_BODY=@echo "sent by sent latex_make on $(shell date)" |

MAIL_COMMAND=$(MESSAGE_BODY) $(MUTT) -s $(MAIL_SUBJECT) $(MAIL_FILES) \
	                             $(MAIL_RCPT)

CREATED_EXT=dvi ps 4.ps pdf src.tgz

OUTPUTS=$(patsubst %,$(DOCUMENT).%,$(CREATED_EXT))

NON_SOURCE_FILES=*.dvi *.ps *.pdf *.src.tgz *.log *.aux *.eepic *~ \
		 *.pstex *.pstex_t

DVI_VIEW_COMMAND=$(TEX_INPUTS) $(XDVI)      $(DOCUMENT).dvi &
PS_VIEW_COMMAND= $(PS_VIEWER) $(DOCUMENT).ps  &

# format: $(PS2PS4) $(DOCUMENT).4.ps $(DOCUMENT).ps 
PS2PS4=a2ps -4 -o

# format: $(DVI2PS) $(DOCUMENT).ps $(DOCUMENT).dvi
DVI2PS=dvips -o
DVI2PS_CMD=$(TEX_INPUTS) dvips $(LANDSCAPE) $(TOPS_PAGES_SWITCH) -o

FIG2EEPIC=fig2dev -L eepic
FIG2EPS=fig2dev -L ps
FIG2EPS=fig2dev -L ps
FIG2PSTEX=fig2dev -L pstex
FIG2PSTEXT=fig2dev -L pstex_t

# inputs to latex, besides the main .tex file:
ADD_LATEX_INPUTS=$(INP_TEX_FILES) $(PIC_FILES)

LATEX_MAIN_FILE=$(DOCUMENT).tex

LATEX_COMMAND=$(TEX_INPUTS) $(LATEX) $(LATEX_MAIN_FILE)

BIBTEX_COMMAND=$(TEX_INPUTS) $(BIBTEX) $(DOCUMENT)

MAKEINDEX_COMMAND=$(TEX_INPUTS) $(MAKEINDEX) $(DOCUMENT).idx

LATEX_INPUTS=$(LATEX_MAIN_FILE) $(ADD_LATEX_INPUTS)

ifdef USE_SEMINAR
  LATEX_SLIDES_COMMAND=$(LATEX_ENV) $(LATEX) $(SLIDES_DOCUMENT)
endif

# note that this is only the definitions file. The "meat" of the 
# makefile is in the common included makefile. I assume that they
# all will have the same "interface"
MAKEFILE=$(DOCUMENT).mak

############################################
# define actions
all: $(ALL_TARGET)

view: view_dvi_doc

view-ps: view_ps_doc

tgz: $(DOCUMENT).src.tgz

ps: $(DOCUMENT).ps

ps4: $(DOCUMENT).4.ps

pdf: $(DOCUMENT).pdf

dvi: $(DOCUMENT).dvi

html: $(HTML_MARKER_FILE)

html-pack: $(HTML_ARCHIVE)

html-again: run_latextohtml

ifdef LYX_SRC
lyx: $(DOCUMENT).lyx
endif

copy: $(TO_COPY)
	$(CP) $(TO_COPY) $(TARGET_DIR)

tex: $(DOCUMENT).tex
	# leave it empty for now

mail: send_files

remove:
	$(DEL) $(OUTPUTS)

clean:
	$(DEL) $(NON_SOURCE_FILES)
	$(HTML_CLEAN_COMMAND)

print: print_doc

latex: run_latex

bibtex: run_bibtex

index: run_makeindex

edit: edit_main_tex_file

slides: $(SLIDES_DOCUMENT).ps

edit-cfg: edit_makefile

help:
	@echo "Makefile for LaTeX processing"
	@echo "============================="
	@echo "Availble targets:"
	@echo "dvi        Run elatex to create a dvi file"
	@echo "ps         Create a PostScript output"
	@echo "ps4        Create a 2x2 PostScript output"
	@echo "pdf        Create a PDF output"
	@echo "tgz        Pack .tex and figures into .src.tgz"
	@echo "html       Create an html version of the document"
	@echo "html-pack  Pack the html copy to an archive"
	@echo ""
	@echo "latex      Run elatex once ($(LATEX_COMMAND))"
	@echo "bibtex     Run bibtex once ($(BIBTEX_COMMAND))"
	@echo "index      Run makeindex once ($(MAKEINDEX_COMMAND))"
	@echo "html-again Run latex2html once"
	@echo ""
	@echo "view       preview document (dvi)"
	@echo "view-ps    preview postscript document"
	@echo "print      print document"
	@echo "edit       edit latex file (editor: $(LATEX_EDITOR))"
	@echo "edit-cfg   edit project configuration file ($(MAKEFILE))"
	@echo "copy       Copy selected targets to target dir (can use s/r-cp)"
	@echo "mail       Send sepsified outputs by mail (experimental)"
	@echo "remove     Delete all outputs. Reprocess"
	@echo "clean      Clean any non-source file in this directory"
	@echo ""
	@echo "target 'all' (default) is $(ALL_TARGET)"
	@echo "figure files: $(PIC_FILES)"
	@echo "copy command: $(CP) $(TO_COPY) $(TARGET_DIR)"

# I used printf because this seems to be the simplest way to print text with
# line breaks.
# This is bound to break on systems where /bin/sh has limitations for length of # the command-line...
vars:
	@printf "`echo $(VARS_DESCRIPTION)`"

###########################################
# implement actions:

$(DOCUMENT).4.ps: $(DOCUMENT).ps
	$(PS2PS4) $(DOCUMENT).4.ps $(DOCUMENT).ps 

$(DOCUMENT).pdf: $(DOCUMENT).ps
	$(PS2PDF) $(DOCUMENT).ps

ifdef USE_BIBTEX
  $(BIBITEMS_FILE): $(LATEX_MAIN_FILE) $(BIBTEX_BIB_FILE) $(BIBTEX_STYLE_FILE)
	$(LATEX_COMMAND)
	$(BIBTEX_COMMAND)
endif

$(DOCUMENT).dvi: $(LATEX_INPUTS) $(BIBITEMS_FILE) $(INDEX_FILE)
	$(LATEX_COMMAND)
ifndef ALWAYS_RUN_LATEX_ONCE
	$(LATEX_COMMAND)
endif

ifdef USE_SEMINAR
# I skip bibtex-ing and makeindex for the moment
  ifdef USE_BIBTEX
    SLIDES_BBL_FILE=$(SLIDES_DOCUMENT).bbl
    $(SLIDES_BBL_FILE): $(DOCUMENT).bbl
	$(CP) $< $@
  else
    SLIDES_BBL_FILE=
  endif
  $(SLIDES_DOCUMENT).tex: $(DOCUMENT).tex
	echo "\newcommand{\$(SLIDES_OPTIONS_MACRO)}{$(SLIDES_OPTIONS)}" >$@
	echo "\newcommand{\$(SLIDES_MAKE_SWITCH_MACRO)}{slides}"       >>$@
	echo "\input{$(DOCUMENT).tex}"                                 >>$@

  $(SLIDES_DOCUMENT).dvi: $(SLIDES_DOCUMENT).tex $(LATEX_INPUTS) \
                          $(SLIDES_BBL_FILE)
	$(LATEX_SLIDES_COMMAND)
  ifndef ALWAYS_RUN_LATEX_ONCE
	$(LATEX_SLIDES_COMMAND)
  endif

  $(SLIDES_DOCUMENT).ps: $(SLIDES_DOCUMENT).dvi
	$(DVI2PS_CMD) $(SLIDES_DOCUMENT).ps $(SLIDES_DOCUMENT).dvi   

  $(SLIDES_DOCUMENT).pdf: $(SLIDES_DOCUMENT).ps
	$(SLIDES_PS2PDF) $(SLIDES_DOCUMENT).ps $(SLIDES_DOCUMENT).pdf   

endif

edit_main_tex_file:
	$(LATEX_EDITOR) $(LATEX_MAIN_FILE)

edit_makefile:
	$(MAKEFILE_EDITOR) $(MAKEFILE)

run_latex: $(LATEX_INPUTS)
	$(LATEX_COMMAND)

run_bibtex: $(LATEX_INPUTS) #Is that the correct dependency?
	$(BIBTEX_COMMAND)

$(DOCUMENT).idx: $(DOCUMENT).tex
	$(LATEX_COMMAND)

$(DOCUMENT).ind: $(DOCUMENT).idx 
	$(MAKEINDEX_COMMAND)

run_makeindex: $(DOCUMENT).idx
	$(MAKEINDEX_COMMAND)

$(DOCUMENT).src.tgz: $(DOCUMENT).tex $(FIGS) $(INP_TEX_FILES) \
                     $(MAKEFILE) $(LYX_INPUT) $(BIBTEX_BIB_FILE) \
		     $(BIBTEX_STYLE_FILE)
	$(TAR) -czf $@ $^

$(DOCUMENT).ps: $(DOCUMENT).dvi
	$(DVI2PS_CMD) $(DOCUMENT).ps $(DOCUMENT).dvi   

ifdef LYX_SRC
$(DOCUMENT).tex: $(DOCUMENT).lyx
	$(LYX) --export latex $^
$(DOCUMENT).txt: $(DOCUMENT).lyx
	$(LYX) --export text $^
endif

view_dvi_doc: dvi
	$(DVI_VIEW_COMMAND)

view_ps_doc: ps
	$(PS_VIEW_COMMAND)

print_doc: $(DOCUMENT).ps
	$(CAT) $< |$(PRINT_CMD)

# The normal way to refresh the html subdirectory
$(HTML_MARKER_FILE): $(LATEX_MAIN_FILE) $(BIBITEMS_FILE)
	$(LATEX_TO_HTML_COMMAND)

# force an update of the html subdirectory
run_latextohtml:
	$(LATEX_TO_HTML_COMMAND)

$(HTML_ARCHIVE): $(HTML_MARKER_FILE)
	$(HTML_PACK_COMMAND)


send_files: $(TO_SEND)
ifndef MAIL_RCPT
	@echo "You must first define a recipient, MAIL_RCPT"
else
	#@echo "TO_SEND=$(TO_SEND)"
	#@echo "MAIL_FILES=$(MAIL_FILES)"
	$(MAIL_COMMAND)
endif

###########################################
# general rules

%.eepic: %.fig
	 $(FIG2EEPIC) -m $(FIGMAG) $< $@

%.eps: %.fig
	$(FIG2EPS) $< $@

%.pstex: %.fig
	$(FIG2PSTEX) $< $@

%.pstex_t: %.fig
	$(FIG2PSTEXT) $< $@
