1
2# Makefile - part of amtc
3# https://github.com/schnoddelbotz/amtc
4#
5# Toplevel Makefile for amtc and amtc-web(2).
6#
7# Targets:
8# make amtc      -- build amtc C binary
9# make amtc-web  -- build amtc-web application (V2 ... incomplete yet)
10# make package   -- build a deb/rpm/osx package of amtc & amtc-web
11# make install-package -- build and install package for current platform (sudo!)
12# make purge     -- remove any installed package, INCLUDING data (sudo!)
13#
14# make dist      -- prepare dist/ tree for distribution
15# make install   -- install amtc and amtc-web, respects $DESTDIR
16# make deb       -- build debian/raspian package of amtc incl. amtc-web
17# make rpm       -- build RPMs (RHEL/CentOS/Fedora...) of amtc and amtc-web
18# make osxpkg    -- build OSX installer .pkg
19# make clean     -- remove build artifacts
20# make debclean  -- remove .deb package build artifacts, including .deb built
21# make farmbuild -- build release packages on VM build hosts
22
23AMTCV = $(shell cat version)
24APP   = amtc-$(AMTCV)
25SHELL = bash
26
27RPMBUILD  ?= $(HOME)/rpmbuild
28RPMSRC    ?= "$(RPMBUILD)/SOURCES/amtc-$(AMTCV).tar.gz"
29DESTDIR   ?= /
30BINDIR    ?= usr/bin
31WWWDIR    ?= usr/share/amtc-web
32ETCDIR    ?= etc
33DATADIR   ?= var/lib
34AMTCWEBDIR = amtc-web
35
36# for farmbuild target - build hosts
37HOSTS_deb  = debian8 ubuntu14 raspbian7
38HOSTS_rpm  = fedora20 centos7
39
40# note: debian derivates (ubuntu, raspbian...) have /etc/d_v, too.
41PKGTYPE = $(shell (test -f /etc/debian_version && echo deb) || \
42		  (test -f /etc/redhat-release && echo rpm) || echo osxpkg)
43
44APACHECONFD = $(shell test -d /etc/apache2/conf-enabled && \
45		      echo conf-enabled || echo conf.d)
46SPOOLER_USER = $(shell (test -f /etc/redhat-release && echo amtc-web) || echo www-data)
47
48#
49all: amtc amtc-web
50
51# build amtc C binary
52amtc:
53	cd src && make
54
55# build amtc-web (fetch/concat js & css libs)
56amtc-web:
57	cd $(AMTCWEBDIR) && make -j10
58
59clean:
60	rm -rf dist amtc amtc*.{deb,pkg} *.build debian/amtc \
61		osxpkgscripts osxpkgroot Distribution.xml amtc_build.spec
62	cd src && make clean
63	cd $(AMTCWEBDIR) && make clean
64
65install: dist
66	mkdir -p $(DESTDIR)
67	cp -R dist/* $(DESTDIR)
68	rm -f $(DESTDIR)/$(WWWDIR)/.htaccess $(DESTDIR)/$(WWWDIR)/basic-auth/.htaccess
69	mkdir -p $(DESTDIR)/etc/apache2/$(APACHECONFD)
70	cp $(AMTCWEBDIR)/_httpd_conf_example $(DESTDIR)/etc/amtc-web/amtc-web_httpd.conf
71	ln -s ../../amtc-web/amtc-web_httpd.conf $(DESTDIR)/etc/apache2/$(APACHECONFD)
72
73dist: amtc amtc-web
74	echo "Preparing clean distribution in dist/"
75	rm -rf dist
76	mkdir -p dist/$(BINDIR) dist/$(WWWDIR) dist/$(ETCDIR)/cron.d dist/$(DATADIR)
77	cp src/amtc dist/$(BINDIR)
78	cp -R $(AMTCWEBDIR)/* dist/$(WWWDIR)
79	cd dist/$(WWWDIR) && make distclean && mv _htaccess_example .htaccess && \
80	   rm -f basic-auth/_htaccess.default config/_htpasswd.default data/amtc-web.db \
81	   config/siteconfig.php build.sh Makefile Makefile.Sources
82	cd dist && mv $(WWWDIR)/crontab-example.txt $(ETCDIR)/cron.d/amtc-web
83	perl -pi -e "s@amtc-web@$(SPOOLER_USER)@" dist/$(ETCDIR)/cron.d/amtc-web
84	cd dist && mv $(WWWDIR)/config $(ETCDIR)/amtc-web && mv $(WWWDIR)/data $(DATADIR)/amtc-web
85	cd dist/$(WWWDIR) && ln -s /$(ETCDIR)/amtc-web config && ln -s /$(DATADIR)/amtc-web data
86	cd dist/$(WWWDIR) && perl -pi -e "s@AuthUserFile .*@AuthUserFile /$(ETCDIR)/amtc-web/.htpasswd@" basic-auth/.htaccess
87
88
89# build package, depending on current os
90package:
91	make $(PKGTYPE)
92
93# build q+d debian .deb package (into ../)
94deb: clean
95	echo y | dh_make --createorig -s -p amtc_$(AMTCV) || true
96	echo  -e "#!/bin/sh -e\nchown www-data:www-data /var/lib/amtc-web /etc/amtc-web\nchmod 770 /var/lib/amtc-web /etc/amtc-web\na2enmod headers\na2enmod rewrite\nservice apache2 restart" > debian/postinst
97	perl -pi -e 's@Description: .*@Description: Intel AMT/DASH remote power management tool@' debian/control
98	perl -pi -e 's@^Depends: (.*)@Depends: $$1, apache2|lighttpd|nginx, php5-curl, php5-sqlite|php5-mysql|php5-pgsql@' debian/control
99	perl -pi -e 's@^Build-Depends: (.*)@Build-Depends: $$1, curl, vim-common, libcurl3, libcurl4-gnutls-dev@' debian/control
100	debuild -i -us -uc -b
101
102# remove debian/ subdirectory and trash package(s) built
103debclean: clean
104	rm -rf debian ../amtc_*
105
106# build RPM package (into ~/rpmbuild/RPMS/)
107rpm: clean
108	mkdir -p $(RPMBUILD)/SOURCES
109	cd ..; mv amtc $(APP); tar --exclude-vcs -czf $(RPMSRC) $(APP); mv $(APP) amtc
110	perl -pe "s/#AMTCV#/$(AMTCV)/" amtc.spec > amtc_build.spec
111	rpmbuild -ba amtc_build.spec
112
113# apply RHELoid + apache 2.4 changes (if installed _on buildhost_)
114# called by amtc.spec%install
115rpmfixup:
116	mv $(DESTDIR)/etc/apache2 $(DESTDIR)/etc/httpd
117	rpm -qa | grep httpd-2.4 && perl -pi -e 'BEGIN{undef $$/;} s@Order allow,deny\n\s+Allow from all@Require all granted@sm' $(DESTDIR)/etc/amtc-web/amtc-web_httpd.conf || true
118	rpm -qa | grep httpd-2.4 && perl -pi -e 'BEGIN{undef $$/;} s@Order allow,deny\n\s+Deny from all@Require all denied@smg' $(DESTDIR)/etc/amtc-web/amtc-web_httpd.conf || true
119
120# build OSX .pkg (into ./); use SecureTransport;
121# postinst enables system apache's php5 module
122osxpkg: clean
123	mkdir -p osxpkgscripts osxpkgroot/Library/LaunchDaemons
124	DESTDIR=osxpkgroot make install
125	cp osxpkgresources/postinstall osxpkgscripts
126	cp osxpkgresources/ch.hacker.amtc-web.plist osxpkgroot/Library/LaunchDaemons
127	chmod +x osxpkgscripts/postinstall
128	mv osxpkgroot/etc/apache2/conf.d osxpkgroot/etc/apache2/other
129	pkgbuild --root osxpkgroot --scripts osxpkgscripts \
130		 --identifier ch.hacker.amtc --version $(AMTCV) amtc.pkg
131	productbuild --synthesize --package amtc.pkg Distribution.xml
132	perl -pi -e 's@</installer-gui-script>@ \
133		<title>amtc</title> \
134		<welcome file="welcome.rtf" mime-type="text/rtf" /> \
135		<conclusion file="conclusion.rtf" mime-type="text/rtf" /> \
136		<background file="amtc-installer-bg.png" mime-type="image/png" alignment="bottomleft" scaling="none" /> \
137		</installer-gui-script>@' Distribution.xml
138	productbuild --distribution Distribution.xml --resources osxpkgresources amtc_$(AMTCV)-unsigned.pkg
139	-productsign --sign 'Developer ID Installer' amtc_$(AMTCV)-unsigned.pkg amtc_$(AMTCV)-OSX_$(shell sw_vers -productVersion|cut -d. -f1-2).pkg
140
141# build and install package for current platform. requires sudo privileges.
142install-package: package
143	test "$(PKGTYPE)" = "osxpkg" && sudo installer -tgt / -pkg amtc_$(AMTCV)-OSX_$(shell sw_vers -productVersion|cut -d. -f1-2).pkg || true
144	test "$(PKGTYPE)" = "deb"    && (sudo dpkg -i ../amtc_*.deb ; sudo apt-get install -f) || true
145	test "$(PKGTYPE)" = "rpm"    && sudo yum localinstall $(RPMBUILD)/RPMS/*/*.rpm || true
146	@echo
147	@echo "Done! If no errors occured, try visiting http://localhost/amtc-web/ now."
148	@echo "After completing installation, change the default admin password ('amtc')!"
149
150# uninstall any installed package and remove ANY file/directory created by amtc-web
151purge:
152ifeq ($(PKGTYPE),osxpkg)
153	-sudo pkgutil --forget ch.hacker.amtc
154	-sudo launchctl disable system/ch.hacker.amtc-web
155	-sudo launchctl remove ch.hacker.amtc-web
156else ifeq ($(PKGTYPE),deb)
157	-sudo apt-get purge -y amtc
158else ifeq ($(PKGTYPE),rpm)
159	-sudo yum remove -y amtc amtc-web amtc-debuginfo
160endif
161	sudo rm -rf /etc/amtc-web /var/lib/amtc-web /usr/share/amtc-web /etc/{httpd,apache2}/{other,conf.d}/amtc-web_httpd.conf /Library/LaunchDaemons/ch.hacker.amtc-web.plist
162
163
164# build farm / 'internal' use only: build and fetch releases on/from remote VMs
165farmbuild:
166	test `uname -s` = 'Darwin' || exit 1
167	mkdir -p releases/$(AMTCV)
168	make osxpkg
169	cp amtc_$(AMTCV)*.pkg releases/$(AMTCV)
170	for host in $(HOSTS_rpm) $(HOSTS_deb); do ssh $$host 'cd checkouts/amtc; git pull; make debclean package'; done
171	for host in $(HOSTS_rpm); do scp $$host:rpmbuild/RPMS/*/*.rpm releases/$(AMTCV); done
172	for host in $(HOSTS_deb); do file=`ssh $$host "cd checkouts; ls -1 amtc_*.deb"`; scp $$host:checkouts/$$file releases/$(AMTCV)/$${host}_$${file}; done
173
174
175#
176.PHONY:	amtc-web
177