# Generated automatically from Makefile.in by configure.
include ../Make.config

EMPTYFILES	= index 1.dat 1.mol
DATAFILE	= data.names
MOLFILE		= molecule.names
PASSWD		= dbpasswd
PASSWDFILE	= passwd
GROUPS		= groups
DOTLINCKS	= .lincksrc
SERVER		= $(BINDIR)/lincks
USERLIST	= 1 2 3 4 5 6 7 8 9 10

# some commands
TOUCH		= touch

all:	warning

warning:
	@ echo "The only thing that you can make in the DB directory "
	@ echo "is \"make create\", which you must do explicitly.  If "
	@ echo "you have completed making and installing the LINCKS"
	@ echo "system, then you are ready to create the database. "
	@ echo ""
	@ echo "type \"make create\" now. "
	@ echo "MAKE SURE YOU ARE ON THE MACHINE WHERE THE DATABASE"
	@ echo "WILL BE RUN!  You have set that host to be $(HOST)."

create: createdbdir createempties createothers dotlincks startserver \
	checkstatus addpasswd continuemsg
	
continuemsg:
	@ echo ""
	@ echo ""
	@ echo "If all has gone well, type \"make installdb\".  Otherwise, you'll need to"
	@ echo "go through step by step as outlined in the system's administration"
	@ echo "manual found in ../docs/lincks_admin.ps.  You can either view it with a"
	@ echo "Postscript (R) viewer or print it out.  See the section entitled \"Getting"
	@ echo "the database running\"."
	@ true

checkstatus:
	@ echo ""
	@ echo "sleeping ...."
	@ sleep 10
	@ if [ ! -x $(BINDIR)/dbstat ] ; then \
		echo "$(BINDIR)/dbstat does not exist." ; \
		echo "Have your installed properly?" ; \
		exit 1 ; \
	else true; fi;
	@ if [ -x $(BINDIR)/dbstat ] ; then \
		echo "checking status of running database $(DBDIR)" ; \
		$(BINDIR)/dbstat $(DBDIR) ; \
	else true; fi;

dotlincks:
	@ if [ ! -f $(DBDIR)/$(DOTLINCKS) ] ; then \
		echo "DBDIR = $(DBDIR)" > $(DBDIR)/$(DOTLINCKS) ; \
		echo "HOST = $(HOST)" >> $(DBDIR)/$(DOTLINCKS) ; \
		echo "OWNER = $(OWNER)" >> $(DBDIR)/$(DOTLINCKS) ; \
		echo "TCPIPNO = $(TCPIPNO)" >> $(DBDIR)/$(DOTLINCKS) ; \
		echo "BINARIES = $(BINDIR)" >> $(DBDIR)/$(DOTLINCKS) ; \
		echo "LOGDIR = $(DBDIR)/log" >> $(DBDIR)/$(DOTLINCKS) ; \
		echo "MONITORPROGNO = $(MONITORPROGNO)" >> $(DBDIR)/$(DOTLINCKS) ; \
	else true; fi;

addpasswd:
	@ if [ -z "`nawk < $(DBDIR)/$(PASSWDFILE) '/lincks/ { print $$3; }'`" ]; then \
		echo "Adding passwords to $(PASSWDFILE) in $(DBDIR)" ; \
		(echo "hargle";echo "hargle")| $(BINDIR)/$(PASSWD) -s -d $(DBDIR) lincks ; \
		(echo "demo";  echo "demo")  | $(BINDIR)/$(PASSWD) -s -d $(DBDIR) demo ; \
		for i in $(USERLIST) ; \
		do \
		 	(echo "emptyuser$$i"; echo "emptyuser$$i") | \
			$(BINDIR)/$(PASSWD) -s -d $(DBDIR) "emptyuser$$i" ; \
		done ; \
	else true; fi;

startserver:
	@ if [ ! -x $(SERVER) ] ; then \
		echo "No control program $(SERVER) installed." ; \
		echo "Do \"make install\" in the top directory first." ; \
		exit 1 ; \
	else true; fi;
	@ if [ ! -x $(BINDIR)/dbstat ] ; then \
		echo "$(BINDIR)/dbstat does not exist." ; \
		echo "Have your installed properly?" ; \
		exit 1 ; \
	else true; fi;
	@ if $(BINDIR)/dbstat $(DBDIR) >/dev/null 2>/dev/null; then \
		echo "mm, you have already the server running ... "; \
	else \
		echo "starting server...."; \
		$(SERVER) -s $(DBDIR) ; \
	fi;

createdbdir:
	@ if [ ! -d $(DBDIR) ] ; then \
		echo "Making directory $(DBDIR)"; \
		$(MKDIRHIER) $(DBDIR); \
		chmod 755 $(DBDIR); \
	else true; fi;
	@ if [ ! -d $(DBDIR)/log ] ; then \
		echo "Making directory $(DBDIR)/log"; \
		$(MKDIRHIER) $(DBDIR)/log; \
		chmod 755 $(DBDIR)/log; \
	else true; fi;
	@ if [ ! -f $(DBDIR)/.fonttrans ] ; then \
		echo "Copying font translation file .fonttrans"; \
		echo "into $(DBDIR)"; \
		cp .fonttrans $(DBDIR); \
	else true; fi;

