/*
 * Copyright (c) 1995-2009, Paul Mattes.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Paul Mattes nor his contributors may be used
 *       to endorse or promote products derived from this software without
 *       specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  Imake template for x3270.
 *  @configure_input@
 */

       PROGRAMS = x3270 x3270if
LOCAL_LIBRARIES = $(XAWLIB) $(XMULIB) $(XTOOLLIB) $(XLIB)
          SRCS1 = Cme.c CmeBSB.c CmeLine.c CmplxMenu.c Husk.c about.c \
		  actions.c ansi.c apl.c charset.c child.c ctlr.c \
		  dialog.c display8.c ft.c ft_cut.c ft_dft.c host.c idle.c \
		  keymap.c keypad.c keysym2ucs.c kybd.c macros.c main.c \
		  menubar.c popups.c printer.c print.c proxy.c resolver.c \
		  resources.c rpq.c save.c screen.c scroll.c see.c select.c \
		  sf.c status.c tables.c toggles.c telnet.c trace_ds.c \
		  unicode.c unicode_dbcs.c utf8.c util.c xio.c
          VOBJS = Cme.o CmeBSB.o CmeLine.o CmplxMenu.o Husk.o about.o \
		  actions.o ansi.o apl.o charset.o child.o ctlr.o dialog.o \
		  display8.o @FALLBACKS_O@ ft.o ft_cut.o ft_dft.o host.o \
		  idle.o keymap.o keypad.o keysym2ucs.o kybd.o macros.o \
		  main.o menubar.o popups.o printer.o print.o proxy.o \
		  resolver.o resources.o rpq.o save.o screen.o scroll.o see.o \
		  select.o sf.o status.o tables.o telnet.o toggles.o \
		  trace_ds.o unicode.o unicode_dbcs.o utf8.o util.o xio.o \
		  @LIBOBJS@
          OBJS1 = $(VOBJS) version.o
          FONTS = FontObj(3270-12) FontObj(3270-12b) FontObj(3270-20) \
		  FontObj(3270-20b) FontObj(3270) FontObj(3270b) \
		  FontObj(3270gr) FontObj(3270gt12) \
		  FontObj(3270gt12b) FontObj(3270gt16) FontObj(3270gt16b) \
		  FontObj(3270gt24) FontObj(3270gt24b) FontObj(3270gt32) \
		  FontObj(3270gt32b) FontObj(3270gt8) FontObj(3270h)
      HOSTSFILE = ibm_hosts

/* Variables set by autoconf. */
    FONTINSTDIR = @IFONTDIR@
         prefix = @prefix@
    exec_prefix = @exec_prefix@
     sysconfdir = @sysconfdir@
autoconf_bindir = @bindir@
autoconf_mandir = @mandir@
    datarootdir = @datarootdir@
autoconf_fontinstdir = @CIFONTDIR@
    LIBX3270DIR = @LIBX3270DIR@
EXTRA_LDOPTIONS = @LDFLAGS@
EXTRA_LIBRARIES = @LIBS@
 EXTRA_INCLUDES = @CPPFLAGS@
             CC = @CC@
        INSTALL = @INSTALL@
@NIXCCOPTIONS@

/* Add HP/UX #define. */
#ifdef HPArchitecture
HP_DEFINE = -D_HPUX_SOURCE
#endif

/* Add Cygwin hack, where Xaw depends on Xpm. */
#ifdef cygwinArchitecture
EXTRA_LOAD_FLAGS=-lXpm
#endif

/* ProgramTargetName isn't defined in R5. */
#if !defined(ProgramTargetName)
#define ProgramTargetName(x)x
#endif

/*
 * Pass -I/usr/local/include to makedepend.  This very unpleasant hack is
 * needed in case one or more header files is in /usr/local/include -- gcc
 * sometimes includes this by default, and complains when it appears in an
 * explicit -I option, but makedepend does not.
 */
DEPENDFLAGS = -I/usr/local/include

