# Makefile to install qemu-web-desktop.
# just type: make install
#
# required:
#   apache2 libapache2-mod-perl2 novnc websockify qemu-kvm bridge-utils qemu
#   iptables dnsmasq libcgi-pm-perl liblist-moreutils-perl libsys-cpu-perl
#   libsys-cpuload-perl libsys-meminfo-perl libnet-dns-perl confget
#   libproc-background-perl libproc-processtable-perl libemail-valid-perl 
#   libnet-smtps-perl libmail-imapclient-perl libnet-ldap-perl 
#   libemail-valid-perl libjson-perl libwww-perl libguestfs-tools libapache2-mpm-itk
#
# NOTES:
#   you may have to tune the path to /etc/apache2/conf-available/serve-cgi-bin.conf
#   or other locations. The current makefile works on Debian-class systems.

all:

install:
	install -D -d $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html/desktop
	install -D -d $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html/desktop/images
	install -D -d $(DESTDIR)$(prefix)/var/lib/qemu-web-desktop/machines
	install -D -d $(DESTDIR)$(prefix)/var/lib/qemu-web-desktop/snapshots
	install -D -d $(DESTDIR)$(prefix)/etc/qemu-web-desktop/
	install -D html/desktop/index.html $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html/desktop
	install -D html/desktop/style.css $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html/desktop
	install -D html/desktop/images/* $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html/desktop/images
	install -D html/desktop/machines/* $(DESTDIR)$(prefix)/var/lib/qemu-web-desktop/machines
	install -D cgi-bin/qemu-web-desktop.pl $(DESTDIR)$(prefix)/usr/lib/cgi-bin/qemu-web-desktop.pl
	install -D bin/qwdctl $(DESTDIR)$(prefix)/usr/bin/
	install -D machines.conf $(DESTDIR)$(prefix)/etc/qemu-web-desktop/
	install -D config.pl $(DESTDIR)$(prefix)/etc/qemu-web-desktop/
	# configure accounts and permissions
	chmod 755 /etc/qemu-ifup
	adduser --system --home $(DESTDIR)$(prefix)/var/lib/qemu-web-desktop --force-badname _qemu-web-desktop
	chown -R _qemu-web-desktop $(DESTDIR)$(prefix)/var/lib/qemu-web-desktop
	find $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html -type f -exec chmod a+r {} +
	find $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html -type d -exec chmod a+rx {} +
	chmod 755 $(DESTDIR)$(prefix)/usr/lib/cgi-bin/qemu-web-desktop.pl
	# add apache configuration for cgi perl
	install apache.conf $(DESTDIR)$(prefix)/etc/apache2/conf-available/qemu-web-desktop.conf
	a2enconf qemu-web-desktop
	a2enmod cgi
	a2enmod include
	service apache2 restart
	qwdctl download
	ln -sf $(DESTDIR)$(prefix)/var/lib/qemu-web-desktop/machines.html $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html/desktop
	ln -sf $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop/html/desktop $(DESTDIR)$(prefix)/var/www/html/
	ln -sf $(DESTDIR)$(prefix)/var/lib/qemu-web-desktop/machines $(DESTDIR)$(prefix)/var/www/html/desktop/

uninstall:
	-rm -rf $(DESTDIR)$(prefix)/usr/share/qemu-web-desktop
	-rm -f  $(DESTDIR)$(prefix)/usr/lib/cgi-bin/qemu-web-desktop.pl
	-rm -rf $(DESTDIR)$(prefix)/var/www/html/desktop/
	-rm -rf $(DESTDIR)$(prefix)/etc/qemu-web-desktop/
	-rm -rf $(DESTDIR)$(prefix)/var/lib/qemu-web-desktop/
	-rm $(DESTDIR)$(prefix)/etc/apache2/conf-available/qemu-web-desktop.conf
	deluser _qemu-web-desktop

.PHONY: all install uninstall

