# MS WINDOWS-BASED ABACUS
#
#	Makefile
#
###
#
#  Copyright (c) 1995 - 2004	David Albert 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.

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

CP		= cp
RM		= rm -f
LN_S		= ln -s

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

# 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

# EXTENTIONS
CEXT		= .c
#CEXT		= .cpp
#CEXT		= .cc

OEXT		= .o
##OEXT		= .obj

EEXT		= .exe
#EEXT		=

.SUFFIXES:	$(CEXT) $(OEXT) $(EEXT) .rc .res

WIDGET		= Abacus
wIDGET		= abacus

PROGRAM		= w$(wIDGET)
VER		= w$(wIDGET)
STORE		= /dosa/$(PROGRAM)/
STORE2		= /dosc/$(PROGRAM)/

DEMOPATH	= /windows

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

HDRS		=\
		$(WIDGET)P.h\
		$(WIDGET).h\
		$(PROGRAM).h
SRCS		=\
		$(WIDGET)$(CEXT)\
		$(WIDGET)C$(CEXT)\
		$(WIDGET)D$(CEXT)\
		$(WIDGET)M$(CEXT)\
		$(PROGRAM)$(CEXT)
OBJS		=\
		$(WIDGET)$(OEXT)\
		$(WIDGET)C$(OEXT)\
		$(WIDGET)D$(OEXT)\
		$(WIDGET)M$(OEXT)\
		$(PROGRAM)$(OEXT)

LESSONS		=\
		$(WIDGET)1.les\
	       	$(WIDGET)2.les\
	       	$(WIDGET)3.les\
	       	$(WIDGET)4.les\
		$(WIDGET)jp1.les\
	       	$(WIDGET)jp2.les\
	       	$(WIDGET)jp3.les\
	       	$(WIDGET)jp4.les\
		$(WIDGET)ko1.les\
	       	$(WIDGET)ko2.les\
	       	$(WIDGET)ko3.les\
	       	$(WIDGET)ko4.les\
		$(WIDGET)ru1.les\
	       	$(WIDGET)ru2.les\
	       	$(WIDGET)ru3.les

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

#OBJS		= $(SRCS:$(CEXT)=$(OEXT))

#DEFINES	= -DDEMOPATH=\"$(DEMOPATH)\" -DWINVER=0x030a
DEFINES	= -DDEMOPATH=\"$(DEMOPATH)\" -DWINVER=0x0950
#DEFINES	= -DDEMOPATH=\"$(DEMOPATH)\"

# 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
#COPT		= -g

# 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)

$(CEXT)$(OEXT):
	$(CC) -c $(CFLAGS) $<

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

all:	$(PROGRAM)$(EEXT)

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

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

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

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

$(WIDGET)D$(CEXT):	../$(WIDGET)D$(CEXT)
	$(LN_S) ../$(WIDGET)D$(CEXT) $(WIDGET)D$(CEXT)

$(WIDGET)M$(CEXT):	../$(WIDGET)M$(CEXT)
	$(LN_S) ../$(WIDGET)M$(CEXT) $(WIDGET)M$(CEXT)

$(PROGRAM)$(CEXT):	../x$(wIDGET)$(CEXT)
	$(LN_S) ../x$(wIDGET)$(CEXT) $(PROGRAM)$(CEXT)

$(WIDGET)1.les:	../$(WIDGET)1.les
	$(LN_S) ../$(WIDGET)1.les $(WIDGET)1.les

$(WIDGET)2.les:	../$(WIDGET)2.les
	$(LN_S) ../$(WIDGET)2.les $(WIDGET)2.les

$(WIDGET)3.les:	../$(WIDGET)3.les
	$(LN_S) ../$(WIDGET)3.les $(WIDGET)3.les

$(WIDGET)4.les:	../$(WIDGET)4.les
	$(LN_S) ../$(WIDGET)4.les $(WIDGET)4.les

$(WIDGET)jp1.les:	../$(WIDGET)jp1.les
	$(LN_S) ../$(WIDGET)jp1.les $(WIDGET)jp1.les

$(WIDGET)jp2.les:	../$(WIDGET)jp2.les
	$(LN_S) ../$(WIDGET)jp2.les $(WIDGET)jp2.les

$(WIDGET)jp3.les:	../$(WIDGET)jp3.les
	$(LN_S) ../$(WIDGET)jp3.les $(WIDGET)jp3.les

$(WIDGET)jp4.les:	../$(WIDGET)jp4.les
	$(LN_S) ../$(WIDGET)jp4.les $(WIDGET)jp4.les

$(WIDGET)ko1.les:	../$(WIDGET)ko1.les
	$(LN_S) ../$(WIDGET)ko1.les $(WIDGET)ko1.les

$(WIDGET)ko2.les:	../$(WIDGET)ko2.les
	$(LN_S) ../$(WIDGET)ko2.les $(WIDGET)ko2.les

$(WIDGET)ko3.les:	../$(WIDGET)ko3.les
	$(LN_S) ../$(WIDGET)ko3.les $(WIDGET)ko3.les

$(WIDGET)ko4.les:	../$(WIDGET)ko4.les
	$(LN_S) ../$(WIDGET)ko4.les $(WIDGET)ko4.les

$(WIDGET)ru1.les:	../$(WIDGET)ru1.les
	$(LN_S) ../$(WIDGET)ru1.les $(WIDGET)ru1.les

$(WIDGET)ru2.les:	../$(WIDGET)ru2.les
	$(LN_S) ../$(WIDGET)ru2.les $(WIDGET)ru2.les

$(WIDGET)ru3.les:	../$(WIDGET)ru3.les
	$(LN_S) ../$(WIDGET)ru3.les $(WIDGET)ru3.les

$(WIDGET).txt:	../README
	$(LN_S) ../README $(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)$(OEXT):	$(WIDGET).h $(WIDGET)P.h
$(WIDGET)C$(OEXT):	$(WIDGET).h $(WIDGET)P.h
$(WIDGET)D$(OEXT):	$(WIDGET).h $(WIDGET)P.h
$(WIDGET)M$(OEXT):	$(WIDGET).h $(WIDGET)P.h
$(PROGRAM)$(OEXT):	$(WIDGET).h $(WIDGET)P.h $(PROGRAM).h

$(PROGRAM).res:	$(PROGRAM).rc $(PROGRAM).h
	$(RC) $< -O coff -o $@

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

$(PROGRAM)$(EEXT):	$(OBJS) $(PROGRAM).res $(LESSONS) $(TEXT)
	$(LD) $(COPT) $(LDFLAGS) $(OBJS) $(PROGRAM).res $(LIBS) -o $@

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

run:
	./$(PROGRAM)

clean:
	$(RM) *$(OEXT) $(PROGRAM)$(EEXT)* core *~ *% *.bak *.res *.obj
	$(RM) *.sbr *.sbt *.pdb *.vcw *.wsp *.bsc

distclean:	clean
	$(RM) $(WIDGET)*$(CEXT) $(WIDGET)*.h $(PROGRAM).c $(LESSONS) $(TEXT)

xpm2ico:
	xpm2wico -f ../icons/$(wIDGET).m.xpm w$(wIDGET).ico

ASCIIWINEXTEND=c h ini rc les txt htm
DOSDIR=../../win/$(PROGRAM)
# 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; do\
		if [ -r $${k} ]; then\
			cp -p $${k} $(DOSDIR)/$${k};\
		fi;\
	done
