# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
# Copyright (c) 2024-2025 Brett Sheffield <bacs@librecast.net>

SHELL = /bin/sh
PROGRAM := @PACKAGE_NAME@
VERSION := @PACKAGE_VERSION@
.SUFFIXES:
.SUFFIXES: .c .o
CFLAGS := @CFLAGS@ -I.
LDFLAGS := @LDFLAGS@
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
LEX = @LEX@
YACC = @YACC@
PREFIX = @prefix@
OBJECTS := lex.yy.o y.tab.o agent.o key.o lcagent.o log.o state.o
HEADERS := agent.h state.h log.h lex.h
LDLIBS += @LIBS@
bindir = @bindir@
builddir = @builddir@
datadir = @datadir@
datarootdir = @datarootdir@
exec_prefix = @exec_prefix@
mandir = @mandir@
prefix = @prefix@
srcdir = @srcdir@

all: $(PROGRAM)

$(PROGRAM): $(OBJECTS) agent.h
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $(OBJECTS) $(LDLIBS)

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

lex.yy.o: lex.yy.c agent.h
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<

y.tab.o: y.tab.c y.tab.h
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<

y.tab.h: grammar.y agent.h
	$(YACC) -d $<

y.tab.c: grammar.y agent.h

lex.h: lex.yy.c agent.h
lex.yy.c: lexer.l y.tab.h agent.h
	$(LEX) --header-file=lex.h lexer.l


install: all
	$(INSTALL) -d $(DESTDIR)$(bindir)
	$(INSTALL_PROGRAM) $(PROGRAM) $(DESTDIR)$(bindir)

.PHONY: clean realclean uninstall

uninstall:
	$(RM) $(DESTDIR)$(bindir)/$(PROGRAM)

clean:
	$(RM) *.o
	$(RM) -f y.tab.c y.tab.h lex.yy.c lex.h

realclean: clean
	$(RM) Makefile config.h
