# Call with make TABLE=XXX or make Water
# If multiple tables do make TABLE=Quiz or make TABLE=QuizItem or etc.
# APP = Water. ABB is a short infix for isapi.js function naming.
APP = quiz
ABB = qz
TABLE = Quiz
ifeq ($(APP),$(TABLE))
	BLANKTABLE = 
else
	BLANKTABLE = $(TABLE)
endif
VER = 2.5

# Scaffold defaults — override on command line, e.g. make HOMEHOST=yoursite.com scaffold
HOMEHOST ?= yoursite.com
HEREPATH ?= is/$(APP)
SHARED   ?= /var/www/shared

# TABLES:
#	$(MAKE) TABLE=READ
#	$(MAKE) TABLE=BILL
#	$(MAKE) TABLE=CHARGE

$(APP): $(APP)/index$(BLANKTABLE).php $(APP)/isapi$(BLANKTABLE).js $(APP)/is$(TABLE).php  $(APP)/is$(TABLE).js  $(APP)/is$(TABLE).db 

# $(APP)/$(TABLE).php: templates/is.php
	# SKIPPING: cp templates/is.php $(APP)/$(TABLE).php
	# (Do this step once, manually, and before editing for your own application with APP, ABB, TABLE &c)
	# (Maybe save a copy as $(APP)/$(TABLE).php.safe after editing it.)

$(APP)/index$(BLANKTABLE).php: templates/isindex.example.php $(APP)/$(TABLE).php
	php templates/isindex.example.php $(APP) $(TABLE) > $(APP)/index.php
	cp -n $(APP)/index.php $(APP)/index$(BLANKTABLE).php

$(APP)/is$(TABLE).php: templates/isphp.php 
	php templates/isphp.php $(APP) $(TABLE) > $(APP)/is$(TABLE).php

$(APP)/is$(TABLE).js: templates/isjs.php $(APP)/$(TABLE).php
	php templates/isjs.php $(APP) $(TABLE) > $(APP)/is$(TABLE).js

$(APP)/is$(TABLE).db: templates/isdb.php $(APP)/$(TABLE).php
	php templates/isdb.php $(APP) $(TABLE) > $(APP)/is$(TABLE).db
	chmod 755 $(APP)/is$(TABLE).db

$(APP)/isapi$(BLANKTABLE).js: templates/isapijs.php $(APP)/$(TABLE).php
	php templates/isapijs.php $(APP) $(TABLE) > $(APP)/isapi$(BLANKTABLE).js

scaffold: $(APP)/here.php $(APP)/lbar.php $(APP)/scrudTop.php $(APP)/humbeep.css $(APP)/log $(APP)/isphp.err $(APP)/is.css $(APP)/is-swipe.js $(APP)/$(APP).css $(APP)/$(APP).js

$(APP)/here.php:
	@printf '<?php\n  $$homehost = "%s";\n  $$herepath = "/%s";\n?>\n' "$(HOMEHOST)" "$(HEREPATH)" > $@
	@echo "wrote $@ (homehost=$(HOMEHOST), herepath=/$(HEREPATH))"

$(APP)/lbar.php: templates/lbar.php
	cp templates/lbar.php $@

# default UI: stylesheet + phone swipe navigation, shipped with the distribution
$(APP)/is.css: templates/is.css
	cp templates/is.css $@

$(APP)/is-swipe.js: templates/is-swipe.js
	cp templates/is-swipe.js $@

# per-app customisation hooks. index.php always links them, so create empty
# stubs rather than leaving two 404s in every app's console.
$(APP)/$(APP).css:
	@echo '/* $(APP): app-specific styles. Loaded after is.css. */' > $@

$(APP)/$(APP).js:
	@echo '/* $(APP): app-specific JS. Loaded after the IS api. */' > $@

$(APP)/scrudTop.php:
	ln -sf $(SHARED)/scrudTop.php $@

$(APP)/humbeep.css:
	ln -sf $(SHARED)/humbeep.css $@

$(APP)/log:
	ln -sf $(SHARED)/log $@

$(APP)/isphp.err:
	touch $@
	chmod 666 $@

# Generate a throwaway app from THIS distribution and check it is internally
# consistent (catches the 2.4 class of bug: index.php referencing a script the
# Makefile never generates).  Run before cutting a distribution.
selftest:
	./selftest.sh

distrib: selftest
	find . -name '*~' -type f -print -delete
	tar czvf is.$(VER).tgz DevDoc.txt README DISCIPLINE.md HOWTO-AI.md index.html LICENSE templates Makefile selftest.sh

