# @(#)Makefile
#
# Copyright 1995 - 2020  David A. Bagley, bagleyd AT verizon.net
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of the author not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# 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.

# Windows Makefile for Abacus

# DOS, UNIX & MKS
#CP = copy
#RM = del
#LN_S = cp

CP = cp -p
RM = rm -f
LN_S = ln -fs

# MinGW System Type Triplets
# most generic
#TRIPLET = i686-pc-mingw32
# on 64 bit building for 32 bit platforms
#TRIPLET = i686-w64-mingw32
# on 64 building for 64 bit platforms
TRIPLET = x86_64-w64-mingw32

# Set your C++ compiler
# Sun, SunC, Borland, Watcom, Microsoft, GNU
#CC = cc
#CC = acc
#CC = bcc
#CC = wcc
#CC = cl
#CC = CC
#CC = g++ -Wall -Woverloaded-virtual
#CC = gcc -Wall -ansi -pedantic
#GCC = gcc-3 -mno-cygwin
GCC = $(TRIPLET)-gcc
CC = $(GCC) -Wall -W -Wcast-qual -Wconversion -Wimplicit -Wmissing-declarations -Wunused -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wno-format -Wstrict-aliasing -Wnested-externs -Wmissing-prototypes -Wno-overlength-strings -Wdeclaration-after-statement -std=c89 -U__STRICT_ANSI__

# DEFAULT, Watcom
LD = $(CC)
#LD = wcl

# Microsoft, ?, Borland
#RC = rc
#RC = brc
#RC = brcc
#WINDRES = windres
WINDRES = $(TRIPLET)-windres
RC = $(WINDRES)

# Sun, SunC
LINT = lint

INDENT = indent

# DOS, UNIX, PostScript
#PRINT = print
PRINT = lpr
#PRINT = enscript -2r

N=
C=.c
#C=.cpp
#C=.cc

O=.o
#O=.obj
S=$(N) $(N)
E=.exe
#E=
A=

# please define
# C as the C source code extension
# O as the object extension
# S as the separator for object code
# E as the executable extension

.SUFFIXES:	$(C) $(O) $(E) .rc .res

wIDGET = abacus
WIDGET = Abacus
P=w
R=$(P)
V=$(P)
PROG = $(R)$(wIDGET)
VER = $(V)$(wIDGET)

READPATH = c:/WINDOWS
#READPATH = /WINDOWS

#WINVER = 0x030a
WINVER = 0x0950

##CHOME = c:\\bc\\
#CHOME = c:\bc\
#CHOME = c:/bc/
#CHOME = /usr/lib/w32api
#CHOME = /usr/$(TRIPLET)
CHOME = /usr/$(TRIPLET)/sys-root/mingw
#INCPATH = /usr/local/include
INCPATH = $(CHOME)/include
LIBPATH = $(CHOME)/lib
BINPATH = $(CHOME)/bin
#INCLUDES = -I$(INCPATH)
LIBRARIES = -lwinmm -lm
#XMLINCLUDES = -I$(INCPATH) -I/usr/include/libxml2
#XMLINCLUDES = -I$(INCPATH) -I$(INCPATH)/libxml2
XMLINCLUDES = -I$(INCPATH)/libxml2
XMLLIBRARIES = -L$(LIBPATH) -lxml2 -lz -llzma -liconv

HDRS = $(WIDGET)P.h $(WIDGET).h \
xwin.h file.h timer.h rngs.h sound.h version.h

COREOBJS = $(WIDGET)$(O)$(S)$(WIDGET)U$(O)$(S)$(WIDGET)C$(O)$(S)\
$(WIDGET)D$(O)$(S) $(WIDGET)M$(O)$(S)$(WIDGET)T$(O)$(S)$(WIDGET)F$(O)$(S)\
$(WIDGET)E$(O)
UTILOBJS = xwin$(O)$(S)file$(O)$(S)timer$(O)$(S)rngs$(O)$(S)sound$(O)
OBJS = $(COREOBJS)$(S)$(P)$(wIDGET)$(O)$(S)$(UTILOBJS)
XMLOBJS = $(COREOBJS)$(S)$(WIDGET)xmlE$(O)$(S)$(P)$(wIDGET)$(O)$(S)$(UTILOBJS)

CORESRCS = $(WIDGET)$(C) $(WIDGET)U$(C) $(WIDGET)C$(C) \
$(WIDGET)D$(C) $(WIDGET)M$(C) $(WIDGET)T$(C) $(WIDGET)F$(C) \
$(WIDGET)E$(C)
UTILSRCS = xwin$(C) file$(C) timer$(C) rngs$(C) sound$(C)
SRCS = $(UTILSRCS) $(CORESRCS) $(P)$(wIDGET)$(C)

TEXT = $(WIDGET).txt x$(wIDGET).htm

#OBJS = $(SRCS:$(C)=$(O))

DEFINES = -DWINVER=$(WINVER) -DHAVE_UNISTD_H \
-DSOUNDPATH=\"$(READPATH)\" -DSOUNDEXT=\".wav\" \
-DBUMPSOUND=\"bump\" -DMOVESOUND=\"move\" -DDRIPSOUND=\"drip\" \
-DDEMOPATH=\"$(DEMOPATH)\"

