# GNU makefile for Dos/Linux compilation.
# Customized by Michael W. Olson for easy building.

.PHONY: all debug test clean distclean dist export work web install
.PRECIOUS: %.o

#### General defs ####

# compiler:
CC         = gcc

# misc. flags for compiler:
CFLAGS     = -Wall

# optimization-oriented flags:
OPTIM      = -O2 -s

# programs to make:
PROGS      = hangit

# name of project:
PROJ       = hangit

# current version:
THIS_VER   = 0.6.0

# path to search through for sources:
VPATH      = src

# force shell to be sh:
SHELL      = /bin/sh

# Files to delete in all directories

JUNK     = \
.emacs.desktop \
.cvsignore \
betatest \
sfchanges \
sfrelease \
testlog

# Documentation

DOCS     = \
AUTHORS \
BUGS \
COPYING \
ChangeLog \
INSTALL \
NEWS \
README \
THANKS \
TODO

# Manual pages

MANS    = \
man/hangit.4

#### Target-specific deps ####

# Figure out the platform we are on

ifdef COMSPEC
SYSTEM   = dos
else
SYSTEM   = linux
endif

# Define target-specific vars

ifeq ($(SYSTEM),dos)
# flags passed to source code:
DFLAGS   = -D__DOS__

# executable suffix:
EXE      = .exe

# program to compress executables:
UPX      = upx

# dos -> unix conversion:
DTOU     = dtou

# unix -> dos conversion:
UTOD     = utod

# character for slash:
SL       = \\

# where to find libraries:
LFLAGS   = -L$(DJDIR)/contrib/pdcur24/lib

# libraries to use:
LIBS     = -lpdcurses

else
# Linux-specific options
LFLAGS   =
LIBS     = -lcurses -lm
DFLAGS   = -D__LINUX__
EXE      =
UPX      = echo Skipping exe compression for
#DTOU     = dos2unix
DTOU     = echo Skipping Dos->Unix conv
#UTOD     = unix2dos
UTOD     = echo Skipping Unix->Dos conv
SL       = /

# where to put installed files:
PREFIX   = /usr/local
endif

#### Special cases ####

# Debug options

debug: OPTIM  = -g
debug: UPX    = @echo Skipping exe compression for

# Test options

test: OPTIM   = -g
test: DFLAGS += -D__TEST__
test: UPX     = @echo Skipping exe compression for

#### Begin processing targets ####

# Default target

all: hangit

# Debug build

debug: extutil.o wordlist.o graphics.o game.o main.o
	$(CC) $(CFLAGS) $(OPTIM) $(LFLAGS) -o $@$(EXE) extutil.o wordlist.o graphics.o game.o main.o $(LIBS)
	$(UPX) $@$(EXE)

# Test build

test: extutil.o wordlist.o main.o
	$(CC) $(CFLAGS) $(OPTIM) $(LFLAGS) -o testit$(EXE) extutil.o wordlist.o main.o -lm
	$(UPX) $@$(EXE)

# Real build

hangit: extutil.o wordlist.o graphics.o game.o main.o
	$(CC) $(CFLAGS) $(OPTIM) $(LFLAGS) -o $@$(EXE) extutil.o wordlist.o graphics.o game.o main.o $(LIBS)
	$(UPX) $@$(EXE)

# Compilation

%.o: %.c
	$(CC) -c $(CFLAGS) $(OPTIM) $(DFLAGS) -o $@ $<

# Clean up the mess

clean:
	-rm -f *.o

# Get rid of changes and executables

distclean: clean
	-rm -f $(PROGS) $(PROGS).exe *~ src/*~ doc/*~ testit testit.exe

# Separate Linux src and dos src

export: clean
	-rm -f *~ src/*~ doc/*~
	-mkdir ../dist
	-rm -fr ../dist/$(PROJ)-$(THIS_VER)*
	mkdir ../dist/$(PROJ)-$(THIS_VER)linux
	cp -r * ../dist/$(PROJ)-$(THIS_VER)linux
	cd ..$(SL)dist$(SL)$(PROJ)-$(THIS_VER)linux && \
	rm -fr $(JUNK) $(PROGS).exe && \
	$(DTOU) src/* Makefile $(DOCS)
	cd $(CURDIR)
	mkdir ../dist/$(PROJ)-$(THIS_VER)dos
	cp -r * ../dist/$(PROJ)-$(THIS_VER)dos
	cd ..$(SL)dist$(SL)$(PROJ)-$(THIS_VER)dos && \
	rm -fr $(JUNK) $(PROGS) && \
	$(UTOD) src/* Makefile $(DOCS)
	cd $(CURDIR)

# Get ready for file distribution

dist: export
	-rm -fr ../dist/$(PROJ)
	cd ..$(SL)dist && \
	cp -r $(PROJ)-$(THIS_VER)dos $(PROJ) && \
	zip -r $(PROJ)-$(THIS_VER)dos.zip $(PROJ) && \
	rm -fr $(PROJ) && \
	cp -r $(PROJ)-$(THIS_VER)linux $(PROJ) && \
	tar -cvf $(PROJ)-$(THIS_VER)linux.tar $(PROJ) && \
	bzip2 -vzf $(PROJ)-$(THIS_VER)linux.tar
	cd .. && \
	tar -cvf work.tar $(PROJ) && \
	bzip2 -vzf work.tar && \
	mv -f work.tar.bz2 dist

# Get a quick image of working dir

work: clean
	-rm -fr *~ src/~ ../work.*
	cd .. ; \
	tar -cvf work.tar $(PROJ) && \
	bzip2 -vzf work.tar

# Convert project to current OS's standards

ifeq ($(SYSTEM),linux)
workin:
	$(DTOU) src/*.c src/*.h Makefile $(DOCS)
else
workin:
	$(UTOD) src/*.c src/*.h Makefile $(DOCS)
endif

# Convert project to other OS's standards

ifeq ($(SYSTEM),linux)
workout:
	$(UTOD) src/*.c src/*.h Makefile $(DOCS)
else
workout:
	$(DTOU) src/*.c src/*.h Makefile $(DOCS)
endif

# Update web page on sourceforge

web:
	scp doc/*.* bigmike160@hangit.sourceforge.net:/home/groups/h/ha/hangit/htdocs

# Install project

ifeq ($(SYSTEM),linux)
install:
	install -c -o root -g wheel -m 755 $(PROGS) $(PREFIX)/bin
	mkdir -p $(PREFIX)/share/$(PROJ)
	-rm -fr $(PREFIX)/share/$(PROJ)/*
	install -c -o root -g wheel -m 644 $(DOCS) $(PREFIX)/share/$(PROJ)
	install -c -o root -g wheel -m 644 $(MANS) $(PREFIX)/man/man4
else
install:
	echo No installation routine made for dos yet!
	echo Just put 'hangit.exe' somewhere and make a link to it.
endif
