# By default, just typing MAKE will compile the target with debugging turned
# on.
# MAKE REL will compile with debugging off
# MAKE ABBREV will compile a release version and turn on abbreviations.  It
# also dumps out the compilation statistics.
# MAKE BLORB will build the release version and blorb it.
#
# Things you might need to change to get it to work on your system:
# ROOTDIR if your Inform compiler isn't on your root drive.
# INFRMROOT to point to the root of your Inform installation
# TARGET to the name of your main executable
#
# I've also included a MAX_OBJECTS definition, mainly to illustrate how to
# properly pass $ commands to Inform through the makefile.
#
# All these flags are located near the beginning of the file to make them easy
# to change.

# Modify the line below (and uncomment it) if your Inform executable is not on
# your root drive
ROOTDIR=C:

# Modify this line to point to your Inform directory
INFRMROOT=$(ROOTDIR)/devl/inform
PYROOT=$(ROOTDIR)/Python27
#INFRMROOT=/usr/local/inform

# Change this to match the name of your main source file
TARGET=illum

# Uncomment the line below if you have any custom .h files for your game and
# you want make to recompile the target if the .h files change
DEPS=$(TARGET).h

# Uncomment the line below if you want to compile to V8
# V8=1

# Change the line below if you need more than 640 objects (default)
# If you intend to leave it at 640, you can leave it commented out
OBJ_FLAG=\$$MAX_LABELS=1400 \$$MAX_QTEXT_SIZE=5000
GENABBR_FLAG=\$$MAX_TRANSCRIPT_SIZE=300000 \$$MAX_QTEXT_SIZE=5000

# Library and include directory settings
BINDIR=$(INFRMROOT)/bin
LIBDIR=$(INFRMROOT)/lib
#CONTRIBDIR=$(LIBDIR)/contrib
CONTRIBDIR=./localcontrib
#INFRM definition
INFRM=$(BINDIR)/inform6.exe
CBLORB=$(BINDIR)/cblorb.exe
BABEL=$(BINDIR)/babel.exe
PYTHON=$(PYROOT)/python.exe
RELDIR=./release
WEBDIR=play_online
TERPDIR=$(WEBDIR)/interpreter
TOOLDIR=tools
PYGAME2JS=$(TOOLDIR)/game2js.py
G2JSFLAGS=--giload
SED=sed

FLAGS=$(OBJ_FLAG)

# see top of file for explanation of default settings
DBGFLAGS=-S -D $(FLAGS)
CBLORBFLAGS=-unix
RELFLAGS=-~S -~D -~X $(FLAGS)
GLULXFLAGS=-G -H \$$MAX_PROP_TABLE_SIZE=90000 \$$MAX_STATIC_DATA=16000 \$$MAX_QTEXT_SIZE=5000
INCLUDE=+include_path=./,$(LIBDIR),$(CONTRIBDIR)
#RM definition for Linux machine or Cygwin
RM=rm -f
CP=cp

SRCS=$(TARGET).inf

reldir:
	@mkdir -p $(RELDIR)

$(TARGET) : $(SRCS) $(DEPS)
	$(INFRM) $(GLULXFLAGS) $(DBGFLAGS) $(INCLUDE) $(SRCS) $(RELDIR)/$(TARGET).ulx

debug: $(SRCS) $(DEPS)
	$(INFRM) $(GLULXFLAGS) $(DBGFLAGS) $(INCLUDE) $(SRCS) $(RELDIR)/$(TARGET).ulx

rel: release

release: $(SRCS) $(DEPS)
	$(INFRM) $(GLULXFLAGS) $(RELFLAGS) $(INCLUDE) $(SRCS) $(RELDIR)/$(TARGET).ulx

glulxrel: $(SRCS) $(DEPS)
	$(INFRM) $(GLULXFLAGS) $(RELFLAGS) $(INCLUDE) $(SRCS) $(RELDIR)/$(TARGET).ulx

.PHONY: checksum_glulx
checksum_glulx:
	 $(eval DATE=`date +%Y-%m-%d`)
	 $(eval CHECKSUM_GLULX=`hexdump -ve '16/1 "%0.2X" "\n"' -s 32 -n 4 $(RELDIR)/$(TARGET).ulx`)
	 $(eval SERIAL_GLULX=`hexdump -ve '16/1 "%c" "\n"' -s 54 -n 6 $(RELDIR)/$(TARGET).ulx`)
	 $(eval RELEASE_GLULX=`hexdump -ve '16/1 "%d" "\n"' -s 52 -n 2 $(RELDIR)/$(TARGET).ulx | sed 's/^0*//'`)

