#$Id: makefile,v 1.26 2026/05/04 05:20:57 sqko Exp $
httpd_conf := $(shell locate httpd.conf | grep -P '.conf$$')
envvar := $(shell locate envvars | grep apache)
httpd2_conf := $(shell locate apache2.conf | grep -P '.conf$$')
sites_enabled := $(shell locate '000-default.conf' | grep 'sites-enabled' | grep '.conf$$')
serve_cgi := $(shell locate serve-cgi-bin.conf | grep -P 'conf-enabled' | grep -P '.conf$$')
centos := $(shell uname -r | perl -ne '{/\.(el\d+)\./; print $$1};print "\n";')
ifeq ($(httpd_conf),)
else
cgi := $(shell grep -P '^[\s\t]*ScriptAlias' $(httpd_conf) | perl -e 'while ($$v = <>){chomp $$v; $$v =~ /\"([\w\-\/]+)\"/; print $$1};')
html := $(shell grep -P '^[\s\t]*DocumentRoot' $(httpd_conf) | perl -e 'while ($$v = <>){chomp $$v; $$v =~ /\"([\w\-\/]+)\"/; print $$1};')
user := $(shell grep -P '^[\s\t]*User\b' $(httpd_conf) | perl -e 'while ($$v = <>){chomp $$v; $$v =~ /(\w+)$$/; print $$1};')
group := $(shell grep -P '^[\s\t]*Group\b' $(httpd_conf) | perl -e 'while ($$v = <>){chomp $$v; $$v =~ /(\w+)$$/; print $$1};')
endif
ifeq ($(httpd2_conf),)
else
cgi := $(shell grep -P '\<Directory' $(serve_cgi) | perl -e 'while ($$v = <>){chomp $$v; $$v =~ /\"([\w\-\/]+)\"/; print $$1};')
html := $(shell grep -P '^[\s\t]*DocumentRoot' $(sites_enabled) | perl -e 'while ($$v = <>){chomp $$v; $$v =~ /Root\s([\w\-\/]+)/; print $$1};')
user := $(shell /usr/sbin/apachectl -S | grep User | perl -ne '/\"([\w\-]+)\"/; print $$1')
group := $(shell /usr/sbin/apachectl -S | grep Group | perl -ne '/\"([\w\-]+)\"/; print $$1')
endif

libdir := $(shell perldoc -lm CGI.pm | perl -e 'while (<>){if(/(.*)\/CGI.pm/){print $$1;}}')
ifeq ($(libdir),)
$(error "CGI.pm not found or not installed, aborting!")
endif

SUBDIRS :=
RFC := Furnace.pm

dist_version := $(shell perl -e 'use RFC::Furnace; print $$RFC::Furnace::DISTVER')

.PHONY: libs dist CPAN


CPAN: 
#	cpan LWP::Protocol::https
	cpan Email::MIME
	cpan MIME::Entity
	cpan Email::Abstract
	cpan Email::Sender::Simple
	cpan IO::All

all: $(libdir)/RFC
	@echo ""
	@echo ""
	@echo "   DTU Energy RFCfurnace ad-on for RFCcontrol"
	@echo ""
	@echo "   Provides a simplified control interface for RFCcontrol rigs used simply as furnaces"
	@echo ""
	@for i in $(SUBDIRS); do \
		echo "make all in $$i..."; \
		(cd $$i; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) all); \
	done
	@echo "Run make install as root to install this adon."
	@echo ""

