# x3270 Makefile for Sun OpenWindows 3.2 (SunOS 4.1.x) or earlier ONLY
# (for newer versions of OpenWindows, use xmkmf)

# NOTE: This makefile must be run with Sun make -- Gnu make will not work!

# Solaris conditional definitions
XSVR4 = if [ `uname -r | cut -c1` -eq 5 ]; then echo -DSVR4; fi
XLIBS = if [ `uname -r | cut -c1` -eq 5 ]; then echo -lnsl -lsocket; fi

OBJECTS = Cme.o CmeBSB.o CmeLine.o CmplxMenu.o about.o actions.o ansi.o apl.o \
	ctlr.o fallbacks.o ft.o ft_cut.o ft_dft.o keypad.o kybd.o macros.o \
	main.o menubar.o popups.o print.o save.o screen.o scroll.o select.o \
	sf.o tables.o status.o telnet.o trace_ds.o util.o
FONTS = 3270.fb 3270b.fb 3270-12.fb 3270-12b.fb 3270-20.fb 3270-20b.fb \
	3270d.fb 3270h.fb 3270gt8.fb 3270gt12.fb 3270gt12b.fb 3270gt16.fb \
	3270gt16b.fb 3270gt24.fb 3270gt24b.fb 3270gt32.fb 3270gt32b.fb
OPENWINHOME=/usr/openwin
LIBX3270DIR = $(INSTDIR)/lib/x3270
HOSTSFILE = ibm_hosts
DEFINES = -DLIBX3270DIR=\"$(LIBX3270DIR)\"

# For debugging, set OPTFLAGS to -g
OPTFLAGS = -O

all: right_os x3270 x3270if fonts

right_os:
	@s=`uname -s``uname -r`; \
	if [ "`echo $$s | sed 's/\..*//'" != SunOS4 ]; \
	then	echo "'make -f Makefile.openwin' is for SunOS 4.1.x only --"; \
		echo "Use 'xmkmf' and 'make' instead."; \
		false; \
	fi

# To use a separate app-defaults file rather than compiling it into x3270,
# uncomment the following two lines:
#EXTRA_DEFINES = -DUSE_APP_DEFAULTS
#all: X3270.ad

# Rule for building the separate app-defaults file.  By default it builds
# color app-defaults; to change to monochrome app-defaults, uncomment
# the second "AD_OPTS=" line below.
AD_OPTS=-DCOLOR
# AD_OPTS=-UCOLOR
X3270.ad: X3270.xad qcpp
	$(RM) $@
	qcpp $(AD_OPTS) X3270.xad >$@
qcpp: qcpp.c
	$(CC) -o $@ qcpp.c

CFLAGS = $(OPTFLAGS) $(EXTRA_DEFINES) -I$(OPENWINHOME)/include -I./owinc \
	$(XSVR4:sh) $(DEFINES)

.bdf.fb:
	$(OPENWINHOME)/bin/convertfont -d. -o `basename $@ .fb` $<
.SUFFIXES: .bdf .fb $(SUFFIXES)

LIBS = -lXaw -lXmu -lXt -lXext -lX11 -lm
# If you get the following ld error:
#
#     ld: Undefined symbol
#       _get_wmShellWidgetClass
#       _get_applicationShellWidgetClass
#
# then you need to get patches 100512-02 and 100573-03 from Sun.  In the
# meantime, use the following LIBS definition as a workaround:
#LIBS = -lXaw -Bstatic -lXmu -Bdynamic -lXt -lXext -lX11 -lm

fonts: Families.list

version.o: $(OBJECTS) version.txt mkversion.sh
	@chmod +x mkversion.sh version.txt
	sh ./mkversion.sh $(CC)

fallbacks.c: mkfb X3270.xad
	./mkfb X3270.xad >fallbacks.c

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

x3270: $(OBJECTS) version.o
	$(CC) $(OPTFLAGS) -o x3270 $(OBJECTS) version.o -L$(OPENWINHOME)/lib \
		$(LIBS) $(XLIBS:sh)

x3270if: x3270if.o
	$(CC) $(OPTFLAGS) -o x3270if x3270if.o

Families.list: $(FONTS)
	touch Compat.list Synonyms.list
	$(OPENWINHOME)/bin/bldfamily -d .

# Install x3270 under OpenWindows.  Puts everything in $OPENWINHOME, unless you
# change the value of INSTDIR below.
INSTDIR=$(OPENWINHOME)
install: x3270 install.fonts
	install -m 755 x3270 $(INSTDIR)/bin
	install -m 644 x3270.man $(INSTDIR)/man/man1/x3270.1
	install -m 644 x3270-script.man $(INSTDIR)/man/man1/x3270-script.1
	install -m 644 ibm_hosts.man $(INSTDIR)/man/man5/ibm_hosts.5
	[ -d $(LIBX3270DIR) ] || mkdir $(LIBX3270DIR)
	[ -r $(LIBX3270DIR)/$(HOSTSFILE) ] || \
		install -m 644 $(HOSTSFILE) $(LIBX3270DIR)/$(HOSTSFILE)

install.fonts: fonts
	install -m 644 $(FONTS) $(INSTDIR)/lib/fonts
	$(OPENWINHOME)/bin/bldfamily -d $(INSTDIR)/lib/fonts
	chmod 644 $(INSTDIR)/lib/fonts/Families.list
	-$(OPENWINHOME)/bin/xset fp rehash

# Run this only if you want to run a separate app-defaults file.
install.app-defaults:
	install -m 644 X3270.ad $(INSTDIR)/lib/app-defaults/X3270

clean:
	$(RM) -f x3270 $(OBJECTS) version.o $(FONTS) mkfb fallbacks.c X3270.ad