blorb: glulxrel ifiction_ulx
	@$(CBLORB) $(CBLORBFLAGS) $(TARGET)ulx.blurb $(RELDIR)/$(TARGET).gblorb
	@echo "Created $(TARGET).glorb with IFID: GLULX-$(RELEASE_GLULX)-$(SERIAL_GLULX)-$(CHECKSUM_GLULX)"
	@$(RM) $(TARGET)ulx.ifiction

blorbrel: blorb website

blorbd: debug ifiction_ulx
	@$(CBLORB) $(CBLORBFLAGS) $(TARGET)ulx.blurb $(RELDIR)/$(TARGET)-d.gblorb
	@$(RM) $(TARGET)ulx.ifiction

.PHONY: website
website: htmlgen
	@$(PYTHON) $(PYGAME2JS) $(G2JSFLAGS) $(RELDIR)/$(TARGET).gblorb > $(TERPDIR)/$(TARGET).gblorb.js

ifiction_ulx: checksum_glulx
	@echo "Generating iFiction Glulx file."
	@echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > $(TARGET)ulx.ifiction
	@echo "<!-- Metadata aggregated by $(TARGET) Makefile -->" >> $(TARGET)ulx.ifiction
	@echo "<ifindex version=\"1.0\" xmlns=\"http://babel.ifarchive.org/protocol/iFiction/\">" >> $(TARGET)ulx.ifiction
	@echo "<story>" >> $(TARGET)ulx.ifiction
	@echo "  <identification>" >> $(TARGET)ulx.ifiction
	@echo "			<ifid>GLULX-$(RELEASE_GLULX)-$(SERIAL_GLULX)-$(CHECKSUM_GLULX)</ifid>" >> $(TARGET)ulx.ifiction
	@echo "			<format>glulx</format>" >> $(TARGET)ulx.ifiction
	@echo "		</identification>	" >> $(TARGET)ulx.ifiction
	@echo "		<bibliographic>" >> $(TARGET)ulx.ifiction
	@echo "			<title>Illuminismo Iniziato</title>" >> $(TARGET)ulx.ifiction
	@echo "			<author>Michael J. Coyne</author>" >> $(TARGET)ulx.ifiction
	@echo "			<language>en-US</language>" >> $(TARGET)ulx.ifiction
	@echo "			<headline>An Interactive Invigoration</headline>" >> $(TARGET)ulx.ifiction
	@echo "			<firstpublished>2018</firstpublished>" >> $(TARGET)ulx.ifiction
	@echo "			<genre>Fantasy</genre>" >> $(TARGET)ulx.ifiction
	@echo "			<description>" >> $(TARGET)ulx.ifiction
	@echo "     As you turn the crank, you hear the turning of tiny gears and elaborate" >> $(TARGET)ulx.ifiction
	@echo "     machinery inside the Mr Wizard Portable Tyromancy Deluxe. For a moment," >> $(TARGET)ulx.ifiction
	@echo "     nothing happens. Then, the machine begins to glow, bathing you in an" >> $(TARGET)ulx.ifiction
	@echo "     otherworldly light that expands to surround the whole area. The world" >> $(TARGET)ulx.ifiction
	@echo "     around you fades, and another place, another time, takes its place." >> $(TARGET)ulx.ifiction
	@echo "     <br/>" >> $(TARGET)ulx.ifiction
	@echo "     An overripe sequel to 2003's Xyzzy-nominated Risorgimento Represso." >> $(TARGET)ulx.ifiction
	@echo "     Fifteen years in the making! Well, fifteen years to finish." >> $(TARGET)ulx.ifiction
	@echo "     More exotic cheeses! Better visions! Longer! Plot-significant!" >> $(TARGET)ulx.ifiction
	@echo "     <br/>" >> $(TARGET)ulx.ifiction
	@echo "     The interplanar rift which brought you to this world is still active, and" >> $(TARGET)ulx.ifiction
	@echo "     you must battle to close it before Renaldo can rip it wide open and visit" >> $(TARGET)ulx.ifiction
	@echo "     his evil upon your own world." >> $(TARGET)ulx.ifiction
	@echo "			<br/>" >> $(TARGET)ulx.ifiction
	@echo "     Coded in Inform6. Includes automap and sound features." >> $(TARGET)ulx.ifiction
	@echo "			<br/>" >> $(TARGET)ulx.ifiction
	@echo "			Difficulty: Standard" >> $(TARGET)ulx.ifiction
	@echo "			<br/>" >> $(TARGET)ulx.ifiction
	@echo "			IFID: GLULX-$(RELEASE_GLULX)-$(SERIAL_GLULX)-$(CHECKSUM_GLULX)" >> $(TARGET)ulx.ifiction
	@echo "			</description>" >> $(TARGET)ulx.ifiction
	@echo "		</bibliographic>" >> $(TARGET)ulx.ifiction
	@echo "		<cover>" >> $(TARGET)ulx.ifiction
	@echo "			<format>jpg</format>" >> $(TARGET)ulx.ifiction
	@echo "			<height>800</height>" >> $(TARGET)ulx.ifiction
	@echo "			<width>800</width>" >> $(TARGET)ulx.ifiction
	@echo "      <description>The wizard's apprentice holds the magial snow globe given to him by Ninario.</description>" >> $(TARGET)ulx.ifiction
	@echo "		</cover>" >> $(TARGET)ulx.ifiction
	@echo "		<contacts>" >> $(TARGET)ulx.ifiction
	@echo "			<url>http://turthalion.blogspot.com</url>" >> $(TARGET)ulx.ifiction
	@echo "		</contacts>" >> $(TARGET)ulx.ifiction
	@echo "		<releases>" >> $(TARGET)ulx.ifiction
	@echo "			<history>" >> $(TARGET)ulx.ifiction
	@echo "				<release>" >> $(TARGET)ulx.ifiction
	@echo "					<version>$(RELEASE_GLULX)</version>" >> $(TARGET)ulx.ifiction
	@echo "					<releasedate>$(DATE)</releasedate>" >> $(TARGET)ulx.ifiction
	@echo "					<compiler>Inform</compiler>" >> $(TARGET)ulx.ifiction
	@echo "					<compilerversion>6.33</compilerversion>" >> $(TARGET)ulx.ifiction
	@echo "				</release>" >> $(TARGET)ulx.ifiction
	@echo "			</history>" >> $(TARGET)ulx.ifiction
	@echo "		</releases>" >> $(TARGET)ulx.ifiction
	@echo "		<glulx>" >> $(TARGET)ulx.ifiction
	@echo "		  <release>$(RELEASE_GLULX)</release>" >> $(TARGET)ulx.ifiction
	@echo "		  <serial>$(SERIAL_GLULX)</serial>" >> $(TARGET)ulx.ifiction
	@echo "		  <checksum>$(CHECKSUM_GLULX)</checksum>" >> $(TARGET)ulx.ifiction
	@echo "		  <coverpicture>1</coverpicture>" >> $(TARGET)ulx.ifiction
	@echo "		  <width>1024</width>" >> $(TARGET)ulx.ifiction
	@echo "		  <height>740</height>" >> $(TARGET)ulx.ifiction
	@echo "		</glulx>" >> $(TARGET)ulx.ifiction
	@echo "        <colophon>" >> $(TARGET)ulx.ifiction
	@echo "            <generator>Michael J. Coyne</generator>" >> $(TARGET)ulx.ifiction
	@echo "            <originated>$(DATE)</originated>" >> $(TARGET)ulx.ifiction
	@echo "        </colophon>" >> $(TARGET)ulx.ifiction
	@echo "	</story>" >> $(TARGET)ulx.ifiction
	@echo "</ifindex>" >> $(TARGET)ulx.ifiction

