# SPDX-License-Identifier: GPL-3.0-or-later
# pomelo-netscan — 網路探索（設備探索、埠與服務、OS 與型號、位置推斷、衝突與異常、埠政策、封包異常、安全評估、拓樸圖）
PREFIX ?= /usr/local
CFLAGS ?= -Os -pipe -Wall -Wextra -Wno-unused-parameter -Wno-format-truncation
override CFLAGS += -fstack-protector-strong -fstack-clash-protection -D_FORTIFY_SOURCE=3 -fPIE -pie
override LDFLAGS += -Wl,-z,relro,-z,now
POMELO_CFLAGS ?= $(shell pkg-config --cflags pomelo)
POMELO_LIBS   ?= $(shell pkg-config --libs pomelo)
CAIRO_CFLAGS ?= $(shell pkg-config --cflags cairo)
CAIRO_LIBS   ?= $(shell pkg-config --libs cairo)

# 核心：不需要顯示器與 libpomelo（無頭模式、單元測試直接編）
CORE = util.c oui.c ber.c crypto.c snmp.c snmp3.c cve.c link.c lldp.c rules.c probe.c disco.c scan.c policy.c store.c conflict.c assess.c topo.c topo-draw.c export.c cli.c pkt.c
GUI  = gui.c
HDR  = cve.h cve-builtin.h ns.h topo.h pkt.h oui-builtin.h
DBG  = -O1 -g -Wall -Wextra -Wno-unused-parameter -Wno-format-truncation -DNS_CORE_TEST

all: pomelo-netscan pomelo-netscan-helper pomelo-netscan.png

pomelo-netscan: $(CORE) $(GUI) main.c $(HDR)
	$(CC) $(CFLAGS) $(POMELO_CFLAGS) $(CAIRO_CFLAGS) -o $@ $(CORE) $(GUI) main.c $(POMELO_LIBS) $(CAIRO_LIBS) -lm -lpthread -ldl $(LDFLAGS)

# 特權輔助程式：只依賴 util／lldp／pkt，不連 libpomelo、cairo
pomelo-netscan-helper: helper.c util.c lldp.c pkt.c ns.h pkt.h
	$(CC) $(CFLAGS) -DNS_CORE_TEST -o $@ helper.c util.c lldp.c pkt.c -lpthread -lm $(LDFLAGS)

pomelo-netscan.png: pomelo-netscan
	LD_LIBRARY_PATH="$(LD_LIBRARY_PATH)" ./pomelo-netscan --icon $@

# 無圖形介面的命令列版（實驗室與容器測試）
test/ns-cli: test/ns-cli.c $(CORE) $(HDR)
	$(CC) $(DBG) -o $@ test/ns-cli.c $(CORE) $(CAIRO_CFLAGS) $(CAIRO_LIBS) -lm -lpthread -ldl

test/test-netscan: test/test-netscan.c $(CORE) $(HDR)
	$(CC) $(DBG) -o $@ test/test-netscan.c $(CORE) $(CAIRO_CFLAGS) $(CAIRO_LIBS) -lm -lpthread -ldl

test: test/test-netscan test/ns-cli
	TESTTMP=$${TESTTMP:-/tmp} TZ=Asia/Taipei test/test-netscan test/fixtures
	sh test/link-test.sh
	@if command -v python3 >/dev/null 2>&1 && command -v openssl >/dev/null 2>&1; then SCRATCH=$${TESTTMP:-/tmp} sh test/snmpv3-test.sh && SCRATCH=$${TESTTMP:-/tmp} sh test/ap-clients-test.sh; else echo "略過 SNMPv3／AP 端對端測試（需要 python3 與 openssl 命令列）"; fi

install: all
	install -Dm755 pomelo-netscan $(DESTDIR)$(PREFIX)/bin/pomelo-netscan
	install -Dm644 pomelo-netscan.desktop $(DESTDIR)$(PREFIX)/share/applications/pomelo-netscan.desktop
	install -Dm644 pomelo-netscan.png $(DESTDIR)$(PREFIX)/share/icons/hicolor/256x256/apps/pomelo-netscan.png
	# 特權輔助程式：不是 setuid；預設由 pkexec 取得權限（polkit 政策），管理員也可以改用 setcap cap_net_raw+ep
	install -Dm755 pomelo-netscan-helper $(DESTDIR)$(PREFIX)/libexec/pomelo/pomelo-netscan-helper
	install -Dm644 tw.fedora.pomelo.netscan.policy $(DESTDIR)$(PREFIX)/share/polkit-1/actions/tw.fedora.pomelo.netscan.policy
	sed -i 's|/usr/libexec/pomelo|$(PREFIX)/libexec/pomelo|' $(DESTDIR)$(PREFIX)/share/polkit-1/actions/tw.fedora.pomelo.netscan.policy
	# 每週評估的 systemd 使用者定時器（預設關閉：不放進 timers.target.wants，由設定頁的開關啟用）
	for u in service timer; do install -Dm644 systemd/pomelo-netscan-assess.$$u $(DESTDIR)$(PREFIX)/lib/systemd/user/pomelo-netscan-assess.$$u; done
	sed -i 's|/usr/bin/pomelo-netscan|$(PREFIX)/bin/pomelo-netscan|' $(DESTDIR)$(PREFIX)/lib/systemd/user/pomelo-netscan-assess.service

clean:
	rm -f pomelo-netscan pomelo-netscan-helper pomelo-netscan.png *.o test/test-netscan test/ns-cli

.PHONY: all clean test install
