# a gnu-make-ism
VERSION:=$(shell cat Version)

PROG=fallback-reboot
SRCS=$(PROG).c
OBJS=$(PROG).o
CC=@CC@
CFLAGS=@GCCFLAGS@ \
	-DVERSION=\"$(VERSION)\" \
	@DEFS@

LIBS=@LIBS@

.PHONY: all
all: $(PROG) lint README

lint:
	./this-pylint --which-2 None --to-pylint fallback-reboot-client
	pycodestyle --max-line-length 132 fallback-reboot-client
	pydocstyle fallback-reboot-client

$(PROG): $(SRCS) Makefile
	rm -f $(PROG)
	$(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LIBS)

README: README.in Makefile
	sed 's/VERSION/$(VERSION)/' < README.in > README

go: $(PROG)
	./fallback-reboot-client localhost 3002
	#./$(PROG)

install: $(PROG)
	@echo
	./install-rc-script
	# create our sbin directory, and all parent directories as required
	mkdir -p @prefix@/sbin
	rm -f @prefix@/sbin/fallback-reboot
	cp $(PROG) @prefix@/sbin/.
	cp gen-pas @prefix@/sbin/.
	chmod 755 gen-pas
	chmod 755 @prefix@/sbin/$(PROG) @prefix@/sbin/gen-pas
	chown root @prefix@/sbin/$(PROG) @prefix@/sbin/gen-pas
	cp fallback-reboot-client @prefix@/sbin/.
	chmod 755 @prefix@/sbin/fallback-reboot-client @prefix@/sbin/gen-pas
	chown root @prefix@/sbin/fallback-reboot-client
	./install-bufsock
	@echo
	@echo Please be sure to create a fallback-reboot password by either
	@echo using gen-pas, running S22fallback-reboot or rebooting.  Also,
	@echo be sure to copy that password to another system that is not
	@echo dependent on this one, so you will have the password when you
	@echo 'actually need it! :)'

clean:
	rm -f *.o $(PROG) README core
	rm -f configure.lineno
	rm -f found-dir
	rm -f gen-pas

pristine: clean
	rm -rf autom4te.cache config.status *.pyc confdefs.h conftest.c
	rm -rf config.log configure.lineno Makefile runlevel gen-pas
	rm -rf found-dir
	rm -f Makefile
	rm -f configure.lineno
	rm -f Makefile
	rm -f S22fallback-reboot 

.PHONY: web
web: pristine README
	(echo '<pre>'; sed 's/^-----$$/<hr>/' < README; echo '</pre>') > $(HOME)/public_html/fallback-reboot/README.html
	rm README
	tar cvf - . | gzip -9 > $(HOME)/public_html/fallback-reboot/fallback-reboot.tar.gz