# Using the default compiler in Ubuntu 16.04:
# gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
CC := gcc

# -g: include debugging information
# -Og: do a little optimization, compatible with debugging
# -m32: compile a 32-bit binary, even on a 64-bit machine
# -m64: compile a 64-bit binary
# -Wall: enable most warning messages
# -fno-stack-protector: disable stack canaries
# -z execstack: allow code execution on the stack
# -z norelro: disable a security mechanism
BC_CFLAGS := -g -Wall -fno-stack-protector -z execstack -z norelro

all: bcmta

bcmta: bcmta.c
	$(CC) -m32 $(BC_CFLAGS) $< -o $@

bcecho: bcecho.c
	$(CC) -m32 -g -Wall -fno-stack-protector -z execstack -z norelro  $< -o $@

ifeq ($(shell which rootshell),/bin/rootshell)

EXE:=bcmta
EXEPATH:=/usr/bin/$(EXE)

install: bcmta
	@# The bcmta executable is setuid root
	sudo -v && sudo cp $(EXE) $(EXEPATH)
	sudo -v && sudo chown root:root $(EXEPATH)
	sudo -v && sudo chmod a+rx $(EXEPATH)
	sudo -v && sudo chmod u+s $(EXEPATH)

else
install:
	@echo "Don't install this on a real machine!"
endif
