# Makefile for wssshd2 - Generated by configure.sh
# Do not edit manually, run ./configure.sh instead

CC = gcc
CFLAGS = -Wall -Wextra -O2 -I. -pthread
LDFLAGS = -lssl -lcrypto -lm -luuid -lsqlite3
PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man
CONFIGDIR = /etc

# Source files
SRCS = main.c config.c tunnel.c terminal.c vnc.c websocket.c websocket_protocol.c web.c assets.c ssl.c
OBJS = $(SRCS:.c=.o)

# Target
TARGET = wssshd

.PHONY: all clean install uninstall

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# Dependencies
main.o: main.c config.h websocket.h web.h
config.o: config.c config.h
tunnel.o: tunnel.c tunnel.h websocket.h
terminal.o: terminal.c terminal.h config.h
vnc.o: vnc.c vnc.h config.h
websocket.o: websocket.c websocket.h websocket_protocol.h config.h
websocket_protocol.o: websocket_protocol.c websocket_protocol.h
web.o: web.c web.h terminal.h vnc.h assets.h websocket.h html_pages/index_page.h html_pages/login_page.h html_pages/terminal_page.h html_pages/vnc_page.h html_pages/users_page.h
assets.o: assets.c assets.h
ssl.o: ssl.c ssl.h

# Asset embedding (run before compilation)
assets.o: image_data.h

image_data.h: embed_assets.sh
	./embed_assets.sh

# HTML page generation
html_pages/index_page.h html_pages/login_page.h html_pages/terminal_page.h html_pages/vnc_page.h: embed_assets.sh
	./embed_assets.sh

clean:
	rm -f $(OBJS) $(TARGET) image_data.h favicon_data.h

install: $(TARGET)
	install -d $(DESTDIR)$(BINDIR)
	install -m 755 $(TARGET) $(DESTDIR)$(BINDIR)/
	install -d $(DESTDIR)$(CONFIGDIR)
	[ -f $(DESTDIR)$(CONFIGDIR)/wssshd.conf ] || install -m 644 wssshd.conf.example $(DESTDIR)$(CONFIGDIR)/wssshd.conf
	install -d $(DESTDIR)$(MANDIR)/man8
	install -m 644 wssshd.8 $(DESTDIR)$(MANDIR)/man8/

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/$(TARGET)
	rm -f $(DESTDIR)$(MANDIR)/man8/wssshd.8

# Development targets
debug: CFLAGS += -g -DDEBUG
debug: clean all

test: $(TARGET)
	@echo "Running basic functionality test..."
	./$(TARGET) --help || true

distclean: clean
	rm -f Makefile

# Help target
help:
	@echo "Available targets:"
	@echo "  all        - Build wssshd2 (default)"
	@echo "  clean      - Remove build artifacts"
	@echo "  install    - Install wssshd2 to system"
	@echo "  uninstall  - Remove wssshd2 from system"
	@echo "  debug      - Build with debug symbols"
	@echo "  test       - Run basic tests"
	@echo "  distclean  - Remove all generated files"
	@echo "  help       - Show this help"