install: CPAN dirs RFClibs /usr/bin/chmod test
	@for i in $(SUBDIRS); do \
		echo "Installing in $$i..."; \
		(cd $$i; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) install); \
	done
	cp -r cgi/*.cgi $(cgi)/RFCfurnace/
	cp -r html/* $(html)/RFCfurnace/
	cp -r html/templates/*.tt $(html)/celltest/templates/
	cp -R bin/* /usr/local/bin/RFCfurnace/
	cp build /home/celltest/furnace_build
	bash update_config.bash

update: CPAN test update_common

update_force: CPAN update_common

/usr/bin/chmod: /bin/chmod
	ln -s /bin/chmod /usr/bin/chmod

update_common: dirs /usr/bin/chmod RFClibs
	@for i in $(SUBDIRS); do \
		echo "Installing in $$i..."; \
		(cd $$i; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) update); \
	done
	cp -r cgi/*.cgi $(cgi)/RFCfurnace/
	cp -r html/* $(html)/RFCfurnace/
	cp -r html/templates/*.tt $(html)/celltest/templates/
	cp -R bin/* /usr/local/bin/RFCfurnace/
	cp build /home/celltest/furnace_build
	bash update_config.bash

dirs: $(libdir)/RFC $(html)/RFCfurnace $(cgi)/RFCfurnace /usr/local/bin/RFCfurnace


$(html)/RFCfurnace:
	test -d $(html)/RFCfurnace || mkdir $(html)/RFCfurnace

$(cgi)/RFCfurnace:
	test -d $(cgi)/RFCfurnace || mkdir $(cgi)/RFCfurnace

/usr/local/bin/RFCfurnace:
	test -d /usr/local/bin/RFCfurnace || mkdir /usr/local/bin/RFCfurnace


RFClibs: $(libdir)/RFC
	for file in $(RFC); do \
		cp RFC/$$file $(libdir)/RFC/; \
	done


$(libdir)/RFC:
	$(error "RFCcontrol not installed, aborting!")


test: CPAN /var/lock/SemaforeFile
	@echo "Current version to be installed:"
	@echo "RFCcontrol-$(version)"
	@echo "Current build to be installed:"
	@cat build
	@echo "Testing configurations for all rigs with new version/build. . . ."
	./make_test.pl
	perl test.t


clean:
	rm -rf test.t
	rm -rf start_servers
	@for i in $(SUBDIRS); do \
		echo "Clearing in $$i..."; \
		(cd $$i; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) clean); \
	done


uninstall:
	rm -rf $(cgi)/RFCfurnace
	rm -rf $(html)/RFCfurnace
	rm -rf /usr/local/bin/RFCfurnace
	for file in $(RFC); do \
		rm -f $(libdir)/RFC/$$file; \
	done


publish:
	md5sum ../RFCfurnace-$(dist_version).tgz > ../RFCfurnace_md5.txt
	sed -i 's/\.\.\///' ../RFCfurnace_md5.txt
	scp ../RFCfurnace-$(dist_version).tgz sofc@www.elchemea.dk:/var/www/html/dist/
	scp ../RFCfurnace_md5.txt sofc@www.elchemea.dk:/var/www/html/RFCfurnace/
	scp /var/www/html/dist/RFCfurnace_version.txt sofc@www.elchemea.dk:/var/www/html/dist/
	scp manual/manual.pdf sofc@www.elchemea.dk:/var/www/html/RFCfurnace/


test_dist: clean
	@echo "Retrieving latest committed files . . . ."
	co RFC/*.pm
	co cgi/*.cgi
	co html/*.css
	co html/*.js
	co *.pl
	co html/templates/*.tt
	@echo "Compiling manual";
		(cd manual; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) all)
		cp manual/manual.pdf html/manual.pdf
	@echo "Incrementing build number";
	../incr_build_nr.pl
	@cat build
	@echo "Building distribution tar file . . ."
	tar --exclude=RCS --exclude=manual -czf ../RFCfurnace-$(dist_version)-unstable.tgz ../RFCfurnace
	cp ../RFCfurnace-$(dist_version)-unstable.tgz /var/www/html/dist/

dist: clean
	@echo "Retrieving latest committed files . . . ."
	co RFC/*.pm
	co cgi/*.cgi
	co html/*.css
	co html/*.js
#	co html/*.html
	co *.pl
	co html/templates/*.tt
	@echo "Compiling manual";
		(cd manual; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) all)
		cp manual/manual.pdf html/manual.pdf
	@echo "Incrementing build number";
	../incr_build_nr.pl
	@cat build
	@echo "Building distribution tar file . . ."
	tar --exclude=RCS --exclude=manual -czf ../RFCfurnace-$(dist_version).tgz ../RFCfurnace
	cp ../RFCfurnace-$(dist_version).tgz /var/www/html/dist/
	rm -rf /var/www/html/dist/RFCfurnace-*-unstable.tgz

publish:
