# *****************************************************************************
#  $Id: Makefile,v 2.36 1994/12/11 18:48:54 gerd Exp gerd $
# =============================================================================
#  
#  This file is part of BibTool.
#  It is distributed under the GNU General Public License.
#  See the file COPYING for details.
#  
#  (c) 1996-1999 Gerd Neugebauer
#  
#  Net: gerd.neugebauer@sdm.de
#	gerd.neugebauer@gmx.de
#  
# *****************************************************************************

#  Look also in the file include/bibtool/config.h for additional
#  configuration parameters.
#  Read the file INSTALL for explanation.
# =============================================================================

#  This is the generic makefile for BibTool.
#  Especially it can be used on Unix systems and as a basis for
#  systems not directly mentioned in the distribution.

# =============================================================================
#  Configuration Section
# =============================================================================
# 
prefix	    = @prefix@
exec_prefix = @exec_prefix@

# -------------------------------------------------------
#  Destination directory for make install
#  This is usually a directory where public executables
#  are installed.

BINDIR     = @bindir@

# -------------------------------------------------------
#  Destination directory for make install.man
#  This is usually a directory where public man pages
#  are installed.
#  Additionally name the section of the man pages to use.
#  This should be 1 (user commands), l (local), or n (new)
# 
MANDIR     = @mandir@
MANSECT    = 1

# -------------------------------------------------------
#  Destination directory for make install.lib
#  This is usually a directory where public shared files
#  are installed plus a final BibTool.
# 

LIBDIR     = @datadir@/bibtool

#-------------------------------------------------------
# Destination directory for make install.include
# This is usually a directory where public include files
# are installed plus a final `bibtool'.
# Note: The include files may contain system specific
#	code and may not be shared amoung architectures.
#

INCLUDEDIR = @includedir@/bibtool

# -------------------------------------------------------
#  Name the C compiler
# 

CC         = @CC@

# -------------------------------------------------------
#  CFLAGS are the flags for the C compiler.
#  For debugging it should be -g. 
#  For final version you can try to activate 
#  the optimizer with -O or -O2 or whatever
#  BEWARE: Some C compiler produce erroneous code if the
# 	   optimizer is turned on.
# 

CFLAGS     =
# CFLAGS = -O
#  GNU C
# CFLAGS = -g -Wall
# CFLAGS = -g -Wall -pedantic -Wpointer-arith -Wshadow -Wcast-align

##-------------------------------------------------------
## C_INCLUDE are the flags for the C compiler pointing it
## to the location of the header files. Usually this
## does not need any modifications.
##

C_INCLUDE     = -Iinclude

# -------------------------------------------------------
#  EXT is the optional extension of the final executable.
#  Various operating systems have their own ideas about
#  that.
# 

EXT        =

# -------------------------------------------------------
#  Extension of object files
# 

OBJ        = .o

# -------------------------------------------------------
#		  Name some programs
# 
# -------------------------------------------------------
#  MV is a command to move certain files maybe to other
#  directories or drives. Only plain files are moved.
#

MV		= mv

# -------------------------------------------------------
#  RM is a command to remove certain files. It should not
#  be confused when trying to remove non-existent files.
#  Only plain files are removed this way.
#

RM		= rm -rf

# -------------------------------------------------------
#  MAKEDEPEND is a program to find dependencies for
#  .c and .h files.
#  Not present on MS-DOS, Atari, Amiga, Next

MAKEDEPEND	= makedepend
# MAKEDPEND	= gcc -MM

# -------------------------------------------------------
#  INSTALL is a program to properly install some files.
#  Maybe cp should also do this job (on UNIX)

INSTALL		= @INSTALL@

# -------------------------------------------------------
#  INSTALL_DATA is a command to install some data files.
# 

INSTALL_DATA	= @INSTALL_DATA@

# -------------------------------------------------------
#  INSTALL_DIR is a command to create a directory if not
#  already existent. If your install does not support the 
#  -d option you can try to use mkdir instead.

INSTALL_DIR	= install -d

# -------------------------------------------------------
# The ranlib program or : for nothing.
#
RANLIB		= @RANLIB@

# -------------------------------------------------------
# The ar program invocation to add files to the archive.
#
AR		= ar r

# -------------------------------------------------------
# PERL is the complete path to an existing perl executable.
# Perl 4.* and Perl 5.* are both ok.
#
PERL		= @PERL@

# -------------------------------------------------------
#  LINT is a program to check the C source for problems.
#  

LINT		= lint

# -------------------------------------------------------
#  CXREF is a C cross reference program.
#  

