--- a/Makefile
+++ b/Makefile
@@ -1,58 +1,36 @@
 T=openssl
+.PHONY: install clean
 
-PREFIX		?=/usr/local
-LIB_OPTION	?= -shared 
+PKGC ?= pkg-config
 
-#Lua auto detect
-LUA_VERSION ?= $(shell pkg-config luajit --print-provides)
-ifeq ($(LUA_VERSION),)                         ############ Not use luajit
-LUAV		?= $(shell lua -e "_,_,v=string.find(_VERSION,'Lua (.+)');print(v)")
-LUA_CFLAGS	?= -I$(PREFIX)/include/lua$(LUAV)
-LUA_LIBS	?= -L$(PREFIX)/lib 
-LUA_LIBDIR	?= $(PREFIX)/lib/lua/$(LUAV)
-else
-LUAV		?= $(shell lua -e "_,_,v=string.find(_VERSION,'Lua (.+)');print(v)")
-LUA_CFLAGS	?= $(shell pkg-config luajit --cflags)
-LUA_LIBS	?= $(shell pkg-config luajit --libs)
-LUA_LIBDIR	?= $(PREFIX)/lib/lua/$(LUAV)
-endif
+LIB_OPTION	= -shared 
 
-#OS auto detect
-SYS := $(shell gcc -dumpmachine)
+# lua's package config can be under various names
+LUAPKGC := $(shell for pc in lua lua5.1 lua5.2 lua5.3; do \
+		$(PKGC) --exists $$pc && echo $$pc && break; \
+	done)
 
-ifneq (, $(findstring linux, $(SYS)))
-# Do linux things
-LDFLAGS		    = -fPIC -lrt -ldl
-OPENSSL_LIBS	?= $(shell pkg-config openssl --libs) 
-OPENSSL_CFLAGS	?= $(shell pkg-config openssl --cflags)
-CFLAGS		    = -fPIC $(OPENSSL_CFLAGS) $(LUA_CFLAGS) 
-endif
-ifneq (, $(findstring apple, $(SYS)))
-# Do darwin things
-LDFLAGS		    = -fPIC -lrt -ldl
+BUILD_DIR = $(shell pwd)
+
+# LUA include/libraries build flags
+#LUAV		?= $(shell lua -e "_,_,v=string.find(_VERSION,'Lua (.+)');print(v)")
+LUA_LIBDIR := $(shell $(PKGC) --variable=libdir $(LUAPKGC))
+LUA_CFLAGS := $(shell $(PKGC) --cflags $(LUAPKGC))
+LUA_LIBS := $(shell $(PKGC) --libs-only-L $(LUAPKGC))
+#LUA_LIBDIR	?= $(PREFIX)/lib/lua/$(LUAV)
+
+# openssl include/libraries build flags
+LDFLAGS		   += -lrt -ldl $(OPENSSL_LIBS) $(LUA_LIBS)
 OPENSSL_LIBS	?= $(shell pkg-config openssl --libs) 
 OPENSSL_CFLAGS	?= $(shell pkg-config openssl --cflags)
-CFLAGS		    = -fPIC $(OPENSSL_CFLAGS) $(LUA_CFLAGS)
-endif
-ifneq (, $(findstring mingw, $(SYS)))
-# Do mingw things
-V			= $(shell lua -e "v=string.gsub('$(LUAV)','%.','');print(v)")
-LDFLAGS		= -mwindows -lcrypt32 -lssl -lcrypto -lws2_32 $(PREFIX)/bin/lua$(V).dll 
-LUA_CFLAGS	= -DLUA_LIB -DLUA_BUILD_AS_DLL -I$(PREFIX)/include/
-CFLAGS		= $(OPENSSL_CFLAGS) $(LUA_CFLAGS)
-endif
-ifneq (, $(findstring cygwin, $(SYS)))
-# Do cygwin things
-OPENSSL_LIBS	?= $(shell pkg-config openssl --libs) 
-OPENSSL_CFLAGS  ?= $(shell pkg-config openssl --cflags)
-CFLAGS		= -fPIC $(OPENSSL_CFLAGS) $(LUA_CFLAGS)
-endif
-#custome config
+# openssl include/libraries build flags
+LOCAL_INCLUDE = -I$(BUILD_DIR)/deps
+CFLAGS		   += -fPIC -DPTHREADS $(LOCAL_INCLUDE) $(OPENSSL_CFLAGS) $(LUA_CFLAGS) 
 ifeq (.config, $(wildcard .config))
 include .config
 endif
 
-LIBNAME= $T.so.$V
+LIBNAME= $T.so
 
 #LIB_OPTION= -bundle -undefined dynamic_lookup #for MacOS X
 
@@ -68,11 +46,12 @@ OBJS=src/asn1.o src/auxiliar.o src/bio.o
 src/ec.o src/engine.o src/hmac.o src/lbn.o src/lhash.o src/misc.o src/ocsp.o src/openssl.o src/ots.o src/pkcs12.o src/pkcs7.o    \
 src/pkey.o src/rsa.o src/ssl.o src/th-lock.o src/util.o src/x509.o src/xattrs.o src/xexts.o src/xname.o src/xstore.o src/xalgor.o src/callback.o 
 
-.c.o:
-	$(CC) -c -o $@ $?
+%.o: %.c
+	$(CC) $(CFLAGS) -c $< -o $@
 
 all: $T.so
 	echo $(SYS)
+	$(CC) $(LDFLAGS) $(LIB_OPTION) $(OBJS) -o $@
 
 $T.so: $(OBJS)
 	MACOSX_DEPLOYMENT_TARGET="10.3"; export MACOSX_DEPLOYMENT_TARGET; $(CC) $(CFLAGS) $(LIB_OPTION) -o $T.so $(OBJS) $(OPENSSL_LIBS) $(LUA_LIBS) $(LDFLAGS)
