# Makefile for WebSocket-based UI communication
#
# Copyright (C) 2025 Rhizomatica
# Author: Rafael Diniz <rafael@riseup.net>
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

include ../config.mk

CFLAGS_COMMON = $(COMMON_CFLAGS) -Wno-stringop-truncation -I../datalink_arq -I../data_interfaces -I../common -I../modem/freedv

# WebSocket server flags.  WS_TLS_CFLAGS is set by config.mk and carries
# -DWS_HAVE_OPENSSL=1 plus the OpenSSL include path where wss:// is
# supported (Linux); elsewhere it is empty and the server refuses wss://
# at startup instead of failing per connection.
WS_CFLAGS = $(COMMON_CFLAGS) -Iwebsocket -I../common $(WS_TLS_CFLAGS)

# Objects
OBJS = ui_communication.o ui_status.o ui_devices.o ui_history.o
WS_OBJS = websocket/mercury_websocket.o websocket/ws_server.o \
          websocket/ws_crypto.o websocket/ws_frame.o websocket/ws_json.o \
          websocket/ws_tls.o \
          websocket/web_packed.o

all: $(OBJS) $(WS_OBJS)

ui_communication.o: ui_communication.c ui_communication.h ui_status.h
	$(CC) $(CFLAGS_COMMON) -c ui_communication.c -o ui_communication.o

ui_devices.o: ui_devices.c ui_communication.h ui_status.h
	$(CC) $(CFLAGS_COMMON) -c ui_devices.c -o ui_devices.o

ui_status.o: ui_status.c ui_status.h
	$(CC) $(CFLAGS_COMMON) -c ui_status.c -o ui_status.o

ui_history.o: ui_history.c ui_history.h
	$(CC) $(CFLAGS_COMMON) -c ui_history.c -o ui_history.o

websocket/mercury_websocket.o: websocket/mercury_websocket.c websocket/mercury_websocket.h
	$(CC) $(WS_CFLAGS) -c websocket/mercury_websocket.c -o websocket/mercury_websocket.o

websocket/ws_server.o: websocket/ws_server.c websocket/ws_server.h
	$(CC) $(WS_CFLAGS) -c websocket/ws_server.c -o websocket/ws_server.o

websocket/ws_crypto.o: websocket/ws_crypto.c websocket/ws_crypto.h
	$(CC) $(WS_CFLAGS) -c websocket/ws_crypto.c -o websocket/ws_crypto.o

websocket/ws_frame.o: websocket/ws_frame.c websocket/ws_frame.h
	$(CC) $(WS_CFLAGS) -c websocket/ws_frame.c -o websocket/ws_frame.o

websocket/ws_json.o: websocket/ws_json.c websocket/ws_json.h
	$(CC) $(WS_CFLAGS) -c websocket/ws_json.c -o websocket/ws_json.o

websocket/ws_tls.o: websocket/ws_tls.c websocket/ws_tls.h
	$(CC) $(WS_CFLAGS) -c websocket/ws_tls.c -o websocket/ws_tls.o

# The UI pages are compiled INTO the binary.  Generated at build time from
# websocket/web/ and never committed: a checked-in generated file drifts from
# the pages it came from, and the point of embedding is that the served UI
# cannot disagree with the shipped one.  Depends on every file in web/, so
# editing a page rebuilds it.
#
# The generator is POSIX sh + od + tr, with no interpreter dependency: the
# Windows x64 cross-compile image has no python3, so a python generator breaks
# that target and only that target.
websocket/web_packed.c: websocket/pack_web.sh $(wildcard websocket/web/*)
	sh websocket/pack_web.sh websocket/web websocket/web_packed.c

websocket/web_packed.o: websocket/web_packed.c websocket/web_assets.h
	$(CC) $(WS_CFLAGS) -c websocket/web_packed.c -o websocket/web_packed.o

clean:
	rm -f $(OBJS) $(WS_OBJS) websocket/web_packed.c *.d websocket/*.d

.PHONY: all clean

# Pull in the header dependencies generated by -MMD (see config.mk).  Include
# only the .d files next to the objects THIS Makefile compiles: paths inside a
# .d are relative to the directory the compiler ran in, so they only resolve
# correctly from the Makefile that built them.
-include $(wildcard *.d websocket/*.d)
# ...and rebuild them when their flags change (BUILD_CONFIG, see config.mk).
$(wildcard *.o websocket/*.o): $(BUILD_CONFIG)
