.SUFFIXES: .html .js .min.js

# The default installation is for a default-install OpenBSD box that's
# running HTTPS (only).

# File-system location (directory) of static media.
# See HTURI for the web-visible component.
HTDOCS = /usr/local/www/bouquins/

# URL location (path) of static media.
# See HTDOCS.
HTURI = 

# File-system location (directory) of CGI script.
# See CGIURI.
CGIBIN = /usr/local/www/bouquins/cgi-bin

# File-system location of database.
# See RDDIR.
DATADIR = /usr/local/www/data

# Web-server relative location of system log file.
# This will have all logging messages by the system.
LOGFILE = /logs/system.log

# Compilation and link options.
# If on a static architecture, STATIC is -static; otherwise empty.
# I use /usr/local for kcgi and ksql, hence using them here.
STATIC = -static
CFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib

# Web-server relative location of DATADIR.
# See DATADIR.
RDDIR = /data

# Name of installed CGI script, since some servers like to have ".cgi"
# appended to everything.
# See TARGET.
CGINAME = bouquins

# URL location (filename) of CGI script.
# See CGIBIN and CGINAME.
CGIURI = /cgi-bin/$(CGINAME)

# This is the name of the binary we're going to build.
# It differs from CGINAME in that it's the install source.
# See CGINAME.
TARGET = bouquins

# If on an HTTPS-only installation, should be "-DSECURE".
SECURE = -DSECURE

# File-system location (directory) of Swagger API.
APIDOCS = /var/www/htdocs/api-docs

# Override these with an optional local file.
sinclude GNUmakefile.local

DATABASE = metadata.db
OBJS	 = db.o db_author.o db_book.o db_series.o json.o main.o

HTMLS	 = index.html book.html author.html series.html search.html
JSMINS = index.min.js book.min.js author.min.js series.min.js search.min.js
EXTJS  = externals/vue.min.js
CSS		 = externals/bootstrap.min.css
FONTS  = externals/fonts/*

CFLAGS	+= -g -W -Wall -O2 $(SECURE)
CFLAGS	+= -DLOGFILE=\"$(LOGFILE)\"
CFLAGS	+= -DDATADIR=\"$(RDDIR)\"
CFLAGS	+= -DDATABASE=\"$(DATABASE)\"
VERSION	 = 0.0.0

.if defined(DEBUG)
  CFLAGS += -DDEBUG
.endif

all: $(TARGET) $(HTMLS) $(JSMINS)

api: swagger.json

installwww: all
	mkdir -p $(HTDOCS) $(HTDOCS)/css $(HTDOCS)/fonts $(HTDOCS)/js
	install -m 0444 $(HTMLS) $(HTDOCS)
	install -m 0444 $(JSMINS) $(EXTJS) $(HTDOCS)/js
	install -m 0444 $(CSS) $(HTDOCS)/css
	install -m 0444 $(FONTS) $(HTDOCS)/fonts

installapi: api
	mkdir -p $(APIDOCS)
	install -m 0444 swagger.json $(APIDOCS)

updatecgi: all
	mkdir -p $(CGIBIN)
	install -m 0555 $(TARGET) $(CGIBIN)/$(CGINAME)
	install -m 0555 $(OBJS) $(CGIBIN)/

installcgi: updatecgi
	mkdir -p $(DATADIR)
	chmod 0777 $(DATADIR)

clean:
	rm -f $(TARGET) $(JSMINS) $(OBJS)
	rm -f swagger.json 

.js.min.js:
	sed -e "s!@HTURI@!$(HTURI)!g" \
	    -e "s!@VERSION@!$(VERSION)!g" \
	    -e "s!@CGIURI@!$(CGIURI)!g" $< >$@

$(TARGET): $(OBJS)
	$(CC) $(STATIC) -o $@ $(OBJS) $(LDFLAGS) -lksql -lkcgi -lkcgijson -lz -lsqlite3 -pthread

$(OBJS): extern.h

swagger.json: swagger.in.json
	@rm -f $@
	sed -e "s!@VERSION@!$(VERSION)!g" -e "s!@TARGET@!$(TARGET)!g" swagger.in.json >$@
	@chmod 400 $@