# Publish: refresh the public repo people clone from. It is served straight off
# tomveatch.com -- Tom's own host, no account on anyone else's service.
#     git clone https://tomveatch.com/is/is.git
PUBREPO = /var/www/tomveatch.com/is/is.git

publish:
	@test -d $(PUBREPO) || { echo "no $(PUBREPO) -- see is/README"; exit 1; }
	./selftest.sh
	tmp=$$(mktemp -d); cp -r Makefile README DISCIPLINE.md HOWTO-AI.md LICENSE \
	    DevDoc.txt index.html selftest.sh templates $$tmp/; \
	  cd $$tmp && git init -q -b main && git add -A && \
	  git -c user.name="Tom Veatch" -c user.email="tv@tomveatch.com" \
	      commit -q -m "IS $(VER)" && \
	  git push -q --force $(PUBREPO) main && cd / && rm -rf $$tmp
	cd $(PUBREPO) && rm -f hooks/*.sample && \
	  git config --remove-section remote.origin 2>/dev/null || true; \
	  echo 'IS - Information Structure. Describe a table, get a working web app. https://tomveatch.com/is/' > $(PUBREPO)/description
	cd $(PUBREPO) && git update-server-info
	@echo "published IS $(VER) -- git clone https://tomveatch.com/is/is.git"


clean:
	rm -f error_log $(APP)/error_log templates/error_log /tmp/isphp.headers /tmp/isphp.err
	@echo "WARNING: this autogenerates $(APP)/ files which you may have manually modified"
	@echo " including: is*.php is*.js is*.db isapi*.js index*.php"
	@read -p "Are you sure? [y/N] " -n 1 -r ; \
	echo; \
	if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
		rm -f error_log $(APP)/error_log templates/error_log /tmp/isphp.headers /tmp/isphp.err; \
		rm -f $(APP)/is*.php $(APP)/is*.js $(APP)/is*.db $(APP)/isapi*.js $(APP)/index*.php; \
		echo "Cleaned."; \
	else \
		echo "Cancelled."; \
	fi



# Test targets for CRUD ops.
tc: $(APP)
	rm -f error_log $(APP)/error_log templates/error_log /tmp/isphp.headers /tmp/isphp.err
	curl http://tomveatch.com/ops/is/$(APP)/is$(APP).php -X POST -T $(APP)/test.data.create -D /tmp/isphp.headers
	cat /tmp/isphp.headers
	touch error_log $(APP)/error_log templates/error_log /tmp/isphp.err
	cat   error_log $(APP)/error_log templates/error_log /tmp/isphp.err

tr: $(APP)
	rm -f error_log $(APP)/error_log templates/error_log /tmp/isphp.headers /tmp/isphp.err
	curl http://tomveatch.com/ops/is/$(APP)/is$(APP).php?id=46 -X GET -D /tmp/isphp.headers
	cat /tmp/isphp.headers
	touch error_log $(APP)/error_log templates/error_log /tmp/isphp.err
	cat   error_log $(APP)/error_log templates/error_log /tmp/isphp.err

tu: $(APP)
	rm -f error_log $(APP)/error_log templates/error_log /tmp/isphp.headers /tmp/isphp.err
	# curl http://tomveatch.com/ops/is/$(APP)/is$(APP).php -X PUT -T $(APP)/test.data.update -D /tmp/isphp.headers
	curl http://tomveatch.com/ops/is/$(APP)/is$(APP).php -X PUT -T $(APP)/test.data.update
	touch error_log $(APP)/error_log templates/error_log /tmp/isphp.err
	cat   error_log $(APP)/error_log templates/error_log /tmp/isphp.err

td: $(APP)
	rm -f error_log $(APP)/error_log templates/error_log /tmp/isphp.headers /tmp/isphp.err
	curl http://tomveatch.com/ops/is/$(APP)/is$(APP).php -X DELETE -T $(APP)/test.data.delete -D /tmp/isphp.headers
	curl http://tomveatch.com/ops/is/$(APP)/is$(APP).php?id=45 -X DELETE
	touch error_log $(APP)/error_log templates/error_log /tmp/isphp.err
	cat   error_log $(APP)/error_log templates/error_log /tmp/isphp.err

FYI:
	$(APP)/is$(TABLE).db create_table
	$(APP)/is$(TABLE).db modify_table
	$(APP)/is$(TABLE).db backup_table 
	$(APP)/is$(TABLE).db delete_table 
	$(APP)/is$(TABLE).db restore_table
	$(APP)/is$(TABLE).db create 
	$(APP)/is$(TABLE).db read id=1,2
	$(APP)/is$(TABLE).db update id=2 var=modval
	$(APP)/is$(TABLE).db delete id=2