/* Main targets: x3270 and x3270if. */
AllTarget($(FONTS) ProgramTargetName(x3270) ProgramTargetName(x3270if))

/* Build/install/man target for x3720.  Requires extra libraries. */
ComplexProgramTarget_1(x3270,,$(LOCAL_LIBRARIES))

/* Fonts. */
FontTarget(3270-12)
FontTarget(3270-12b)
FontTarget(3270-20)
FontTarget(3270-20b)
FontTarget(3270)
FontTarget(3270b)
FontTarget(3270gr)
FontTarget(3270gt12)
FontTarget(3270gt12b)
FontTarget(3270gt16)
FontTarget(3270gt16b)
FontTarget(3270gt24)
FontTarget(3270gt24b)
FontTarget(3270gt32)
FontTarget(3270gt32b)
FontTarget(3270gt8)
FontTarget(3270h)
InstallMultipleFlags($(FONTS),$(FONTINSTDIR),$(INSTDATFLAGS))

/* Extra directories. */
MakeDirectories(install,$(LIBX3270DIR))
MakeDirectories(install,$(autoconf_bindir))
MakeDirectories(install.man,$(autoconf_mandir)/man1)
MakeDirectories(install.man,$(autoconf_mandir)/man5)
MakeDirectories(install.byprefix,$(LIBX3270DIR))
MakeDirectories(install.byprefix,$(autoconf_bindir))
MakeDirectories(install.byprefix,$(autoconf_mandir)/man1)
MakeDirectories(install.byprefix,$(autoconf_mandir)/man5)
MakeDirectories(install.byprefix,$(autoconf_fontinstdir))

/* Non-default build rules for certain C modules. */
version.o: $(VOBJS) version.txt mkversion.sh
	@chmod +x mkversion.sh version.txt
	sh ./mkversion.sh $(CC)

fallbacks.c: mkfb X3270.xad
	$(RM) $@
	./mkfb X3270.xad $@

mkfb: mkfb.c
	$(CC) $(CFLAGS) -o mkfb mkfb.c

/* Build rule for x3270if.  This is specified explicitly instead of using
   ComplexProgramTarget, because we may want to install it somewhere other
   than under PROJECTROOT. */
ProgramTargetName(x3270if): x3270if.c conf.h
	$(CC) $(CFLAGS) -o x3270if x3270if.c $(EXTRA_LDOPTIONS) $(EXTRA_LIBRARIES)

/* Rules for using and installing a separate app-defaults file. */
@DEFINE_UAD@
#ifdef USE_APP_DEFAULTS
AD_DEFINE = -DUSE_APP_DEFAULTS
InstallAppDefaults(X3270)
all:: X3270.ad
X3270.ad: X3270.xad qcpp conf.h qcpp-defs.sh
	$(RM) $@
	./qcpp @UAD_FLAGS@ `./qcpp-defs.sh` X3270.xad $@
qcpp: qcpp.c
	$(CC) $(CFLAGS) -o $@ qcpp.c
#endif

/* Combined EXTRA_DEFINES definition. */
EXTRA_DEFINES = $(HP_DEFINE) $(AD_DEFINE) @XANSI@ -DLIBX3270DIR=\"$(LIBX3270DIR)\"

/******************************************************************************
 * Install rules.                                                             *
 ******************************************************************************/

/* Install rule for x3270if. */
install:: ProgramTargetName(x3270if)
	$(INSTALL) $(INSTALLFLAGS) $(INSTPGMFLAGS) ProgramTargetName(x3270if) $(DESTDIR)$(autoconf_bindir)/ProgramTargetName(x3270if)

/* Extra install rule to regenerate fonts.dir. */
install:: $(DESTDIR)$(FONTINSTDIR)/fonts.dir
$(DESTDIR)$(FONTINSTDIR)/fonts.dir::
	-chmod u+w $(DESTDIR)$(FONTINSTDIR) $(DESTDIR)$(FONTINSTDIR)/fonts.dir
	$(MKFONTDIR) $(DESTDIR)$(FONTINSTDIR)
	chmod u=rwx,go=rx $(DESTDIR)$(FONTINSTDIR)
	chmod a=r $(DESTDIR)$(FONTINSTDIR)/fonts.dir

