# file: Makefile		G. Moody		24 February 2012
# 				Last revised:		  13 May 2012
# 'make' description file for building PhysioBank search tools
#
# 'make install' compiles and installs the standalone search tool (pbsqs), the
# server (pbsqsd), the plugins (pbs-and, pbs-not, etc.), the help and HTML
# files, and the web and command-line clients (pbsearch and pbsqsc);  it
# then (re)starts the server, and (using pbsqsc) recreates the list of all
# records (RECORDS-ALL).  If you want the server to start automatically
# whenever the computer is rebooted, you must set up this behavior manually
# (typically by adding 'pbsqsd' to '/etc/rc.local').
#
# 'make check' runs a minimal test to verify that the server is running and
# that it returns the expected result when asked for the number of records
# in the MIT-BIH Arrhythmia Database (48, which has not changed since 1979).
#
# By default, the server initializes itself by reading a local copy of
# physiobank-index in /home/physionet/html/physiobank/database (see
# pbsqs.h).  If you remove '-DLOCAL' from the rule for compiling
# daemon.o or standalone.o, however, the server or standalone tool,
# respectively, will read the current copy of physiobank-index from
# physionet.org (see pbsqs.h).  This may affect startup time
# (typically 1 or 2 seconds) if the network connection is slow, but it
# does not affect response time once the initialization is complete,
# since the server keeps the index in memory and does not re-read it unless
# signalled to do so. (Send it a SIGHUP, e.g., by 'killall -HUP pbsqsd',
# to force it to reread the index if it has been updated.)

BINDIR  = /usr/local/bin
CGIDIR  = /home/physionet/cgi-bin
PBDDIR  = /home/physionet/html/physiobank/database
PBSDIR  = /home/physionet/html/physiobank/database/pbs
LSTDIR  = /ptmp/atm/pbs
OFILES  = pbsqs.o daemon.o standalone.o
SFILES  = pbsqs.h pbsqs.c daemon.c standalone.c pbs-and.c pbs-not.c pbs-or.c \
	  pbs-erase.c pbsqsc pbsearch
XFILES  = pbsqsd pbsqs pbs-and pbs-not pbs-or pbs-erase
HTML    = doctype.html head.html footer.html help.html results.html
HELP    = pbsqs-short-help.txt pbsqs-help.txt

pbsqs:		pbsqs.o standalone.o
	$(CC) -o pbsqs pbsqs.o standalone.o -lcurl -lreadline

pbsqsd:	pbsqs.o daemon.o
	$(CC) -o pbsqsd pbsqs.o daemon.o -lcurl

pbsqs.o:	pbsqs.h pbsqs.c
	$(CC) -c -O pbsqs.c

daemon.o:	pbsqs.h daemon.c
	$(CC) -c -O -DLOCAL daemon.c

standalone.o:	pbsqs.h standalone.c
	$(CC) -c -O -DLOCAL standalone.c

pbs-and:	pbs-and.c
	$(CC) -o pbs-and -O pbs-and.c

pbs-not:	pbs-not.c
	$(CC) -o pbs-not -O pbs-not.c

pbs-or:		pbs-or.c
	$(CC) -o pbs-or -O pbs-or.c

pbs-erase:	pbs-erase.c
	$(CC) -o pbs-erase -O pbs-erase.c


x-install:	$(XFILES)
	sudo cp -p $(XFILES) pbsqsc $(BINDIR)

RECORDS-ALL:	pbsqsd
	pbsqsc "record ?" >$(PBDDIR)/RECORDS-ALL

install:	$(XFILES) $(HELP) $(HTML)
	-sudo killall pbsqsd
	sudo mkdir -p $(BINDIR) $(PBSDIR) $(LSTDIR)
	sudo chown apache.apache $(LSTDIR)
	sudo cp -p pbsearch $(CGIDIR)
	sudo cp -p $(HELP) $(HTML) $(PBSDIR)
	sleep 1
	$(MAKE) x-install
	pbsqsd
	sleep 3
	$(MAKE) RECORDS-ALL

check:
	pbsqsc '#record ~ mitdb' >test-output
	@if cmp -s test-output expected; then echo Check passed; \
	 else echo Check failed; fi

listing:
	enscript -2r $(SFILES) $(HTML) $(HELP) Makefile

clean:
	rm -f *~ $(OFILES) $(XFILES) test-output
