# Install rc.local file
USER := $(shell whoami)
PATH := /usr/local/go/bin:$(PATH)

.SILENT:copy_files
.SILENT:hard_copy_files
.SILENT:build
.SILENT:clean

install: as_root copy_files build
	@echo "Install complete"

install_hard: as_root hard_copy_files build
	@echo "Install complete"

as_root:
ifneq ($(USER),root)
	$(error Must run as superuser!)
endif

copy_files:
	if [ -f /etc/rc.local ] ; then \
		echo "/etc/rc.local left unmodified" ; \
	else \
		cp rc.local /etc; \
	fi
	if [ -f /etc/netsender.conf ] ; then \
		echo "/etc/netsender.conf left unmodified" ; \
	else \
		cp netsender.conf /etc; \
	fi

hard_copy_files:
	if [ -f /etc/rc.local ] ; then \
		echo "Backed up rc.local to /etc/rc.local.bak" ; \
		cp /etc/rc.local /etc/rc.local.bak ; \
	fi
	cp -f rc.local /etc
	if [ -f /etc/netsender.conf ] ; then \
		echo "Backed up netsender.conf to /etc/netsender.conf.bak" ; \
		cp /etc/netsender.conf /etc/netsender.conf.bak ; \
	fi
	cp -f netsender.conf /etc

build:
	if grep -q 'Raspberry Pi 3' '/proc/device-tree/model'; then \
		echo "Compiling for Raspberry pi 3";\
		go build -tags pi3;\
	else \
		echo "Compiling for Raspberry pi 0";\
		go build -tags pi0;\
	fi