CXREF		= cxref

# -------------------------------------------------------
#  Defines to support (non-ANSI) C compilers.
#  For Solaris 2 you may have to define HAVE_STRING_H
#  For ANSI C compilers they should be empty.
#  Mostly needed for the GNU regex library.
#  Include
#  -DHAVE_STRING_H	if your C compiler has string.h
# 			(e.g. on Solaris 2)
# 			Maybe you have to enlarge the
# 			include search path.
#  -DREGEX_MALLOC	if you have NO alloca() library
# 			function
#  -DHAVE_ALLOCA_H	if you need alloca.h for alloca()
# 

NON_ANSI_DEFS = @DEFS@

# -------------------------------------------------------
#  GNU Regular Expression Library support.
#  First of all the (sub)directory containing the 
#  necessary files (excluding trailing slash).
# 
#  This directory is contained in the BibTool distribution.
#  I have tried newer versions of this library without
#  positive results. (Try it if you don't believe me:-)
# 
#REGEX_DIR  = regex-0.12
# 

REGEX_DEF  = -DREGEX

#REGEX      = regex$(OBJ)

# -------------------------------------------------------
#  Kpathsea Library support.
#  This library provides means to specify a search path 
#  with recursive search in subdirectories.
#  The library is NOT contained in the BibTool distribution.
#  This routines are expected to work with Kpathsea-2.6 or
#  above.
# 
#  See the file INSTALL for details.
# 
#  Kpathsea is not known to work yet on anything else but
#  Unix platforms.
#  This item is purely experimental.
#  Maybe you are better off not trying it!
# 
#  The value of KPATHSEA_DIR is the directory containing
#  the distribution of kpathsea. I.e. the directory
#  containing the subdirectory kpathsea.
#  The value of KPATHSEA is the name of the kpathsea library.
#  if kpathsea should not be used then it has to be empty.
#  The value of KPATHSEA_DEF are the additional flags for the
#  C compiler. It should arrange things that the macro
#  KPATHSEA is defined and the directory beneath kpathsea
#  is in the include search path.

KPATHSEA_DIR = @kpathsea_dir@
KPATHSEA     = @kpathsea_lib@
KPATHSEA_DEF = @kpathsea_def@

# -------------------------------------------------------
#  Default search paths
#  The values are NULL or a string containing a colon
#  separated list of directories.
#  DON'T FORGET THE CURRENT DIRECTORY!
# 
#  The character ENV_SEP separates the directories in
#  environment search paths.
# 

BIBINPUTS_DEFAULT = NULL
BIBTOOL_DEFAULT   = \".:$(LIBDIR)\"
ENV_SEP           = \":\"

# -------------------------------------------------------
#  Declare the file naming conventions.
#  FILE_TYPES contains optionally a macro definition
#  determining the file naming conventions.
#  -DMS-DOS denotes MSDOG-like file names. (Also for Atari)
#  -DAMIGA denotes the Amiga file names.
#  The default (empty) are UN*X-like file names.
# 
#  DIR_SEP is the directory-file separator.

FILE_TYPES    = 
DIR_SEP       = /

# =============================================================================
#  End of Configuration Section
# =============================================================================

MAKEFILE      = makefile

SHELL	      = /bin/sh

RSC_DEF	      = -DRSC_BIBINPUTS_DEFAULT=$(BIBINPUTS_DEFAULT)	\
		-DRSC_BIBTOOL_DEFAULT=$(BIBTOOL_DEFAULT)	\
		-DENV_SEP=$(ENV_SEP)

DONT_LINK     = -c
LINK_TO       = -o
STANDALONE    = -DSTANDALONE

C_FLAGS	      = $(CFLAGS) $(C_INCLUDE) $(FILE_TYPES)

CFILES	      = main.c				\
		$(CLIBFILES)
CLIBFILES     = database.c			\
		entry.c				\
		error.c				\
		expand.c			\
		init.c				\
		key.c				\
		macros.c			\
		names.c				\
		parse.c				\
		print.c				\
		pxfile.c			\
		record.c			\
		rewrite.c			\
		rsc.c				\
		s_parse.c			\
		symbols.c			\
		stack.c				\
		sbuffer.c			\
		tex_aux.c			\
		tex_read.c			\
		type.c				\
		version.c			\
		wordlist.c

