# 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
# program to make
PROG       = hangit
# name of project
PROJ       = hangit
# current version
THIS_VER   = 0.7.0
# path to search through for sources
VPATH      = src
# force shell to be sh
SHELL      = /bin/sh
# current directory
CURDIR     = $(shell pwd)
# Files to delete in all directories
JUNK     = \
.emacs.desktop \
.cvsignore \
CVS \
doc/CVS \
man/CVS \
src/CVS \
betatest \
sfchanges \
sfrelease \
testlog
# Documentation
DOCS     = *.txt
# Manual pages
MANS    = \
man/hangit.4

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

# Figure out the platform we are on

ifdef COMSPEC
SYSTEM   = dos
else
ifeq ($(CROSS),y)
SYSTEM   = cross
else
SYSTEM   = linux
endif
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
ifeq ($(SYSTEM),cross)
# DOS binary on a Linux system
CC       = dos-gcc
DFLAGS   = -D__DOS__
EXE      = .exe
UPX      = upx
DTOU     = recode -f MSDOS..Latin-1
UTOD     = recode -f Latin-1..MSDOS
SL       = /
LFLAGS	 =
LIBS     = -lpdcurses
else
# Linux-specific options
DFLAGS   = -D__LINUX__
EXE      =
UPX      = : Skipping exe compression for
DTOU     = recode -f MSDOS..Latin-1
UTOD     = recode -f Latin-1..MSDOS
SL       = /
LFLAGS   =
LIBS     = -lcurses -lm
# where to put installed files:
PREFIX   = /usr/local
endif
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
	-mkdir ../dist
	-mv -f $(PROG).exe ../dist
	-rm -f $(PROG) *~ src/*~ doc/*~ testit testit.exe

# Separate Linux src and dos src

export: distclean
	-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) && \
	$(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) && \
	$(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) && \
	cp $(PROG).exe $(PROJ) && \
	zip -r $(PROJ)-$(THIS_VER)dos.zip $(PROJ) && \
	rm -fr $(PROJ) && \
	cp -r $(PROJ)-$(THIS_VER)linux $(PROJ) && \
	tar -cvzf $(PROJ)-$(THIS_VER)linux.tar.gz $(PROJ)
	cd $(CURDIR)

# Get a quick image of working dir

work: clean
	-rm -fr *~ src/~ ../work.*
	cd .. ; \
	tar -cvzf work.tar.gz $(PROJ)
	cd $(CURDIR)

# 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 staff -m 755 $(PROG) $(PREFIX)/bin
	mkdir -p $(PREFIX)/share/$(PROJ)
	-rm -fr $(PREFIX)/share/$(PROJ)/*
	install -c -o root -g staff -m 644 $(DOCS) $(PREFIX)/share/$(PROJ)
	install -c -o root -g staff -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 shortcut to it.
endif
