# @(#) $Id: Makefile 2 2022-02-25 17:44:09Z perette $

IOD=x10-snap/x10iod
CLI=x10-snap/cli
VERSION=2022-02-25

ifndef X10MANAGER
X10MANAGER:=x10
endif

all clean templates:
	make -C x10iod $@
	make -C cli $@

help:
	@ echo "X10-snap Makefile targets---run as a standard user:"
	@ echo "  all (or unspecified): Build software."
	@ echo "  install: Installs man pages and system binaries."
	@ echo "  uninstall: Removes man pages and system binaries."
	@ echo "  templates: Installs crontab, config and action files if not already present."
	@ echo "Special targets---must run as root or with sudo:"
	@ echo "  createuser: Creates a suitable $(X10MANAGER) user account for some Linuxes."
	@ echo "  removeuser: Removes the $(X10MANAGER) user account and its home directory."
	@ echo "  The environment variable X10MANAGER overrides $(X10MANAGER)."

install: all
	sudo sh -c "make -C x10iod installroot && make -C cli installroot"
	sh -C cli install
	sh -C x10iod install

uninstall:
	sudo sh -c "make -C x10iod uninstallroot && make -C cli uninstallroot"
	sh -C x10iod uninstall

createuser:
	@ groups $(X10MANAGER) || /usr/sbin/groupadd -f $(CREATEUSER)
	@ id $(X10MANAGER) || /usr/sbin/useradd -c "X10 daemon manager" -g $(CREATEUSER) -G staff,dialout,audio,sudo,crontab -d /home/$(CREATEUSER) $(CREATEUSER)
	@ # Add x10 to pi group if it exists
	@ ! getent group pi || /usr/sbin/usermod --append -G pi $(X10MANAGER)

removeuser:
	userdel -r $(X10MANAGER)
	groupdel $(X10MANAGER)

release:
	rm -f ../x10-snap-$(VERSION).tar.bz2
	cd .. && tar cfv x10-snap-$(VERSION).tar \
		$$(find x10-snap -name '*.[CH]') \
		x10-snap/README x10-snap/ChangeLog \
		x10-snap/Makefile $(IOD)/Makefile $(CLI)/Makefile \
		$(IOD)/x10event $(IOD)/x10iod.ini \
		$(IOD)/x10iod.1 $(IOD)/x10event.5 \
		$(CLI)/x10cli.1 $(CLI)/action \
		x10-snap/contrib/x10iod.service \
		x10-snap/contrib/x10rotate \
		x10-snap/contrib/modemmon \
		x10-snap/contrib/x10iod.init \
		x10-snap/contrib/sumpd \
		x10-snap/contrib/jukebox 
	bzip2 ../x10-snap-$(VERSION).tar
	mkdir -p ../test
	rm -f ../test/x10-snap-$(VERSION).tar.bz2
	cp ../x10-snap-$(VERSION).tar.bz2 ../test
	bunzip2 ../test/x10-snap-$(VERSION).tar.bz2
	cd ../test && tar xfv x10-snap-$(VERSION).tar
	rm -f ../test/x10-snap-$(VERSION).tar
	cd ../test/x10-snap && make install
	rm -rf ../test/x10-snap
	echo "Release seems good."
