CC      = gcc
CFLAGS  = -O2 -Wall -Wextra -pthread \
          -I. -Iconfig -Iutil -Ihttp -Inotifs -Iqueue -Iintegrations
LDFLAGS = -pthread -lcurl
TARGET  = iptv-dl

SRCS = main.c \
       config/config.c \
       util/buf.c \
       util/json.c \
       http/http.c \
       http/server.c \
       http/handlers.c \
       notifs/notify.c \
       queue/iptv_api.c \
       queue/queue.c \
       integrations/discord.c \
       integrations/jellyfin.c

OBJS = $(SRCS:.c=.o)

# Install paths (override: make install PREFIX=/usr/local)
PREFIX     = /usr/local
BINDIR     = $(PREFIX)/bin
SHAREDIR   = $(PREFIX)/share/iptv-dl
SYSCONFDIR = /etc/iptv-downloader

all: $(TARGET)

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

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

install: $(TARGET)
	install -d $(BINDIR) $(SHAREDIR)/css $(SHAREDIR)/js $(SHAREDIR)/html $(SYSCONFDIR)
	install -m 755 $(TARGET)                       $(BINDIR)/iptv-dl
	install -m 644 static/css/iptv.css             $(SHAREDIR)/css/iptv.css
	install -m 644 static/js/iptv.js               $(SHAREDIR)/js/iptv.js
	install -m 644 static/js/downloads.js          $(SHAREDIR)/js/downloads.js
	install -m 644 static/js/series_show.js        $(SHAREDIR)/js/series_show.js
	install -m 644 static/html/header.html         $(SHAREDIR)/html/header.html
	install -m 644 static/html/footer.html         $(SHAREDIR)/html/footer.html
	@echo ""
	@echo "Installed to $(BINDIR)/iptv-dl"
	@echo "Create config at ~/.iptv-downloader/config.json or $(SYSCONFDIR)/config.json"
	@echo "Run: iptv-dl --dump-config   (to see active defaults)"

test: tests/test_runner
	./tests/test_runner

tests/test_runner: tests/test_json.c util/json.c util/buf.c
	$(CC) $(CFLAGS) $^ -o $@

clean:
	rm -f $(OBJS) $(TARGET) tests/test_runner

.PHONY: all install clean test
