LIB_NAME=	bsdiskinfo
SRC=		lua_bsdiskinfo.c

PREFIX?=	/usr/local

# If LUA_VER is explicitly passed (e.g. make LUA_VER=5.3), use it.
# Otherwise, discover the latest installed version via pkg-config dynamically.
.if !empty(LUA_VER)
LUA_PC=		lua-${LUA_VER}
.else
LUA_PC!=	pkg-config --list-all | grep -E '^lua-[0-9]' | awk '{print $$1}' | sort -V | tail -n 1
.endif

# Extract compilation flags and target C modules directory from pkg-config
LUA_CFLAGS!=	pkg-config --cflags ${LUA_PC}
LUA_MODDIR!=	pkg-config --variable=INSTALL_CMOD ${LUA_PC}

# Include project headers and system-wide Lua layouts
CFLAGS+=	-O2 -fPIC -Wall -I../../include ${LUA_CFLAGS}

# Link against our core standalone library built in the root directory
LDFLAGS+=	-shared -L../.. -lbsdiskinfo -Wl,-rpath,${PREFIX}/lib:.

all: ${LIB_NAME}.so

${LIB_NAME}.so: ${SRC}
	${CC} ${CFLAGS} ${LDFLAGS} -o ${LIB_NAME}.so ${SRC}

clean:
	rm -f ${LIB_NAME}.so

install: all
	mkdir -p ${DESTDIR}${LUA_MODDIR}
	install -m 755 ${LIB_NAME}.so ${DESTDIR}${LUA_MODDIR}/${LIB_NAME}.so

.PHONY: all clean install

