DB = $(HOME:/home/%=%)
ITEM = $(HOME:/home/%=%)
EMAIL = $(HOME:/home/%=%)@$(HOSTNAME)
MASTER_AUTH = userauth
HTML = public_html
DEF = $(HTML)/def
TR_D = sed -e 's/%dbname%/$(DB)/g'
TR_I = sed -e 's/%item%/$(ITEM)/g'
TR_M = sed -e 's/%master_auth%/$(MASTER_AUTH)/g'
TR_E = sed -e 's/%email_addr%/$(EMAIL)/g'
TR_H = sed -e 's/%home%/\/home\/$(DB)\/$(HTML)/g'
TR_ALL = $(TR_D) $? | $(TR_I) | $(TR_M) | $(TR_E) | $(TR_H)
DEF_F = eki.def evaluation.def evaluation_cmds.def expert.def special.def special_cmds.def userauth.def userauth_cmds.def userdata.def item_cmds.def
SQL_F = mkartable.sql mkcatable.sql mkrcatable.sql tables.sql
BIN_F = setup_dir.sh rmdir.sh chkuid daybkup.sh log_dayly.sh log_weeky.sh md5.php mdef mkartable.pl mkca.sh prepcatedata.php mkcatable.pl mkrcatable.php mkcamenu.php mksysdir.php pack.sh recalcItem.php recalcUser.php recalcUserAff.php reset.sh sendmail2all.php updpass
DAT_F = item.dat
IHTML = item_area.ihtml item_brow.ihtml item_cate.ihtml item_cdel.ihtml item_set.ihtml item_setcom.ihtml item_table.ihtml