/* Extra install rule for the sample ibm_hosts file (runs only if there is
   no ibm_hosts file installed yet). */
install:: $(HOSTSFILE)
	@[ -r $(DESTDIR)$(LIBX3270DIR)/$(HOSTSFILE) ] || \
		$(INSTALL) -c $(INSTDATFLAGS) $(HOSTSFILE) $(DESTDIR)$(LIBX3270DIR)/$(HOSTSFILE)

/*
 * Install rules for x3270if, x3270-script and ibm_hosts man pages.
 * These are specified explicitly, instead of using the standard Imake macros,
 * because we may want to install in a directory other than PROJECTROOT.
 */
install.man:: x3270if.man x3270-script.man ibm_hosts.man
	$(INSTALL) -c $(INSTMANFLAGS) x3270if.man $(DESTDIR)$(autoconf_mandir)/man1/x3270if.1
	$(INSTALL) -c $(INSTMANFLAGS) x3270-script.man $(DESTDIR)$(autoconf_mandir)/man1/x3270-script.1
	$(INSTALL) -c $(INSTMANFLAGS) ibm_hosts.man $(DESTDIR)$(autoconf_mandir)/man5/ibm_hosts.5

XCOMM Rules for installing everything in $(PREFIX), rather than in the system
XCOMM X11 directory.  This is hideous, but I see no way around it.
install.byprefix::
	$(INSTALL) -c $(INSTBINFLAGS) ProgramTargetName(x3270) $(DESTDIR)$(autoconf_bindir)/ProgramTargetName(x3270)
#	$(INSTALL) -c $(INSTBINFLAGS) ProgramTargetName(x3270hist.pl) $(DESTDIR)$(autoconf_bindir)/ProgramTargetName(x3270hist.pl)
	$(INSTALL) -c $(INSTBINFLAGS) ProgramTargetName(x3270if) $(DESTDIR)$(autoconf_bindir)/ProgramTargetName(x3270if)
	$(INSTALL) -c $(INSTMANFLAGS) x3270.man $(DESTDIR)$(autoconf_mandir)/man1/x3270.1
	$(INSTALL) -c $(INSTMANFLAGS) x3270if.man $(DESTDIR)$(autoconf_mandir)/man1/x3270if.1
	$(INSTALL) -c $(INSTMANFLAGS) x3270-script.man $(DESTDIR)$(autoconf_mandir)/man1/x3270-script.1
	$(INSTALL) -c $(INSTMANFLAGS) ibm_hosts.man $(DESTDIR)$(autoconf_mandir)/man5/ibm_hosts.5
	@for i in $(FONTS); \
	do	(set -x; $(INSTALL) -c $(INSTDATFLAGS) $$i $(DESTDIR)$(autoconf_fontinstdir)/$$i); \
	done
	-chmod u+w $(DESTDIR)$(autoconf_fontinstdir) $(DESTDIR)$(autoconf_fontinstdir)/fonts.dir
	$(MKFONTDIR) $(DESTDIR)$(autoconf_fontinstdir)
	chmod u=rwx,go=rx $(DESTDIR)$(autoconf_fontinstdir)
	chmod a=r $(DESTDIR)$(autoconf_fontinstdir)/fonts.dir
	@[ -r $(DESTDIR)$(LIBX3270DIR)/$(HOSTSFILE) ] || \
		(set -x; $(INSTALL) -c $(INSTDATFLAGS) $(HOSTSFILE) $(DESTDIR)$(LIBX3270DIR)/$(HOSTSFILE))

/******************************************************************************
 * Clean rules.                                                               *
 ******************************************************************************/
clean:: 
	$(RM) $(FONTS) qcpp mkfb fallbacks.c