# If you get this error, you need to put dll libs in your path.
# wabacus.exe error while loading shared libraries: [? libml2-2.dll]:
# cannot open shared object file: No such file or directory
# Define for reading xml demo -DHAVE_LIBXML2

XMLDEFINES = -DHAVE_LIBXML2

# large memory model: Borland, Microsoft, UNIX
##COPT = -ml -WE -v -w4
#COPT = -AL -GA -Ow -W4 -Zp -Zi
#COPT = -mno-cygwin
COPT = -O2
#COPT = -g
#COPT = -g -DMONOTEST

# Borland, Microsoft, UNIX
#LOPT = -e
#LOPT = , /align:16, NUL, /nod llibcew libw oldnames,
#LOPT = -o
LOPT = -mwindows

CFLAGS = $(COPT) $(DEFINES) $(INCLUDES)
LDFLAGS = $(LOPT)
LIBS = $(LIBRARIES)
CXMLFLAGS = $(XMLDEFINES) $(XMLINCLUDES)
XMLLIBS = $(XMLLIBRARIES)

$(C)$(O):
	$(CC) -c $(CFLAGS) $<

.rc.res:
	$(RC) -r $(INCLUDES) $<

all:	$(PROG)$(E) $(TEXT)

$(WIDGET)xmlE$(O):	$(WIDGET)E$(C)
	$(CC) -c $(CFLAGS) $(CXMLFLAGS) $< -o $@

$(WIDGET)P.h:	../$(WIDGET)P.h
	$(LN_S) ../$(WIDGET)P.h $@

$(WIDGET).h:	../$(WIDGET).h
	$(LN_S) ../$(WIDGET).h $@

xwin.h:	../xwin.h
	$(LN_S) ../xwin.h $@

file.h:	../file.h
	$(LN_S) ../file.h $@

timer.h:	../timer.h
	$(LN_S) ../timer.h $@

rngs.h:	../rngs.h
	$(LN_S) ../rngs.h $@

sound.h:	../sound.h
	$(LN_S) ../sound.h $@

version.h:	../version.h
	$(LN_S) ../version.h $@

$(WIDGET)$(C):	../$(WIDGET)$(C)
	$(LN_S) ../$(WIDGET)$(C) $@

$(WIDGET)U$(C):	../$(WIDGET)U$(C)
	$(LN_S) ../$(WIDGET)U$(C) $@

$(WIDGET)C$(C):	../$(WIDGET)C$(C)
	$(LN_S) ../$(WIDGET)C$(C) $@

$(WIDGET)D$(C):	../$(WIDGET)D$(C)
	$(LN_S) ../$(WIDGET)D$(C) $@

$(WIDGET)M$(C):	../$(WIDGET)M$(C)
	$(LN_S) ../$(WIDGET)M$(C) $@

$(WIDGET)T$(C):	../$(WIDGET)T$(C)
	$(LN_S) ../$(WIDGET)T$(C) $@

$(WIDGET)F$(C):	../$(WIDGET)F$(C)
	$(LN_S) ../$(WIDGET)F$(C) $@

$(WIDGET)E$(C):	../$(WIDGET)E$(C)
	$(LN_S) ../$(WIDGET)E$(C) $@

xwin$(C):	../xwin$(C)
	$(LN_S) ../xwin$(C) $@

file$(C):	../file$(C)
	$(LN_S) ../file$(C) $@

timer$(C):	../timer$(C)
	$(LN_S) ../timer$(C) $@

rngs$(C):	../rngs$(C)
	$(LN_S) ../rngs$(C) $@

sound$(C):	../sound$(C)
	$(LN_S) ../sound$(C) $@

$(PROG)$(C):	../x$(wIDGET)$(C)
	$(LN_S) ../x$(wIDGET)$(C) $@

# cannot be a symbolic link
$(wIDGET)Demo.xml:	../$(wIDGET)Demo.xml
	$(CP) ../$(wIDGET)Demo.xml $@

$(WIDGET).txt:	version.h ../README ../TODO ../ChangeLog
	> $@
#	cat ../COPYING >> $@
#	echo "" >> $@
	grep VERSION version.h | cut -f2 -d\" >> $@
	echo "" >> $@
	sed s/^x$(wIDGET)/w$(wIDGET)/ ../README >> $@
	echo "" >> $@
	cat ../TODO >> $@
	echo "" >> $@
	echo "Updates:" >> $(WIDGET).txt
	cat ../ChangeLog >> $@
#	echo "" >> $@
#	cat ../NEWS >> $@

# This is for X-Windows but contains some general stuff too.
x$(wIDGET).htm:	../x$(wIDGET).html
	$(LN_S) ../x$(wIDGET).html $@