all:	index.php $(DEF_F) $(SQL_F) $(BIN_F) $(IHTML) $(DAT_F) crondata
	cp src/public_html/*.inc $(HTML)/
	cp src/public_html/javascript.js $(HTML)/
	cp src/public_html/style.css $(HTML)/
	cp src/public_html/img/*.* $(HTML)/img/
	cp src/public_html/def/*_cmds.def $(HTML)/def/
	cp src/public_html/ihtml/*.ihtml $(HTML)/ihtml/
	bin/mkcamenu.php > ~/public_html/ihtml/categorymenu.ihtml
	@echo DB=$(DB) ITEM=$(ITEM) MASTER_AUTH=$(MASTER_AUTH)
	
index.php:		src/public_html/index.php
	mkdir $(HTML)
	mkdir $(HTML)/img
	mkdir $(HTML)/def
	mkdir $(HTML)/ihtml
	mkdir tabledef
	mkdir sql
	mkdir bin
	mkdir area
	mkdir category
	mkdir backup
	createdb $(DB)
	$(TR_ALL) >$(HTML)/$@

eki.def:	src/public_html/def/eki.def
	$(TR_ALL) >$(DEF)/$@
evaluation.def:	src/public_html/def/evaluation.def
	$(TR_ALL) >$(DEF)/$@
evaluation_cmds.def: src/public_html/def/evaluation_cmds.def
	cp $? $(DEF)/$@
expert.def:	src/public_html/def/expert.def
	$(TR_ALL) >$(DEF)/$@
special.def:	src/public_html/def/special.def
	$(TR_ALL) >$(DEF)/$@
special_cmds.def:	src/public_html/def/special_cmds.def
	cp $? $(DEF)/$@
userauth.def:	src/public_html/def/userauth.def
	$(TR_ALL) >$(DEF)/$@
userauth_cmds.def:	src/public_html/def/userauth_cmds.def
	cp $? $(DEF)/$@
userdata.def:	src/public_html/def/userdata.def
	$(TR_ALL) >$(DEF)/$@
item.def:	src/public_html/def/item.def
	$(TR_ALL) >$(DEF)/$@
item_cmds.def:	src/public_html/def/item_cmds.def
	$(TR_ALL) >$(DEF)/$@

mkartable.sql:	src/sql/mkartable.sql
	cp src/area/* area/
	$(TR_ALL) >sql/$@
mkcatable.sql:	src/sql/mkcatable.sql
	cp src/category/* category/
	$(TR_ALL) >sql/$@
mkrcatable.sql:	src/sql/mkrcatable.sql
	$(TR_ALL) >sql/$@
tables.sql:	src/sql/tables.sql
	$(TR_ALL) >sql/$@
	/usr/local/pgsql/bin/psql $(DB) < sql/tables.sql
item.sql:	src/sql/item.sql
	$(TR_ALL) >sql/$(ITEM).sql
	/usr/local/pgsql/bin/psql $(DB) < sql/$(ITEM).sql

setup_dir.sh:	src/bin/setup_dir.sh
	cp $? bin/$@
	bin/$@
chkuid:	src/bin/chkuid
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
daybkup.sh:	src/bin/daybkup.sh
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
log_dayly.sh:	src/bin/log_dayly.sh
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
log_weeky.sh:	src/bin/log_weekly.sh
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
md5.php:	src/bin/md5.php
	cp $? bin/$@
	chmod +x bin/$@
mdef:	src/bin/mdef
	cp $? bin/$@
	chmod +x bin/$@
mkartable.pl:	src/bin/mkartable.pl
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
	/usr/local/pgsql/bin/psql $(DB) < sql/mkartable.sql
	bin/$@
mkca.sh:	src/bin/mkca.sh
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
prepcatedata.php:	src/bin/prepcatedata.php
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
	bin/$@ category.dat > ~/category/category.txt
mkcatable.pl:	src/bin/mkcatable.pl
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
	/usr/local/pgsql/bin/psql $(DB) < sql/mkcatable.sql
	bin/$@
mkrcatable.php:	src/bin/mkrcatable.php
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
	/usr/local/pgsql/bin/psql $(DB) < sql/mkrcatable.sql
	bin/$@
mkcamenu.php:	src/bin/mkcamenu.php
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
mksysdir.php:	src/bin/mksysdir.php
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
	bin/$@ isch
pack.sh:	src/bin/pack.sh
	cp $? bin/$@
recalcItem.php:	src/bin/recalcItem.php
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
recalcUser.php:	src/bin/recalcUser.php
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
recalcUserAff.php:	src/bin/recalcUserAff.php
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
reset.sh:	src/bin/reset.sh
	cp $? bin/$@
rmdir.sh:	src/bin/rmdir.sh
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
sendmail2all.php:	src/bin/sendmail2all.php
	$(TR_ALL) >bin/$@
	chmod +x bin/$@
updpass:	src/bin/updpass
	$(TR_ALL) >bin/$@
	chmod +x bin/$@


item_area.ihtml:	src/public_html/ihtml/item_area.ihtml
	cp $? $(HTML)/ihtml/item_area.ihtml
item_brow.ihtml:	src/public_html/ihtml/item_brow.ihtml
	cp $? $(HTML)/ihtml/item_brow.ihtml
item_cate.ihtml:	src/public_html/ihtml/item_cate.ihtml
	cp $? $(HTML)/ihtml/item_cate.ihtml
item_cdel.ihtml:	src/public_html/ihtml/item_cdel.ihtml
	cp $? $(HTML)/ihtml/item_cdel.ihtml
item_set.ihtml:	src/public_html/ihtml/item_set.ihtml
	cp $? $(HTML)/ihtml/item_set.ihtml
item_setcom.ihtml:	src/public_html/ihtml/item_setcom.ihtml
	cp $? $(HTML)/ihtml/item_setcom.ihtml
item_table.ihtml:	src/public_html/ihtml/item_table.ihtml
	cp $? $(HTML)/ihtml/item_table.ihtml

crondata: src/crondata
	$(TR_ALL) >$@

item.dat:	src/tabledef/item.dat
	$(TR_ALL) >tabledef/$@
	bin/mdef --sql-exec tabledef/$@

restore:
	cp $(HTML)/*.inc src/public_html/
	cp $(HTML)/ihtml/*.ihtml src/public_html/ihtml/
	rm -f src/public_html/ihtml/item*.ihtml

clean:
	dropdb $(DB)
	rm -fr tabledef
	rm -fr sql
	rm -fr area
	rm -fr category
	rm -fr backup
	rm -f crondata
	bin/rmdir.sh
	rm -fr bin
	rm -fr $(HTML)

