-CPPFLAGS=-I.. $(shell pkg-config --silence-errors --cflags lua5.1)
-LIBS=-L.. -luci $(shell pkg-config --silence-errors --libs lua5.1)
+# find lua prefix
+LUA_PREFIX=$(firstword \
+ $(foreach prefix,$(PREFIX_SEARCH),\
+ $(if $(wildcard $(prefix)/include/lua.h),$(prefix)) \
+ ) \
+)
+
+libdir=$(prefix)/libs
+luadir=$(if $(LUA_PLUGINDIR),$(LUA_PLUGINDIR),$(libdir)/lua/$(LUA_VERSION))
+luainc=$(shell pkg-config --silence-errors --cflags lua$(LUA_VERSION))
+
+CPPFLAGS=-I.. $(if $(luainc),$(luainc), -I$(LUA_PREFIX)/include)
+LIBS=-L.. -luci $(shell pkg-config --silence-errors --libs lua$(LUA_VERSION))