#
# Copyright (c) 2000 Blue Mug, Inc.  All Rights Reserved.
#

# if you want to support Armadillo-J firmware update command "firmupdate"
# uncomment the followiing line
CFLAGS_EXTRA := -DAJ_FIRMUPDATE_SUPPORT

CFLAGS := -Wall -g -I../../../include \
	-DHERMIT_VERSION='"$(HERMIT_VERSION)"' \
	$(CFLAGS_EXTRA) -D_CONSOLE
# Suggested CFLAGS for optimization:
#CFLAGS :=-Wall -O2 -fomit-frame-pointer -fstrength-reduce

SRCS := crc.c download.c erase.c eth.c ethutil.c flash.c main.c memmap.c \
	options.c region.c serial.c target.c util.c console.c md5sum.c
OBJS := $(SRCS:.c=.o)
DEPS := $(SRCS:.c=.d)

BIN := hermit

WHOAMI := $(shell whoami)
ifneq ($(WHOAMI),root)
	SUDO := sudo
endif

.PHONY: all default suid $(BIN)-suid
default: all
all: $(BIN)
suid: $(BIN)-suid
$(BIN)-suid: .suid.stamp

.suid.stamp: $(BIN)
	$(SUDO) chown root $(BIN)
	$(SUDO) chmod u+s,go-w $(BIN)
	touch .suid.stamp

$(BIN): $(OBJS)
	rm -f .suid.stamp
	$(CC) $(CFLAGS) -o $@ $^

# automated dependency checking
ifeq (,$(filter clean scrub,$(MAKECMDGOALS)))
-include $(DEPS)

DEPENDS := ../../../depend.sh
%.d: %.c
	$(DEPENDS) $< $@ $(CFLAGS)
endif

# installation

.PHONY: install
install:
	$(INSTALL) -m 4755 -o root -g root hermit $(INSTALLPREFIX)/bin/

# housekeeping
.PHONY: clean
clean:
	rm -f .suid.stamp
	rm -f $(OBJS) $(BIN)
.PHONY: scrub
scrub: clean
	rm -f $(DEPS)
.PHONY: distclean
distclean: scrub
	rm -f *~