# @(#)Makefile
#
# Copyright 1995 - 2012  David A. Bagley, bagleyd@tux.org
#
# 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 Hexagons

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

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

# 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
CC = gcc-3 -Wall -W -Wcast-qual -Wconversion -Wimplicit -Wmissing-declarations -Wunused -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wstrict-prototypes -Wno-format -Wstrict-aliasing

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

# Microsoft, ?, Borland
#RC = rc
#RC = brc
#RC = brcc
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 = hexagons
WIDGET = Hexagons
P=w
R=$(P)
V=$(P)
PROG = $(R)$(wIDGET)
VER = $(V)$(wIDGET)
STORE = /dosa/$(PROG)/
STORE2 = /dosc/$(PROG)/

READPATH = c:/WINDOWS
#READPATH = /WINDOWS

#WINVER = 0x030a
WINVER = 0x0950

#DATAFILE = ./$(PROG).data
DATAFILE = $(PROG).ini
SCOREFILE = $(wIDGET).scores
LOGFILE = $(wIDGET).log

##CHOME = c:\\bc\\
#CHOME = c:\bc\
#CHOME = c:/bc/
#CHOME =
CHOME = /usr/lib/w32api
##INCPATH = $(CHOME)include
INCPATH = /usr/local/include
#LIBPATH = $(CHOME)lib
LIBPATH = $(CHOME)
INCLUDES = -I$(INCPATH)
LIBRARIES = -L$(LIBPATH) -lwinmm

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)
OBJS = xwin$(O)$(S)file$(O)$(S)timer$(O)$(S)rngs$(O)$(S)sound$(O)$(S)\
$(COREOBJS)$(S)$(P)$(wIDGET)$(O)

CORESRCS = $(WIDGET)$(C) $(WIDGET)U$(C)
SRCS = xwin$(C) file$(C) timer$(C) rngs$(C) sound$(C) \
$(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\" \
-DDATAFILE=\"$(DATAFILE)\" \
-DSCOREFILE=\"$(SCOREFILE)\" -DLOGFILE=\"$(LOGFILE)\"

# large memory model: Borland, Microsoft, UNIX
##COPT = -ml -WE -v -w4
#COPT = -AL -GA -Ow -W4 -Zp -Zi
COPT = -O2 -mno-cygwin
#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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

$(wIDGET).scores:	../$(wIDGET).scores
	$(CP) ../$(wIDGET).scores $(wIDGET).scores

$(WIDGET)$(O):	$(HDRS)
$(WIDGET)U$(O):	$(WIDGET).h $(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/$(wIDGET).t.xpm ../pixmaps/$(wIDGET).m.xpm ../pixmaps/$(wIDGET).l.xpm
	xpmtoppm -alphaout $(wIDGET)_alpha.t.pgm ../pixmaps/$(wIDGET).t.xpm >\
		$(wIDGET).t.ppm
	xpmtoppm -alphaout $(wIDGET)_alpha.m.pgm ../pixmaps/$(wIDGET).m.xpm >\
		$(wIDGET).m.ppm
	xpmtoppm -alphaout $(wIDGET)_alpha.l.pgm ../pixmaps/$(wIDGET).l.xpm >\
		$(wIDGET).l.ppm
	ppmtowinicon -andpgms\
		$(wIDGET).t.ppm $(wIDGET)_alpha.t.pgm\
		$(wIDGET).m.ppm $(wIDGET)_alpha.m.pgm\
		$(wIDGET).l.ppm $(wIDGET)_alpha.l.pgm\
		> $(wIDGET).ico
	$(RM) *.ppm *.pgm

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

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

$(PROG)$(E):	$(OBJS) $(PROG).res $(wIDGET).scores
	$(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:	$(wIDGET).scores
	./$(PROG)

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
	$(RM) $(wIDGET).scores

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 scores
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
