# Makefile for libuniquecheck

CC = gcc
CFLAGS = -fPIC -Wall -O2 -Iinclude
LDFLAGS = -shared -Wl,-soname,libuniquecheck.so.1
VERSION = 1.0.0
SONAME = 1

SRCDIR = src
INCDIR = include
LIBDIR = lib
OBJDIR = obj

SOURCES = $(SRCDIR)/uniquecheck.c $(SRCDIR)/uniquecheck_user.c
OBJECTS = $(OBJDIR)/uniquecheck.o $(OBJDIR)/uniquecheck_user.o
HEADERS = $(INCDIR)/uniquecheck.h

TARGET = $(LIBDIR)/libuniquecheck.so.$(VERSION)
SONAME_LINK = $(LIBDIR)/libuniquecheck.so.$(SONAME)
LINKER_NAME = $(LIBDIR)/libuniquecheck.so

all: directories $(TARGET)

directories:
	mkdir -p $(OBJDIR) $(LIBDIR)

$(OBJDIR)/%.o: $(SRCDIR)/%.c $(HEADERS)
	$(CC) $(CFLAGS) -c $< -o $@

$(TARGET): $(OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(OBJECTS)
	ln -sf libuniquecheck.so.$(VERSION) $(SONAME_LINK)
	ln -sf libuniquecheck.so.$(SONAME) $(LINKER_NAME)

install: all
	install -d $(DESTDIR)/usr/lib
	install -m 644 $(TARGET) $(DESTDIR)/usr/lib/
	cd $(DESTDIR)/usr/lib && \
		ln -sf libuniquecheck.so.$(VERSION) libuniquecheck.so.$(SONAME) && \
		ln -sf libuniquecheck.so.$(SONAME) libuniquecheck.so
	install -d $(DESTDIR)/usr/include
	install -m 644 $(HEADERS) $(DESTDIR)/usr/include/

uninstall:
	rm -f $(DESTDIR)/usr/lib/libuniquecheck.so*
	rm -f $(DESTDIR)/usr/include/uniquecheck.h

clean:
	rm -rf $(OBJDIR) $(LIBDIR) test

example: all
	$(CC) -o test examples/test.c -Iinclude -Llib -luniquecheck -Wl,-rpath,lib

.PHONY: all clean install uninstall example directories