#
#  Perlweiss - jetzt mit advanced Entwissning-Formela.
#
#
#


CC		= g++
#CFLAGS		= -O6 -mpentium
CFLAGS		= -O2
DEBUGFLAGS	= -Wall -g

OFILES		= main.o hiscore.o pidmaster.o
HFILES		= config.h hiscore.h
FINAL		= upd

USER		= root
GROUP		= root
MODE		= 755
BINDIR		= /usr/local/bin
LIBDIR		= /usr/local/lib/upd
PHPDEMO		= upd.php3
CGIDEMO		= upd.cgi
HTMLTPL		= upd.template.html
HTMLDIR		= /usr/local/apache/htdocs
CGIDIR		= /usr/local/apache/cgi-bin
RC3S		= /etc/rc.d/rc3.d/S97upd
RC5S		= /etc/rc.d/rc5.d/S97upd
RCINIT		= /etc/rc.d/init.d/upd

RM		= /bin/rm -vfr
CP		= /bin/cp -vfa
MV		= /bin/mv -vf
LN		= /bin/ln -sf
CHMOD		= /bin/chmod



all:		$(FINAL) $(OFILES) $(HFILES) updconvert
		strip $(FINAL)
		@echo -e "\nThanks for using the tisprut uptime daemon!"
		@echo -e "Please mail me the URL of your upd.html or upd.php"
		@echo -e "and the architechture of your machine to:"
		@echo -e "stephan@nerd.dk."
		@echo -e "It would make me a very, very happy man =)\n"
		@exit


$(FINAL):   	$(OFILES) $(HFILES)
		$(CC)    -o $(FINAL) $(OFILES) $(CFLAGS) $(DEBUGFLAGS)


$(OFILES): %.o:	%.cpp
		$(CC) -c -o $@ $< $(CFLAGS) $(DEBUGFLAGS)


updconvert:	updconvert.o hiscore.o hiscore.h config.h
		@echo -e  "\nBaking cookie transformation tool:"
		$(CC)    -o updconvert updconvert.o hiscore.o $(DEBUGFLAGS)

install:
		cat ./INSTALL
	

install_upd:	$(FINAL)
		@echo -e "\nInstalling uptime daemon:"
		install -o $(USER) -g $(GROUP) -m $(MODE) -s $(FINAL) $(BINDIR)
		@echo -e "\nInstallation complete!\n";


install_data:
		@echo -e "\nCreating directory for data files:"
		install -o $(USER) -g $(GROUP) -m $(MODE) -d $(LIBDIR)
		touch $(LIBDIR)/upd.hiscores
		touch $(LIBDIR)/upd.tmp
		@echo -e "\nInstalling PHP demo file:"
		install -o $(USER) -g $(GROUP) -m $(MODE) $(PHPDEMO) $(HTMLDIR)
		@echo -e "\nInstalling CGI demo file:"
		install -o $(USER) -g $(GROUP) -m $(MODE) $(CGIDEMO) $(CGIDIR)
		@echo -e "\nInstalling template HTML file:"
		install -o $(USER) -g $(GROUP) -m $(MODE) $(HTMLTPL) $(HTMLDIR)
		@echo -e "\nData files installed!\n"


install_scripts:
		@echo -e "\nInstalling start scripts for runlevel 3 and 5:"
		install -o $(USER) -g $(GROUP) -m $(MODE) ./upd.rcinit $(RCINIT)
		@echo $(RCINIT)
		$(LN) $(RCINIT) $(RC3S)
		$(LN) $(RCINIT) $(RC5S)
		@echo -e "\nInit scripts installation complete!\n"


start:		$(FINAL)
		@$(BINDIR)/upd start
		@touch $(LIBDIR)/upd.tmp
		@$(BINDIR)/upd top


upgrade:	$(FINAL) updconvert
		@echo -e "\nShutting down current upd:"
		$(BINDIR)/upd stop
		@echo -e "\nBacking up current hiscore list:"
		$(MV) $(LIBDIR)/upd.hiscores /tmp/upd.hiscores.OLD
		@echo -e "Converting hiscores ..."
		./updconvert /tmp/upd.hiscores.OLD $(LIBDIR)/upd.hiscores
		@echo -e "\nInstalling new upd:"
		install -o $(USER) -g $(GROUP) -m $(MODE) -s $(FINAL) $(BINDIR)
		@echo -e "\nDeleting upd.tmp:"
		$(RM) $(LIBDIR)/upd.tmp
		@echo -e "\nStarting new upd:"
		$(BINDIR)/upd start
		@echo -e "\nUpgrade complete!\n"


uninstall:
		@echo -e "\nUninstalling uptime demon:"
		@if [ -f $(BINDIR)/$(FINAL) ]; then\
			$(RM) $(BINDIR)/$(FINAL); fi
		@if [ -f $(HTMLDIR)/$(PHPDEMO) ]; then\
			$(RM) $(HTMLDIR)/$(PHPDEMO); fi
		@if [ -f $(CGIDIR)/$(CGIEMO) ]; then\
			$(RM) $(CGIDIR)/$(CGIDEMO); fi
		@if [ -f $(HTMLDIR)/$(HTMLTPL) ]; then\
			$(RM) $(HTMLDIR)/$(HTMLTPL); fi
		@if [ -d $(LIBDIR) ]; then\
			$(RM) $(LIBDIR); fi
		@echo -e "\nDeleting start scripts:"
		@if [ -f $(RC4) ]; then\
			$(RM) $(RC3S); fi
		@if [ -f $(RC5) ]; then\
			$(RM) $(RC5S); fi
		@if [ -f $(RCINIT) ]; then\
			$(RM) $(RCINIT); fi
		@echo -e "\nUptime daemon successfully uninstalled!\n";



backup:		bak
		$(CP) README INSTALL CHANGELOG Makefile *.cpp *.h\
		upd.template.html upd.cgi\
		upd.php*\
		upd.rcinit\
		bak


bak:
		mkdir bak


clean:
		$(RM) $(FINAL) updconvert *.o core



