#!/usr/bin/make -f
#
# Makefile for Magnetic 2.2
# This is the enhanced release 2.2-vw1 by Valerie Winter
#
# dumb and curses versions, unix only.
#
# type 'make' in a shell to build magnetic-curses and magnetic-dumb.
#

# define your site's directory where the magnetic scrolls adventures
# are kept.  the request further below shows the internal default
# which is used when you do not define this.
#STORYDIR=


# set to the curses library available on your system
# (ignore this if you just want to compile the dumb version)
CURSES_LIB=ncurses


# where to install the binaries to
#BINDIR=~/bin
#BINDIR=/usr/games
BINDIR=/usr/local/games

#----------------------------------------------------------------------
# do not change anything below

NAME=magnetic
RELEASE=2.2_vw1

STORYDIR?=/usr/share/games/if/magnetic_scrolls/

CC=c99-gcc
COPTS=-g -O2 
CFLAGS=-w

OBJ_CORE=src/core_engine.o
OBJ_DUMB=$(OBJ_CORE) src/gfx_dumb.o src/term_common.o src/term_dumb.o
OBJ_CURSES=$(OBJ_CORE) src/gfx_dumb.o src/term_common.o src/term_curses.o

OBJ_DUMB_OLD=$(OBJ_CORE) src/gfx_dumb.o src/term_dumb-working.o
OBJ_CURSES_OLD=$(OBJ_CORE) src/gfx_dumb.o src/term_curses-working.o

SRCDIR_UTILS=src/utilities
UTILS=gfxlink2 hintlink myth passwd xtract64 xtractmw xtractpc

BINARIES=$(NAME)-dumb $(NAME)-curses d$(NAME) $(NAME) $(UTILS)


# targets:

.PHONY: clean distclean

default: all

magnetic interpreter: curses dumb

all: interpreter pristine utils

# note: don't compile this one unless you want to lose your eyesight...
#       and don't look at the code, for the same reason.
#       you have been warned.  better use the ansi version.
pristine:
	$(CC) $(COPTS) -o $(NAME)-pristine src/_pristine/emu.c src/_pristine/main.c


mkcurses: cursesterm $(OBJ_CURSES)
	$(CC) $(COPTS) -o $(NAME)-curses $(OBJ_CURSES) -l$(CURSES_LIB)
	ln -s $(NAME)-curses $(NAME)

mkoldcurses: $(OBJ_CURSES_OLD)
	$(CC) $(COPTS) -o $(NAME)-curses $(OBJ_CURSES_OLD) -l$(CURSES_LIB)
	ln -s $(NAME)-curses $(NAME)

mkansi mkdumb: dumbterm $(OBJ_DUMB) 
	$(CC) $(COPTS) -o $(NAME)-dumb $(OBJ_DUMB)
	ln -s $(NAME)-dumb d$(NAME)

mkoldansi mkolddumb: $(OBJ_DUMB_OLD) 
	$(CC) $(COPTS) -o $(NAME)-dumb $(OBJ_DUMB_OLD)
	ln -s $(NAME)-dumb d$(NAME)

curses: cursesterm
	make CFLAGS='$(CFLAGS) -DUSE_CURSES' mkcurses

ansi dumb:
	rm -f src/term_common.o
	make CFLAGS='$(CFLAGS) -DUSE_DUMB'  src/term_common.o
	make CFLAGS='$(CFLAGS) -DUSE_DUMB' mkdumb

oldcurses:
	make CFLAGS='$(CFLAGS) -DUSE_CURSES' mkoldcurses

oldansi olddumb:
	make CFLAGS='$(CFLAGS) -DUSE_DUMB' mkolddumb

utils:
	for file in $(UTILS); do \
	$(CC) $(COPTS) -o $$file $(SRCDIR_UTILS)/$$file.c; \
	done
	echo -e "\n\nMakefile: NOT compiling gfxlink since it is broken.\n\n"

clean:
	rm -f $(BINARIES) $(UTILS) $(OBJ_CORE) $(OBJ_DUMB) $(OBJ_CURSES) \
		$(OBJ_CURSES_OLD) $(OBJ_DUMB_OLD) $(NAME)-pristine

distclean: clean
	rm -Rf *~ src/*~ src/utilities/*~

srcdist backup: clean
	cd ..;\
	tar cjlpvf $(NAME)-$(RELEASE)-`date +"%d%b%Y"`-src.tar.bz2 $(NAME)-$(RELEASE);\
	cd $(NAME)-$(RELEASE)

bindist: all
	mkdir $(NAME)-$(RELEASE)
	cp $(BINARIES) $(UTILS) $(NAME)-$(RELEASE)
	tar cjlpvf $(NAME)-$(RELEASE)-`uname -s`-`uname -p`-bin.tar.bz2 $(NAME)-$(RELEASE)
	rm -Rf $(BINARIES) $(UTILS) $(NAME)-$(RELEASE)

install: interpreter
	$(INSTALL) -c -m 755 $(BINARIES) $(BINDIR)


#src/term_common.o:
dumbterm:
	$(CC) -w -c $(CFUNCS) -DUSE_DUMB -DSTORYDIR=\"$(STORYDIR)\" -o src/term_common.o src/term_common.c

cursesterm:
	$(CC) -w -c $(CFUNCS) -DUSE_CURSES -DSTORYDIR=\"$(STORYDIR)\" -o src/term_common.o src/term_common.c

src/term_dumb-working.o:
	$(CC) -w -c $(CFUNCS) -DUSE_DUMB -DSTORYDIR=\"$(STORYDIR)\" -o src/term_dumb-working.o src/term_dumb-working.c

src/term_curses-working.o:
	$(CC) -w -c $(CFUNCS) -DUSE_CURSES -DSTORYDIR=\"$(STORYDIR)\" -o src/term_curses-working.o src/term_curses-working.c

showconfig:
	@echo -e "\
current configuration in Makefile is:\n\
curses library: $(CURSES)\n\
local game library dir: $(STORYDIR)\n\
installation dir: $(BINDIR)"

.DEFAULT: 
	@echo -e "\
invoke make with one of these targets:\n\
curses\t\tbuild the ncurses version\n\
dumb\t\tbuild the dumb ansi-c version\n\
interpreter\tbuild both versions\n\
utilities\tbuild utilities\n\
all\t\tbuild curses, dumb, and utilities\n\
install\tbuild curses and dumb and install them to $(BINDIR)\n\
clean\t\tclean up"
