/*
 *                            COPYRIGHT
 *
 *  PCB, interactive printed circuit board design
 *  Copyright (C) 1994,1995 Thomas Nau
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Contact addresses for paper mail and Email:
 *  Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
 *  Thomas.Nau@rz.uni-ulm.de
 *
 *  RCS: $Header: /sda4/users/nau/src/pcb/RCS/Imakefile,v 2.5 1994/10/29 17:29:04 nau Exp $
 */

/* ------------------- start of user configuration ---------------------------
 *
 *   INFODIR:     where to install the GNU-info files
 *   PCBLIBDIR:   where to install fonts...
 *   CIRCUITDIR:  where to install circuits...
 *   PACKAGEDIR:  where to install packages...
 * it is set, for example, to /usr/lib/X11/pcb (LIBDIR is defined by imake)
 *
 */
INFODIR = /usr/local/info
PCBLIBDIR = $(LIBDIR)/pcb
CIRCUITDIR = $(PCBLIBDIR)/circuits
PACKAGEDIR = $(PCBLIBDIR)/packages

/* ----------------------------------------------------------------------
 * BTNMOD determies the modifier key to be used for the
 * function key and buttons in the default translations.
 * The default, <mod1>, works fine if you do not use the HP-VUE
 * window manager which uses <mod1> itself. It's save to use <ctrl> instead.
 */
BTNMOD = Mod1

/* ----------------------------------------------------------------------
 * You will probably need an additional searchpath for include files
 * if you use OpenWindows
 */
/* EXTRA_INCLUDES  = -I/usr/openwin/include */

/* ----------------------------------------------------------------------
 * Check the following section of architecture dependend settings.
 * If there's a #ifdef - #endif pair for your machine fine; if
 * not you may create a new one (and mail it to the author too if
 * it works) or try the default. Here's some more information:
 *
 * if your system supports atexit() define EXITCALL as -DHAS_ATEXIT
 * if your system has on_exit() but no atexit() like suns with BSD
 * define it as -DHAS_ON_EXIT
 *
 * setup the system libraries needed for lex (flex), also add -lm
 *
 * a symbol PATCHES is used to pass additional information to
 * the compiler. Additional options are:
 *   -DNEED_STRDUP       if your system doesn't have strdup()
 */
/* PATCHES = -DNEED_STRDUP */

/* ----------------------------------------------------------------------
 * some default values which can be changed in the architecture
 * depending section
 */
SYS_LIBRARIES = -ll -lm         /* these are the default libraries */ 
#ifdef SYSV                     /* System V has atexit() */
      EXITCALL = -DHAS_ATEXIT
#endif

/* ----------------------------------------------------------------------
 * the architecture depending section
 */
#ifdef AnsiDefines              /* some ANSI CC need additional defines */
     EXTRA_DEFINES  = AnsiDefines
#endif

#ifdef LinuxArchitecture        /* LINUX */
      SYS_LIBRARIES = -lfl -lm
      EXITCALL = -DHAS_ATEXIT
#endif

#ifdef SGIArchitecture          /* Silicon Graphics */
      CCOPTIONS = -D__STDC__    /* thanks to Bernd Leibing */
      PROTO_DEFINES =           /* Bernd.Leibing@e-technik.uni-ulm.de */
      EXITCALL = -DHAS_ATEXIT
#endif

#ifdef HPArchitecture           /* HP */
      EXITCALL = -DHAS_ATEXIT
#endif

#ifdef SunArchitecture          /* Sun */
#if OSMajorVersion < 5          /* BSD */
      EXITCALL = -DHAS_ON_EXIT
#else                           /* Solaris */
      EXITCALL = -DHAS_ATEXIT
#endif
#endif

#ifdef ArmArchitecture          /* Acorn RISCiX, BSD libraries */
      EXITCALL = -DHAS_ATEXIT   /* thanks to Adrian Godwin */
                                /* agodwin@acorn.co.uk */
                                /* the BSD environment does not define M_PI */
      PATCHES = -DNEED_STRDUP
      CC = /usr/ucb/cc
#endif

/* ------------------- end of user configuration ------------------------- */

RELEASE = 1.3

COMPRESS = gzip
SUFFIX   = gz

DEFINES = -DRELEASE=\"$(RELEASE)\" $(EXITCALL) \
          -DCIRCUITDIR=\"$(CIRCUITDIR)\" \
          -DPACKAGEDIR=\"$(PACKAGEDIR)\" \
          -DPCBLIBDIR=\"$(PCBLIBDIR)\" \
          -DBTNMOD=\"$(BTNMOD)\" \
          $(PATCHES)

PROGRAMS = pcb

SRCS = parse_y.c parse_l.c \
 action.c buffer.c change.c command.c control.c copy.c create.c crosshair.c \
 data.c dialog.c draw.c error.c file.c fileselect.c find.c log.c \
 main.c mymem.c menu.c mirror.c misc.c move.c \
 pinout.c polygon.c print.c printdialog.c printpanner.c \
 remove.c rotate.c search.c select.c set.c sizedialog.c undo.c