.PHONY: htmlgen
htmlgen:
	@mkdir -p $(WEBDIR)
	@echo "Generating playable index.html."
	@echo "<!DOCTYPE html>" > $(WEBDIR)/index.html
	@echo "<html>" >> $(WEBDIR)/index.html
	@echo "<head>" >> $(WEBDIR)/index.html
	@echo "<title>Michael J. Coyne - Illuminismo Iniziato — Play</title>" >> $(WEBDIR)/index.html
	@echo "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">" >> $(WEBDIR)/index.html
	@echo "<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\">" >> $(WEBDIR)/index.html
	@echo "<meta name=\"author\" content=\"Michael J. Coyne\">" >> $(WEBDIR)/index.html
	@echo "<link rel=\"stylesheet\" href=\"style.css\">" >> $(WEBDIR)/index.html
	@echo "<link rel=\"stylesheet\" href=\"interpreter/glkote.css\" type=\"text/css\">" >> $(WEBDIR)/index.html
	@echo "<link rel=\"stylesheet\" href=\"interpreter/dialog.css\" type=\"text/css\">" >> $(WEBDIR)/index.html
	@echo "<script src=\"interpreter/jquery-1.12.4.min.js\" type=\"text/javascript\"></script>" >> $(WEBDIR)/index.html
	@echo "<script src=\"interpreter/glkote.min.js\" type=\"text/javascript\"></script>" >> $(WEBDIR)/index.html
	@echo "<script src=\"interpreter/quixe.min.js\" type=\"text/javascript\"></script>" >> $(WEBDIR)/index.html
	@echo "<script src=\"interpreter/resourcemap.js\" type=\"text/javascript\"></script>" >> $(WEBDIR)/index.html
	@echo "<script type=\"text/javascript\">" >> $(WEBDIR)/index.html
	@echo "game_options = {" >> $(WEBDIR)/index.html
	@echo "image_info_map: 'StaticImageInfo',  // image data is here, not in blorb" >> $(WEBDIR)/index.html
	@echo "use_query_story: false," >> $(WEBDIR)/index.html
	@echo "set_page_title: false," >> $(WEBDIR)/index.html
	@echo "inspacing: 0,     // gap between windows" >> $(WEBDIR)/index.html
	@echo "outspacing: 0     // gap between windows and edge of gameport" >> $(WEBDIR)/index.html
	@echo "};" >> $(WEBDIR)/index.html
	@echo "</script>" >> $(WEBDIR)/index.html
	@echo "<script src=\"interpreter/$(TARGET).gblorb.js\" type=\"text/javascript\"></script>" >> $(WEBDIR)/index.html
	@echo "</head>" >> $(WEBDIR)/index.html
	@echo "<body class=\"play\">" >> $(WEBDIR)/index.html
	@echo "<div class=\"container\">" >> $(WEBDIR)/index.html
	@echo "<div class=\"coverimage\"><span><a href=\"Cover.jpg\"><img src=\"Small Cover.jpg\" border=\"1\"></a></span></div>" >> $(WEBDIR)/index.html
	@echo "<div class=\"links\">" >> $(WEBDIR)/index.html
	@echo "<div class=\"smalltitle\">Illuminismo Iniziato</div>" >> $(WEBDIR)/index.html
	@echo "</div>" >> $(WEBDIR)/index.html
	@echo "<div id="gameport">" >> $(WEBDIR)/index.html
	@echo "<div id="windowport">" >> $(WEBDIR)/index.html
	@echo "<noscript><hr>" >> $(WEBDIR)/index.html
	@echo "<p>You'll need to turn on Javascript in your web browser to play this game.</p>" >> $(WEBDIR)/index.html
	@echo "<hr></noscript>" >> $(WEBDIR)/index.html
	@echo "</div>" >> $(WEBDIR)/index.html
	@echo "<div id=\"loadingpane\">" >> $(WEBDIR)/index.html
	@echo "<img src=\"interpreter/waiting.gif\" alt=\"LOADING\"><br>" >> $(WEBDIR)/index.html
	@echo "<em>&nbsp;&nbsp;&nbsp;Loading...</em>" >> $(WEBDIR)/index.html
	@echo "</div>" >> $(WEBDIR)/index.html
	@echo "<div id=\"errorpane\" style=\"display:none;\"><div id=\"errorcontent\">...</div></div>" >> $(WEBDIR)/index.html
	@echo "</div>" >> $(WEBDIR)/index.html
	@echo "</div>" >> $(WEBDIR)/index.html
	@echo "<div class=\"interpretercredit\"><a href=\"http://eblong.com/zarf/glulx/quixe/\">Quixe v. 2.1.6</a></div>" >> $(WEBDIR)/index.html
	@echo "</body>" >> $(WEBDIR)/index.html
	@echo "</html>" >> $(WEBDIR)/index.html

clean:
	-$(RM) -rf $(RELDIR)
