SHELL=/bin/sh

MAELSTROM = Maelstrom Maelstrom_sound
PROGS     = test-asyncio macres

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"
#XPMINCLUDE = /share/include
XPMINCLUDE = ../xpm-3.4c/include
#CFLAGS = -g -O2 -DUSE_SVGALIB -DLIBDIR=\"$(LIBDIR)\" \
#					-I/usr/openwin/include -I$(XPMINCLUDE)
CFLAGS = -g -O2 -fomit-frame-pointer -DUSE_SVGALIB -DLIBDIR=\"$(LIBDIR)\" \
					-I/usr/openwin/include -I$(XPMINCLUDE)
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 dialog.o Utils.o shared.o
SOUNDOBJS = Maelstrom_sound.o Mac_Resource.o soundres.o mixer.o shared.o

#XPMLINKDIR = /share/lib
XPMLINKDIR = ../xpm-3.4c/lib
# Linux libraries and flags
CC = gcc
LIBS =  -lm -lvga -lvgagl -lXpm -lX11 -lXext -L/usr/X11R6/lib  -L$(XPMLINKDIR)
SOUNDLIBS = 
# Solaris libraries and flags
#CC = gcc
#LIBS = -lm -lsocket -lucb -lXpm -lX11 -lXext \
#       -L/usr/openwin/lib -L/usr/ucblib -L$(XPMLINKDIR)
#SOUNDLIBS = -lucb -L/usr/ucblib
# SGI libraries and flags
#CC = CC
#CFLAGS = -g -DLIBDIR=\".\" -I$(XPMINCLUDE)
#LIBS = -lm -lXpm -lX11 -lXext -L$(XPMLINKDIR)
# HPUX libraries and flags
#CC = gcc
#CFLAGS = -g -DLIBDIR=\".\" -I$(XPMINCLUDE)
#LIBS = -lm -lXpm -lX11 -lXext -L$(XPMLINKDIR)

.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 -)
	@if [ ! -f "$(LIBDIR)/Maelstrom-Scores" ]; then \
		echo "Installing Maelstrom high-scores file"; \
		install -m 666 Maelstrom-Scores  $(LIBDIR); \
		chmod 666 $(LIBDIR)/Maelstrom-Scores; \
	fi
	@echo "Fixing permissions for Maelstrom (set-uid root)"
	@-chown root $(BINDIR)/Maelstrom && chmod u+s $(BINDIR)/Maelstrom
	@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

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

dppm: dppm.o
	$(CC) -o $@ $< -lvga -lvgagl

clean:
	rm -f *.o *.bak

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

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

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