createempties:
	@ for i in $(EMPTYFILES) ; \
	do \
	if [ ! -f $(DBDIR)/$$i ] ; then \
		echo "creating $$i in $(DBDIR)" ; \
		$(TOUCH) $(DBDIR)/$$i ; \
	else \
		cat /dev/null > $(DBDIR)/$$i ; \
	fi; \
	done

createothers:
	@ if [ ! -f $(DBDIR)/$(DATAFILE) ] ; then \
		echo "creating $(DATAFILE) in $(DBDIR)" ; \
		echo "1.dat" > $(DBDIR)/$(DATAFILE) ; \
	else true; fi;
	@ if [ ! -f $(DBDIR)/$(MOLFILE) ] ; then \
		echo "creating $(MOLFILE) in $(DBDIR)" ; \
		echo "1.mol" > $(DBDIR)/$(MOLFILE) ; \
	else true; fi;
	@ if [ ! -f $(DBDIR)/$(PASSWDFILE) ] ; then \
		echo "creating $(PASSWDFILE) in $(DBDIR)" ; \
		echo "lincks 0" > $(DBDIR)/$(PASSWDFILE) ; \
		echo "demo 1" >> $(DBDIR)/$(PASSWDFILE) ; \
		for i in $(USERLIST) ; \
		do \
			echo "emptyuser$$i `expr $$i + 1`" >> $(DBDIR)/$(PASSWDFILE) ; \
		done ; \
		chmod 600 $(DBDIR)/$(PASSWDFILE) ; \
	else true; fi;
	@ if [ ! -f $(DBDIR)/$(GROUPS) ] ; then \
		echo "creating $(GROUPS) in $(DBDIR)" ; \
		LIST="0 1"; \
		for i in $(USERLIST) ; \
		do \
			LIST="$${LIST} `expr $$i + 1`"; \
		done; \
		LIST="$${LIST}*" ;\
		echo "0 $${LIST}" > $(DBDIR)/$(GROUPS) ; \
		echo "1 $${LIST}" >> $(DBDIR)/$(GROUPS) ; \
		for i in $(USERLIST) ; \
		do \
			echo "`expr $$i + 1` $${LIST}" >> $(DBDIR)/$(GROUPS) ; \
		done ; \
		chmod 600 $(DBDIR)/$(GROUPS) ; \
	else true; fi;

# create the root and run t2lincks
installdb: createroot t2lincks

createroot:
	@ if [ ! -x $(BINDIR)/dbroot ] ; then \
		echo "$(BINDIR)/dbroot does not exist." ; \
		echo "Have your installed properly?" ; \
		exit 1 ; \
	else true; fi;
	@ if [ -x $(BINDIR)/dbroot ] ; then \
		echo "running dbroot on $(DBDIR)" ; \
		$(BINDIR)/dbroot -u lincks -p hargle $(DBDIR) ; \
	else true; fi;

t2lincks:
	@ if [ ! -x $(BINDIR)/t2lincks ] ; then \
		echo "$(BINDIR)/t2lincks does not exist." ; \
		echo "Have your installed properly?" ; \
		exit 1 ; \
	else true; fi;
	@ if [ -x $(BINDIR)/t2lincks ] ; then \
		echo "" ; \
		echo "This will take a while, and don't worry if you get" ; \
		echo "some messages about connection timed out...." ; \
		echo "" ; \
		echo "running t2lincks to load $(DBDIR)" ; \
		if $(BINDIR)/t2lincks -f Export.t2l -u lincks -p hargle $(DBDIR) ; then \
			echo "" ; \
			echo "t2lincks run successfully.  You can now log in" ; \
			echo "using xlincks, but please read the documentation first." ; \
			echo "We suggest that you first log in as user demo with" ; \
			echo "with the password demo." ; \
			echo "" ; \
			echo "Database creation and loading completed." ; \
		else \
			echo ""; \
			echo "Failed to load database - please consult the documentation"; \
			echo "and the LINCKS.FAQ for answers."; \
			echo ""; \
		fi ; \
	else true; fi;

# cleaning targets...
clean:
	@ echo "Nothing cleaned, but if you want to whack the current"
	@ echo "files in $(DBDIR), type \"make cleandb\""
	@ true

cleandb:
	/bin/rm -f $(DBDIR)/$(GROUPS) $(DBDIR)/$(PASSWDFILE) \
	$(DBDIR)/$(MOLFILE) $(DBDIR)/$(DATAFILE) $(DBDIR)/index \
	$(DBDIR)/1.dat $(DBDIR)/1.mol $(DBDIR)/$(DOTLINCKS)
