SHELL=/bin/sh

# These are system-specific features
# Add "-DUSE_SVGALIB" if you are on a Linux system and want to run
# Maelstrom in both the console and X11.
# Add "-DPLAY_DEV_AUDIO" if you want to enable /dev/audio support.
#   -- The sound is not synchronous in /dev/audio mode under Linux.
# Add "-DPIXEL_DOUBLING" if you want Maelstrom in 1280x960 X resolution (slow!)
# Adding "-DASYNCHRONOUS_IO" results in smoother play on systems that
# support it.  These systems currently are: Linux, Solaris
FEATURES = -DUSE_SVGALIB -DASYNCHRONOUS_IO

MAELSTROM = Maelstrom Maelstrom_sound
PROGS     = test-asyncio macres
#PROGS     = test-asyncio macres scored

#XPMINCLUDE = /share/include
XPMINCLUDE = ../xpm-3.4g/include
#XPMLINKDIR = /share/lib
XPMLINKDIR = ../xpm-3.4g/lib

LIBDIR = /usr/local/lib/Maelstrom
OLDBINDIR = /usr/local/bin/X11
BINDIR = /usr/local/bin
DATAFILES = Images/*.icon Images/*.cicn "Maelstrom Fonts" \
            "Maelstrom Sounds" "Maelstrom Sprites"

OBJS = main.o framebuf.o x11_framebuf.o vga_framebuf.o fontserv.o sound.o \
       Mac_Resource.o init.o make.o misc.o blit.o game.o controls.o \
       scores.o netscore.o dialog.o Utils.o shared.o
SOUNDOBJS = Maelstrom_sound.o Mac_Resource.o soundres.o mixer.o shared.o

#########################################################
# Linux libraries and flags
CC = gcc
CFLAGS = -g -O2 -fomit-frame-pointer $(FEATURES) -DLIBDIR=\"$(LIBDIR)\" \
							-I$(XPMINCLUDE)
LIBS =  -lm -lvga -lvgagl -lXpm -lX11 -lXext -L/usr/X11R6/lib  -L$(XPMLINKDIR)
SOUNDLIBS = 

#########################################################
# Solaris libraries and flags
#CC = gcc
#CFLAGS = -g -O2 -fomit-frame-pointer $(FEATURES) -DLIBDIR=\"$(LIBDIR)\" \
#				-I/usr/openwin/include -I$(XPMINCLUDE)
#LIBS = -lm -lsocket -lnsl -lucb -lXpm -lX11 -lXext \
#		       -L/usr/openwin/lib -L/usr/ucblib -L$(XPMLINKDIR)
#SOUNDLIBS = -lucb -L/usr/ucblib -lsocket

#########################################################
# SunOS 4.1.4 libraries and flags
#CC = gcc
#LIBS = -lm -lXpm -lX11 -lXext -L/usr/openwin/lib -L$(XPMLINKDIR) $(FEATURES)
#SOUNDLIBS = -lucb -L/usr/ucblib -lsocket

#########################################################
# SGI libraries and flags
#CC = CC
#CFLAGS = -g -DLIBDIR=\"$(LIBDIR)\" -I$(XPMINCLUDE) $(FEATURES)
#LIBS = -lm -lXpm -lX11 -lXext -L$(XPMLINKDIR)

#########################################################
# HPUX libraries and flags
#CC = gcc
#CFLAGS = -g -O2 -DLIBDIR=\"$(LIBDIR)\" -I/usr/include/X11R5 -I$(XPMINCLUDE) \
#								$(FEATURES)
#LIBS = -lm -lXpm -lX11 -lXext -L$(XPMLINKDIR) -L/usr/lib/X11R5
#SOUNDLIBS =

#########################################################
# DEC libraries and flags
#CC = gcc
#CFLAGS = -g -O2 -DLIBDIR=\"$(LIBDIR)\" -I$(XPMINCLUDE) -I/usr/include \
#								$(FEATURES)
#LIBS = -lm -lXpm -lX11 -lXext -L$(XPMLINKDIR)
#SOUNDLIBS =

.cc.o:
	$(CC) $(CFLAGS) -c $<


all: $(PROGS) $(MAELSTROM)

install: $(MAELSTROM)
	@if [ -x $(OLDBINDIR)/Maelstrom ]; then \
		echo "Removing old Maelstrom binaries..."; \
		rm $(OLDBINDIR)/Maelstrom*; \
	fi
	@if [ ! -d $(BINDIR) ]; then mkdir $(BINDIR); fi
	@echo "Installing binaries in $(BINDIR)..."
	@for i in $(MAELSTROM) ; do \
		strip $$i; \
		install -m 755 $$i $(BINDIR); \
	done
	@if [ ! -d $(LIBDIR) ]; then \
		mkdir $(LIBDIR); chmod 755 $(LIBDIR); \
	fi
	@echo "Installing data files in $(LIBDIR) ..."
	@tar cf - $(DATAFILES) | (cd $(LIBDIR); tar xvf -)
	@chmod -R +r $(LIBDIR)
	@if [ ! -f "$(LIBDIR)/Maelstrom-Scores" ]; then \
		echo "Installing Maelstrom high-scores file"; \
		install -m 666 Maelstrom-Scores  $(LIBDIR); \
		chmod 666 $(LIBDIR)/Maelstrom-Scores; \
	fi
	@-if [ `uname` = "Linux" ]; then \
		echo "Fixing permissions for Maelstrom (set-uid root)"; \
		chown root $(BINDIR)/Maelstrom && \
					chmod u+s $(BINDIR)/Maelstrom; \
	fi
	@echo "Maelstrom installed!"
	@echo ""
	@echo "Make sure that $(BINDIR) is in your execution path"
	@echo "and type 'Maelstrom' to play!"

# I've always really disliked programs that installed themselves
# all over my system and didn't show me how to remove them.
uninstall:
	@if [ -x $(BINDIR)/Maelstrom ]; then \
		echo "Removing Maelstrom binaries..."; \
		rm $(BINDIR)/Maelstrom*; \
	fi
	@if [ -d $(LIBDIR) ]; then \
		if [ -f $(LIBDIR)/Maelstrom-Scores ]; then \
			if [ `echo "\c"` = "" ]; then \
				echo "Save high scores? [Y/n]: \c"; \
			elif [ `echo -n ""` = ""]; then \
				echo -n "Save high scores? [Y/n]: "; \
			else \
				echo "Save high scores? [Y/n]"; \
			fi; \
			read savem; \
			if [ "$$savem" != "n" ]; then \
				cp $(LIBDIR)/Maelstrom-Scores . ; \
				echo \
				"High scores saved as ./Maelstrom-Scores"; \
			fi; \
		fi; \
		echo "Removing data files..."; \
		rm -r $(LIBDIR); \
	fi
	@echo "Maelstrom uninstalled!"
	

Maelstrom: $(OBJS)
	$(CC) -o $@ $(OBJS) $(LIBS)

Maelstrom_sound: $(SOUNDOBJS)
	$(CC) -o $@ $(SOUNDOBJS) $(SOUNDLIBS)

macres: macres.o Mac_Resource.o Mac_Resource.h
	$(CC) -o $@ macres.o Mac_Resource.o

test-asyncio: test-asyncio.o
	$(CC) -o $@ test-asyncio.o $(LIBS)

countcolors: countcolors.o Mac_Resource.o
	$(CC) -o $@ countcolors.o Mac_Resource.o

clean:
	rm -f *.o *.bak

spotless: clean
	rm -f $(PROGS) $(MAELSTROM)
clobber: spotless

backup:
	-mount /A ;\
	cp -v *.cc *.c *.h *.xpm *.xbm *.URL /A ;\
	cp -v Makefile Copyright /A ;\
	cp -v utils/* /A/utils ;\
	cp -v Doc/* /A/doc ;\
	umount /A

Mac_Resource.o: Mac_Resource.cc Mac_Resource.h bytesex.h
Maelstrom_sound.o: Maelstrom_sound.cc Mac_Resource.h mixer.h sound_cmds.h
Utils.o: Utils.cc fontserv.h Sprite.h framebuf.h \
         vga_framebuf.h Maelstrom.h Maelstrom_Inline.h bytesex.h
blit.o: blit.cc Maelstrom_Globals.h fontserv.h Sprite.h framebuf.h \
        vga_framebuf.h Maelstrom.h Maelstrom_Inline.h
controls.o: controls.cc Maelstrom_Globals.h fontserv.h Sprite.h framebuf.h \
            Maelstrom_Inline.h dialog.h keyboard.h
dialog.o: dialog.cc dialog.h
fontserv.o: fontserv.cc Sprite.h
framebuf.o: framebuf.cc keyboard.h
game.o: game.cc Maelstrom_Globals.h fontserv.h Sprite.h framebuf.h \
        vga_framebuf.h Maelstrom.h Maelstrom_Inline.h
init.o: init.cc sound.h sound_cmds.h fontserv.h Sprite.h framebuf.h \
        vga_framebuf.h Maelstrom.h Maelstrom_Inline.h Maelstrom_icon.xpm \
        colortable.h
main.o: main.cc fontserv.h Sprite.h framebuf.h vga_framebuf.h \
        Maelstrom.h Maelstrom_Inline.h buttons.h
make.o: make.cc Maelstrom_Globals.h fontserv.h Sprite.h framebuf.h \
        vga_framebuf.h Maelstrom.h Maelstrom_Inline.h
misc.o: misc.cc Maelstrom_Globals.h fontserv.h Sprite.h framebuf.h \
        vga_framebuf.h Maelstrom.h Maelstrom_Inline.h
mixer.o: mixer.cc mixer.h sound_cmds.h sample.h
netscore.o: netscore.cc Mac_Resource.h framebuf.h Maelstrom_Inline.h \
            netscore.h
scores.o: scores.cc Maelstrom_Globals.h sound.h sound_cmds.h fontserv.h \
          Sprite.h framebuf.h vga_framebuf.h Maelstrom.h Maelstrom_Inline.h \
          dialog.h bytesex.h
shared.o: shared.cc usleep.c
sound.o: sound.cc sound_cmds.h
soundres.o: soundres.cc soundres.h Mac_Resource.h
vga_framebuf.o: vga_framebuf.cc framebuf.h
x11_framebuf.o: x11_framebuf.cc x11_framebuf.h framebuf.h cursor.h \
                cursor.xbm cursorm.xbm
# DO NOT DELETE
#
# The SGI needs this:
include .c++howto