OBJS = parse_y.o parse_l.o \
 action.o buffer.o change.o command.o control.o copy.o create.o crosshair.o \
 data.o dialog.o draw.o error.o file.o fileselect.o find.o log.o \
 main.o mymem.o menu.o mirror.o misc.o move.o \
 pinout.o polygon.o print.o printdialog.o printpanner.o \
 remove.o rotate.o search.o select.o set.o sizedialog.o undo.o

FONTS = default_font

ADDITIONALDIR =   circuits packages README_FILES doc

DEPLIBS =         $(DEPXAWLIB) $(DEPXTOOLLIB) $(DEPEXTENSIONLIB) $(DEPXLIB) 
LOCAL_LIBRARIES = $(XAWLIB) $(XMULIB) $(XTOOLLIB) $(XLIB) 

/*
 * add
 *   all::
 *   depend::
 * rules to make sure that the parser files have been generated before
 * the dependencies are checked
 */
all:: pcb Pcb.ad pcb.man

depend:: parse_l.c parse_y.c

ComplexProgramTarget(pcb)
InstallManPage(pcb,$(MANDIR))
InstallAppDefaults(Pcb)
InstallMultipleFlags($(FONTS), $(PCBLIBDIR), $(INSTLIBFLAGS))
InstallMultipleDestFlags(install.info, pcb.info, $(INFODIR), $(INSTLIBFLAGS))

install::
	MakeDir($(CIRCUITDIR))
	@case '${MFLAGS}' in *[i]*) set +e;; esac; 	\
	for i in `ls circuits` ; do 				\
		(set -x; $(INSTALL) -c $(INSTDATFLAGS) circuits/$$i $(CIRCUITDIR));	\
	done
	MakeDir($(PACKAGEDIR))
	@case '${MFLAGS}' in *[i]*) set +e;; esac; 	\
	for i in `ls packages` ; do 				\
		(set -x; $(INSTALL) -c $(INSTDATFLAGS) packages/$$i $(PACKAGEDIR));	\
	done

sed.script: Imakefile create_sed_script.sh const.h
	@echo "creating sed-script from defines"
	@./create_sed_script.sh $(DEFINES) Imakefile > $@

pcb.man: pcb.man.raw sed.script
	@echo "creating manual from raw file"
	@sed -f sed.script  < pcb.man.raw > $@

Pcb.ad: Pcb.ad.raw sed.script
	@echo "creating application default resource from raw file"
	@sed -f sed.script  < Pcb.ad.raw > $@

pcb.texi: pcb.texi.raw sed.script
	@echo "creating pcb.texi from raw file"
	@sed -f sed.script  < pcb.texi.raw > $@

pcb.info: pcb.texi
	@echo "creating pcb.info file"
	@makeinfo --no-split pcb.texi

pcb.dvi: pcb.texi
	@echo "creating pcb.dvi file"
	@tex pcb.texi; texindex pcb.fn pcb.vr pcb.cp; tex pcb.texi; tex pcb.texi

pcb.ps: pcb.dvi
	@echo "creating $@ file"
	@dvips -D 300 -o $@ pcb.dvi

refcard.tex: refcard.tex.raw sed.script
	@echo "creating refcard.tex from raw file"
	@sed -f sed.script  < refcard.tex.raw > $@

refcard.dvi: refcard.tex
	@echo "creating refcard.dvi file"
	@latex refcard.tex

refcard.ps: refcard.dvi
	@echo "creating $@ file"
	@dvips -D 300 -o $@ refcard.dvi

parse_y.h parse_y.c: parse_y.y global.h
	$(YACC) -d parse_y.y
	$(MV) y.tab.c parse_y.c
	$(MV) y.tab.h parse_y.h

parse_l.c: parse_l.l parse_y.h global.h
	$(LEX) parse_l.l
	$(MV) lex.yy.c parse_l.c

default-doc::
	make refcard.ps
	$(COMPRESS) < refcard.ps > doc/refcard.ps.$(SUFFIX)
	make pcb.ps
	$(COMPRESS) < pcb.ps > doc/pcb.ps.$(SUFFIX)

new::
	make clean
	make

clean-doc::
	$(RM) pcb.info pcb.dvi pcb.texi pcb.ps \
		refcard.ps refcard.dvi refcard.tex \
		pcb.man; \
	for f in refcard pcb; do \
		$(RM) $$f.log $$f.aux $$f.toc $$f.cps $$f.fns $$f.vrs \
		      $$f.cp $$f.fn $$f.ky $$f.pg $$f.tp $$f.vr; \
	done;

clean::
	make clean-doc
	$(RM) parse_y.h parse_y.c parse_l.c sed.script Pcb.ad

distclean::
	make clean
	$(RM) Makefile

dist::
	make distclean
	@dir=/tmp/pcb-$(RELEASE); \
		echo "creating public release `basename $$dir`, please be patient"; \
		if [ -d $$dir ] ; then \
			echo "can't create distribution, $$dir exists" >&2; exit 1; fi; \
		mkdir $$dir; \
		cp -p `find . -maxdepth 1 -type f -print` $$dir; \
		cp -r $(ADDITIONALDIR) $$dir; \
		(cd $$dir; make distclean); \
		cd /tmp; \
		prog=`basename $(COMPRESS)`; \
		target=$$dir.tar.$(SUFFIX); \
		tar cf - `basename $$dir` | $(COMPRESS) > $$target; \
		rm -rf $$dir; \
		echo "$$target is ready to be shipped"