HPATH	      = include${DIR_SEP}bibtool${DIR_SEP}
HFILES	      = config.h		\
		${HPATH}database.h		\
		${HPATH}bibtool.h		\
		${HPATH}config.h		\
		${HPATH}entry.h			\
		${HPATH}error.h			\
		${HPATH}expand.h		\
		${HPATH}general.h		\
		${HPATH}init.h			\
		${HPATH}key.h			\
		${HPATH}keynode.h		\
		${HPATH}macros.h		\
		${HPATH}names.h			\
		${HPATH}parse.h			\
		${HPATH}print.h			\
		${HPATH}pxfile.h		\
		${HPATH}regex.h			\
		${HPATH}record.h		\
		${HPATH}resource.h		\
		${HPATH}rewrite.h		\
		${HPATH}rsc.h			\
		${HPATH}s_parse.h		\
		${HPATH}sbuffer.h		\
		${HPATH}stack.h			\
		${HPATH}symbols.h		\
		${HPATH}tex_aux.h		\
		${HPATH}tex_read.h		\
		${HPATH}type.h			\
		${HPATH}version.h		\
		${HPATH}wordlist.h

OFILES	      = main$(OBJ)			\
		$(OLIBFILES)
OLIBFILES     = database$(OBJ)			\
		entry$(OBJ)			\
		error$(OBJ)			\
		expand$(OBJ)			\
		init$(OBJ)			\
		key$(OBJ)			\
		macros$(OBJ)			\
		names$(OBJ)			\
		parse$(OBJ)			\
		print$(OBJ)			\
		pxfile$(OBJ)			\
		record$(OBJ)			\
		rewrite$(OBJ)			\
		rsc$(OBJ)			\
		s_parse$(OBJ)			\
		symbols$(OBJ)			\
		stack$(OBJ)			\
		sbuffer$(OBJ)			\
		tex_aux$(OBJ)			\
		tex_read$(OBJ)			\
		type$(OBJ)			\
		version$(OBJ)			\
		wordlist$(OBJ)

DOCFILES      = Doc$(DIR_SEP)bibtool.1		\
		Doc$(DIR_SEP)bibtool.tex	\
		Doc$(DIR_SEP)bibtool.bib	\
		Doc$(DIR_SEP)bibtool.sty	\
		Doc$(DIR_SEP)bibtool.bbl	\
		Doc$(DIR_SEP)bibtool.aux	\
		Doc$(DIR_SEP)bibtool.ind	\
		Doc$(DIR_SEP)bibtool.idx	\
		Doc$(DIR_SEP)bibtool.ist	\
		Doc$(DIR_SEP)dina4.sty		\
		Doc$(DIR_SEP)ref_card.tex	\
		Doc$(DIR_SEP)Makefile		\
		Doc$(DIR_SEP)c_lib.tex		\
		Doc$(DIR_SEP)c_lib.ind

LIBFILES      = Lib$(DIR_SEP)braces.rsc		\
		Lib$(DIR_SEP)check_y.rsc	\
		Lib$(DIR_SEP)default.rsc	\
		Lib$(DIR_SEP)field.rsc		\
		Lib$(DIR_SEP)improve.rsc	\
		Lib$(DIR_SEP)iso2tex.rsc	\
		Lib$(DIR_SEP)month.rsc		\
		Lib$(DIR_SEP)opt.rsc		\
		Lib$(DIR_SEP)sort_fld.rsc	\
		Lib$(DIR_SEP)tex_def.rsc

PROGFILES     = Perl$(DIR_SEP)bibtool.pl	\
		Tcl$(DIR_SEP)bibtool.tcl
ETCFILES      = README				\
		INSTALL				\
		COPYING				\
		Changes.tex			\
		makefile.unx			\
		makefile.dos			\
		makefile.ata			\
		makefile.ami			\
		ToDo.tex			\
		THANKS				\
		pxfile.man			\
		sbuffer.man			\
		$(MSDOS_targets)		\
		$(PROGFILES)

DISTFILES     = $(ETCFILES)			\
		$(CFILES)			\
		$(HFILES)			\
		$(LIBFILES)			\
		$(DOCFILES)			
# -----------------------------------------------------------------------------
#  The main target
# -----------------------------------------------------------------------------

default all: bibtool$(EXT)

bibtool$(EXT): $(OFILES) $(KPATHSEA)
	$(CC) $(C_FLAGS) $(LINK_TO) $@ $(OFILES) $(KPATHSEA)

tex_read$(EXT): tex_read.c
	$(CC) $(C_FLAGS) $(STANDALONE) tex_read.c $(LINK_TO) tex_read$(EXT)

