# for single-table apps like NV or Water, APP and TABLE should be the same:
# Thus APP = NV and TABLE = NV (but mysql table to build or work with is named in APP.php:TableName)
# If multiple tables like Quiz set APP = quiz (directory) and TABLE = QuizItem 
# Use by just calling 'make'. 
# APP = Water
APP = NV
TABLE = NV
ifeq ($(APP),$(TABLE))
	BLANKTABLE = 
else
	BLANKTABLE = $(TABLE)
endif
VER = 2.2

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

$(APP): $(APP)/index$(BLANKTABLE).php $(APP)/lbar.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)
	# (Maybe save a copy as $(APP)/$(TABLE).php.safe after editing it.)

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

$(APP)/lbar.php: templates/lbar.php $(APP)/$(TABLE).php
	php templates/lbar.php $(APP) $(TABLE) > $(APP)/lbar.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

distrib:
	tar czvf is.$(VER).tgz DevDoc.txt README index.html LICENSE templates Makefile redo

gitup:
	# To bring a repository here I did this:
	# cd ..; mkdir is; cd is;
	# git init 
	# git add .
	# git remote add origin https://github.com/tcveatch/is
	# To send changes to the global repository at github:
	# git status  # to see what has changed
	# git add <list modified files here> # this is called "staging"
	# git commit -m "Description of changes"
	# git push origin master

AUTOGENS = $(APP)/index.php $(APP)/isapi.js $(APP)/isREAD.db $(APP)/isREAD.php $(APP)/isREAD.js $(APP)/isBILL.db $(APP)/isBILL.php $(APP)/isBILL.js $(APP)/isCHARGE.db $(APP)/isCHARGE.php $(APP)/isCHARGE.js

clean:
	rm -f error_log $(APP)/error_log templates/error_log /tmp/isphp.headers /tmp/isphp.err
	rm $(AUTOGENS)

# 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
