#
# Makefile for G0FRD CW tutor for UNIX flavours
# Copyright (C) 1998  Simon Baldwin (simonb@sco.com)
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# 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.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# The following UNIX variants are available:
#
#   Linux: IBM PC Console running Linux.
#   SCO:   IBM PC Console running SCO OpenServer.
#   UW2:   IBM PC Console running SCO UnixWare 2.1.
#   UW7:   IBM PC Console running SCO UnixWare 7.
#

# Start of platform-specific information

# select the C compiler
#CC	= gcc	- for Linux
#CC	= cc	- for SCO OpenServer
#CC	= cc	- for SCO UnixWare 2.1
#CC	= cc	- for SCO UnixWare 7
CC	= gcc

# C compiler flags necessary to compile the programs correctly
#CFLAGS	= -DLINUX	- for Linux
#CFLAGS	= -DSCO -bELF	- for SCO OpenServer (optional -bELF)
#CFLAGS	= -DUNIXWARE2	- for SCO UnixWare 2.1
#CFLAGS	= -DUNIXWARE7	- for SCO UnixWare 7
CFLAGS	= -DLINUX

# curses library
#LCURSES	= -lncurses	- for Linux
#LCURSES	= -lcurses	- for SCO OpenServer
#LCURSES	= -lcurses	- for SCO UnixWare 2.1
#LCURSES	= -lcurses	- for SCO UnixWare 7
LCURSES	= -lncurses

#ICURSES	= -I/usr/include/ncurses	- for Linux
#ICURSES	= 				- for SCO OpenServer
#ICURSES	= 				- for SCO UnixWare 2.1
#ICURSES	= 				- for SCO UnixWare 7
ICURSES	= -I/usr/include/ncurses

# variable targets for platforms
#T_MAN	= linux_mans	- for Linux
#T_MAN	= nonlinux_mans	- for SCO OpenServer
#T_MAN	= nonlinux_mans	- for SCO UnixWare 2.1
#T_MAN	= nonlinux_mans	- for SCO UnixWare 7
T_MAN	= linux_mans

#T_INSTALL	= linux_install		- for Linux
#T_INSTALL	= nonlinux_install	- for SCO OpenServer
#T_INSTALL	= nonlinux_install	- for SCO UnixWare 2.1
#T_INSTALL	= nonlinux_install	- for SCO UnixWare 7
T_INSTALL	= linux_install

#T_UNINSTALL	= linux_uninstall	- for Linux
#T_UNINSTALL	= nonlinux_uninstall	- for SCO OpenServer
#T_UNINSTALL	= nonlinux_uninstall	- for SCO UnixWare 2.1
#T_UNINSTALL	= nonlinux_uninstall	- for SCO UnixWare 7
T_UNINSTALL	= linux_uninstall

# End of platform-specific information

# information for the installation
BINDIR	= /usr/local/bin
MANDIR	= /usr/local/man/man1
OWNER	= root:root
MODE	= 755


# default make target
default:	bins manpages install

# do everything
all:	bins manpages install test1 test2

# build the tutor binaries
bins:	cw cwgen cwcp

cw:	cw.h cw.c
	$(CC) $(CFLAGS) -o cw cw.c
	strip cw

cwgen:	cwgen.c
	$(CC) $(CFLAGS) -o cwgen cwgen.c
	strip cwgen

cwcp:	cw.h cwcp.c
	$(CC) $(CFLAGS) $(ICURSES) -o cwcp cwcp.c $(LCURSES)
	strip cwcp

# build manpages in different formats; useful for SCO where *roff
# is not available by default
manpages:	$(T_MAN)

linux_mans:	cw.1.ps cw.1.cat cwgen.1.ps cwgen.1.cat cwcp.1.ps cwcp.1.cat

cw.1.ps:	cw.1
	-man -t ./cw.1 >|cw.1.ps

cw.1.cat:	cw.1
	-man ./cw.1 >|cw.1.cat

cwgen.1.ps:	cwgen.1
	-man -t ./cwgen.1 >|cwgen.1.ps

cwgen.1.cat:	cwgen.1
	-man ./cwgen.1 >|cwgen.1.cat

cwcp.1.ps:	cwcp.1
	-man -t ./cwcp.1 >|cwcp.1.ps

cwcp.1.cat:	cwcp.1
	-man ./cwcp.1 >|cwcp.1.cat

nonlinux_mans:
	echo "Make for manpages not available on non-Linux platforms"

# install the binaries and man pages
install:	$(T_INSTALL)

linux_install:
	-mkdir -p $(BINDIR)
	-mkdir -p $(MANDIR)
	cp cw $(BINDIR)
	chown $(OWNER) $(BINDIR)/cw
	chmod $(MODE) $(BINDIR)/cw
	cp cwgen $(BINDIR)
	chown $(OWNER) $(BINDIR)/cwgen
	chmod $(MODE) $(BINDIR)/cwgen
	cp cwcp $(BINDIR)
	chown $(OWNER) $(BINDIR)/cwcp
	chmod $(MODE) $(BINDIR)/cwcp
	gzip -c cw.1 >$(MANDIR)/cw.1.gz
	chown $(OWNER) $(MANDIR)/cw.1.gz
	chmod $(MODE) $(MANDIR)/cw.1.gz
	gzip -c cwgen.1 >$(MANDIR)/cwgen.1.gz
	chown $(OWNER) $(MANDIR)/cwgen.1.gz
	chmod $(MODE) $(MANDIR)/cwgen.1.gz
	gzip -c cwcp.1 >$(MANDIR)/cwcp.1.gz
	chown $(OWNER) $(MANDIR)/cwcp.1.gz
	chmod $(MODE) $(MANDIR)/cwcp.1.gz

nonlinux_install:
	-mkdir -p $(BINDIR)
	cp cw $(BINDIR)
	chown $(OWNER) $(BINDIR)/cw
	chmod $(MODE) $(BINDIR)/cw
	cp cwgen $(BINDIR)
	chown $(OWNER) $(BINDIR)/cwgen
	chmod $(MODE) $(BINDIR)/cwgen
	cp cwcp $(BINDIR)
	chown $(OWNER) $(BINDIR)/cwcp
	chmod $(MODE) $(BINDIR)/cwcp

# run tests on the binaries built
test1:
	./cw -w25 <cwtest1.cw

test2:
	./cw <cwtest2.cw

# uninstall the binaries and man pages
uninstall:	$(T_UNINSTALL)

linux_uninstall:
	-rm -f $(BINDIR)/cw $(BINDIR)/cwgen $(BINDIR)/cwcp
	-rmdir $(BINDIR)
	-rm -f $(MANDIR)/cw.1.gz $(MANDIR)/cwgen.1.gz $(MANDIR)/cwcp.1.gz
	-rmdir $(MANDIR)

nonlinux_uninstall:
	-rm -f $(BINDIR)/cw $(BINDIR)/cwgen $(BINDIR)/cwcp
	-rmdir $(BINDIR)

# clear up for a rebuild
clean:
	-rm -f *.o cw cwgen cwcp *.1.ps *.1.cat

# completely remove
clobber: clean uninstall