tex_aux$(OBJ): tex_aux.c
	$(CC) $(C_FLAGS) $(REGEX_DEF) $(DONT_LINK) tex_aux.c -o tex_aux$(OBJ)

main$(OBJ): main.c
	$(CC) $(C_FLAGS) $(REGEX_DEF) $(KPATHSEA_DEF) $(DONT_LINK) main.c

init$(OBJ): init.c
	$(CC) $(C_FLAGS) $(KPATHSEA_DEF) $(DONT_LINK) init.c

parse$(OBJ): parse.c
	$(CC) $(C_FLAGS) $(KPATHSEA_DEF) $(DONT_LINK) parse.c -o parse$(OBJ)

rewrite$(OBJ): rewrite.c
	$(CC) $(C_FLAGS) $(REGEX_DEF) $(DONT_LINK) rewrite.c -o rewrite$(OBJ)

rsc$(OBJ): rsc.c
	$(CC) $(C_FLAGS) $(RSC_DEF) $(DONT_LINK) rsc.c -o rsc$(OBJ)

.c$(OBJ):
	$(CC) $(C_FLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@

# __________________________________________________________________
#  Include the makefile into the dependencies to force updates
#  when the makefile is modified.

$(OFILES): $(MAKEFILE)

# __________________________________________________________________
#  Targets from the GNU Regular Expression Library.

#regex$(OBJ): $(REGEX_DIR)$(DIR_SEP)regex.c $(MAKEFILE)
#	$(CC) $(C_FLAGS) -I$(REGEX_DIR) -I.. $(NON_ANSI_DEFS) $(REGEX_DIR)$(DIR_SEP)regex.c $(DONT_LINK) -o $@


bibtcl:
	cd BibTcl && $(MAKE) $(MFLAGS)

# -----------------------------------------------------------------------------
#  General development targets
# -----------------------------------------------------------------------------

CLEAN_TARGETS	= *$(OBJ) xref *.bak core #* *~ 

clean mostlyclean:
	-$(RM) $(CLEAN_TARGETS)
	-cd Doc && $(MAKE) $(MFLAGS) clean
	-cd BibTcl && $(MAKE) $(MFLAGS) clean

veryclean distclean realclean extraclean: clean
	-$(RM) bibtool config.cache config.status makefile

doc dvi: Doc$(DIR_SEP)config.tex
	cd Doc; $(MAKE) $(MFLAGS) DIR_SEP=$(DIR_SEP)

info:

test check:
	(cd Test; $(MAKE))

libbib.a: $(OLIBFILES)
	$(AR) $@ $(OLIBFILES)
	$(RANLIB) $@
#regex.o

depend:
	-$(MAKEDEPEND) -fmakefile -DMAKEDEPEND -Iinclude $(CFILES)

install: install.bin install.lib

install.bin install-exec: bibtool$(EXT)
	-$(INSTALL_DIR) $(BINDIR)
	$(INSTALL) bibtool$(EXT) $(BINDIR)

INSTALL_LIB_FILES = Lib$(DIR_SEP)*.*

install.lib install-data: 
	-$(INSTALL_DIR) $(LIBDIR)
	for lib in $(INSTALL_LIB_FILES); do	\
	  $(INSTALL_DATA) $$lib $(LIBDIR);	\
	done

INSTALL_INCLUDE_FILES = $(HFILES)

install.include install-include: 
	-$(INSTALL_DIR) $(INCLUDEDIR)
	for inc in $(INSTALL_INCLUDE_FILES); do	\
	  $(INSTALL_DATA) $$inc $(INCLUDEDIR);	\
	done

install.man install-man: 
	-$(INSTALL_DIR) $(MANDIR)$(DIR_SEP)man$(MANSECT)
	$(INSTALL_DATA) Doc$(DIR_SEP)bibtool.1 \
		$(MANDIR)$(DIR_SEP)man$(MANSECT)$(DIR_SEP)bibtool.$(MANSECT)

uninstall: uninstall.bin uninstall.lib

uninstall.bin uninstall-exec:
	-$(RM) $(BINDIR)$(DIR_SEP)bibtool$(EXT)

uninstall.lib uninstall-data:
	-$(RM) $(LIBDIR)

uninstall.include uninstall-include:
	-$(RM) $(INCLUDEDIR)

uninstall.man uninstall-man:
	-$(RM) $(MANDIR)$(DIR_SEP)man$(MANSECT)$(DIR_SEP)bibtool.$(MANSECT)

status:
	@echo $(LIBDIR)

# =============================================================================
# DO NOT DELETE THIS LINE -- make depend depends on it.
