CC       = gcc
CFLAGS   = -g -O2 -Wall -Wextra -Wno-unused-parameter
SNMP_CFLAGS := $(shell net-snmp-config --cflags 2>/dev/null)
SNMP_LIBS   := $(shell net-snmp-config --libs 2>/dev/null | sed 's/-specs=[^ ]*//g')

ifeq ($(SNMP_LIBS),)
SNMP_LIBS = -lnetsnmp
endif

# Detect net-snmp 5.9+ SHA-2 auth and AES-192/256 priv protocol support
HASH := \#
SNMP_FEAT_FLAGS :=
HAS_SHA2 := $(shell printf '$(HASH)include <net-snmp/net-snmp-config.h>\n$(HASH)include <net-snmp/net-snmp-includes.h>\nint main(void){return sizeof(usmHMAC128SHA224AuthProtocol);}\n' | $(CC) $(SNMP_CFLAGS) -x c - -o /dev/null $(SNMP_LIBS) 2>/dev/null && echo yes)
HAS_AES_EXT := $(shell printf '$(HASH)include <net-snmp/net-snmp-config.h>\n$(HASH)include <net-snmp/net-snmp-includes.h>\nint main(void){return sizeof(usmAES192PrivProtocol);}\n' | $(CC) $(SNMP_CFLAGS) -x c - -o /dev/null $(SNMP_LIBS) 2>/dev/null && echo yes)
ifeq ($(HAS_SHA2),yes)
SNMP_FEAT_FLAGS += -DHAVE_USM_SHA2
endif
ifeq ($(HAS_AES_EXT),yes)
SNMP_FEAT_FLAGS += -DHAVE_USM_AES_EXT
endif

TARGET  = snmp_interface_walk
SOURCES = snmp_interface_walk.c

DESTDIR = /usr/local/nagios/libexec

all: $(TARGET)

$(TARGET): $(SOURCES)
	$(CC) $(CFLAGS) $(SNMP_CFLAGS) $(SNMP_FEAT_FLAGS) -o $@ $< $(SNMP_LIBS)

install: $(TARGET)
	install -m 755 $(TARGET) $(DESTDIR)/

clean:
	rm -f $(TARGET) *.o

.PHONY: all install clean