$(WIDGET)$(O):	$(HDRS)
$(WIDGET)U$(O):	$(WIDGET).h $(WIDGET)P.h
$(WIDGET)C$(O):	$(WIDGET).h $(WIDGET)P.h
$(WIDGET)D$(O):	$(WIDGET).h $(WIDGET)P.h file.h
$(WIDGET)M$(O):	$(WIDGET).h $(WIDGET)P.h
$(WIDGET)T$(O):	$(WIDGET).h $(WIDGET)P.h
$(WIDGET)F$(O):	$(WIDGET)P.h
$(WIDGET)E$(O):	$(WIDGET)P.h
xwin$(O):	xwin.h
file$(O):	file.h
timer$(O):	timer.h
rngs$(O):	rngs.h
sound$(O):	sound.h file.h
$(PROG)$(O):	$(WIDGET).h $(WIDGET)P.h xwin.h

$(wIDGET).ico:	../pixmaps/16x16/$(wIDGET).xpm ../pixmaps/32x32/$(wIDGET).xpm ../pixmaps/48x48/$(wIDGET).xpm
	LIST="";\
	for i in 16x16 32x32 48x48; do\
		xpmtoppm -alphaout $(wIDGET)_alpha_$$i.pgm\
		../pixmaps/$$i/$(wIDGET).xpm > $(wIDGET)_$$i.ppm;\
		LIST="$$LIST $(wIDGET)_$$i.ppm $(wIDGET)_alpha_$$i.pgm";\
	done;\
	ppmtowinicon -andpgms $$LIST > $@
	$(RM) *.ppm *.pgm

$(PROG).res:	$(PROG).rc $(WIDGET).h $(wIDGET).ico version.h
	$(RC) $< -O coff -o $@

#	$(RC) $(INCLUDES) $(PROG).rc

# libxml2 needed
all-xml:	$(XMLOBJS) $(PROG).res $(wIDGET)Demo.xml
	$(LD) $(COPT) $(LDFLAGS) $(XMLOBJS) $(PROG).res $(LIBS) $(XMLLIBS) -o $(PROG)$(E)
	@echo "$(BINPATH) in your path?"

$(PROG)$(E):	$(OBJS) $(PROG).res
	$(LD) $(COPT) $(LDFLAGS) $(OBJS) $(PROG).res $(LIBS) -o $@

##$(PROG)$(E):	$(OBJS) $(PROG).res $(PROG).def
# Borland, Microsoft, UNIX
#	$(LD) $(COPT) $(OBJS) $(LIBS)
#	$(LD) $(COPT) $(OBJS) $(LIBS) $(LOPT)$(PROG)$(E)
#	$(LD) $(LOPT)$(PROG)$(E) $(COPT) $(OBJS) $(LIBS)
#	$(RC) $(INCLUDES) $(PROG).rc

run:
	./$(PROG)

# runtime options are currently ignored in Windows, set in ini file

clean:
	$(RM) *.o *.exe* core *~ *% *.bak *.res *.obj *.lnk
	$(RM) *.sbr *.sbt *.pdb *.vcw *.wsp *.bsc

distclean:	clean
	$(RM) $(WIDGET)*$(C) $(WIDGET)*.h $(PROG)$(C) $(TEXT)
	$(RM) xwin$(C) file$(C) timer$(C) rngs$(C) sound$(C)
	$(RM) xwin.h file.h timer.h rngs.h sound.h version.h

small:	all
	strip --strip-all $(PROG)$(E); true
	upx $(PROG)$(E); true

install:	small
	$(CP) $(PROG)$(E) `cygpath -W`
	$(CP) $(PROG).ini `cygpath -W`

uninstall:
	$(RM) `cygpath -W`/$(PROG)$(E)
	$(RM) `cygpath -W`/$(PROG).ini

antic:
	antic *.h *.c

lint:	$(SRCS)
	$(LINT) -ax -DLINT -DWINVER $(DEFS) $(INCLUDES) -I.. $(SRCS) $(LIBRARIES)

ASCIIWINEXTEND=c h ini rc txt htm
DOSDIR=../../win/$(PROG)
# Handles both versions (Linux and SunOS) of unix2dos and dos2unix

dos2unix:
	for j in ${ASCIIWINEXTEND}; do\
		for k in *.$${j}; do\
			if [ -r $${k} ]; then\
				cp $${k} $${k}.old;\
				dos2unix $${k}.old $${k};\
				rm $${k}.old;\
			fi;\
		done;\
	done

unix2dos:
	for j in ${ASCIIWINEXTEND}; do\
		for k in *.$${j}; do\
			if [ -r $${k} ]; then\
				cp $${k} $${k}.old;\
				unix2dos $${k}.old $${k};\
				rm $${k}.old;\
			fi;\
		done;\
	done

dist:
	mkdir -p $(DOSDIR);\
	for j in $(ASCIIWINEXTEND); do\
		for k in *.$${j}; do\
			if [ -r $${k} ]; then\
				cp $${k} $(DOSDIR)/$${k}.old;\
				cp $${k} $(DOSDIR)/$${k};\
				unix2dos $(DOSDIR)/$${k}.old $(DOSDIR)/$${k};\
				rm $(DOSDIR)/$${k}.old;\
			fi;\
		done;\
	done;\
	for k in *.exe *.ico *.bmp *.wav; do\
		if [ -r $${k} ]; then\
			cp -p $${k} $(DOSDIR)/$${k};\
		fi;\
	done
