diff -Nurr lua.orig/Portfile lua/Portfile
--- lua.orig/Portfile	2011-12-19 08:17:29.000000000 -0200
+++ lua/Portfile	2011-12-19 08:27:33.000000000 -0200
@@ -1,12 +1,12 @@
-# $Id: Portfile 82708 2011-08-18 20:17:40Z ryandesign@macports.org $
+# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
 
 PortSystem          1.0
 
 name                lua
-conflicts           lua50
-set compat_version  5.1
-version             ${compat_version}.4
-revision            4
+conflicts           lua51
+set compat_version  5.2
+version             ${compat_version}.0
+revision            0
 categories          lang
 license             MIT
 platforms           darwin
@@ -21,8 +21,9 @@
 master_sites        ${homepage}/ftp/
 
 checksums           ${distfiles} \
-                    sha1    2b11c8e60306efb7f0734b747588f57995493db7 \
-                    rmd160  c867c8364295d3e4fb4e4d8ebb27fa2b2312cdef
+                    md5     f1ea831f397214bae8a265995ab1a93e \
+                    sha1    08f84c355cdd646f617f09cebea48bd832415829 \
+                    rmd160  6153bddd57493490b72e700cc99135432bf653f0
 
 depends_lib         port:readline
 build.target        macosx
@@ -30,23 +31,19 @@
 test.run            yes
 test.env            DYLD_LIBRARY_PATH=./lib
 
-patchfiles          patch-lua-5.1.4-3 \
-                    patch-Makefile.diff \
+patchfiles          patch-Makefile.diff \
                     patch-src_Makefile.diff
 
 post-patch {
     reinplace "s|/usr/local|${prefix}|g" ${worksrcpath}/Makefile ${worksrcpath}/src/luaconf.h
     reinplace "s|/man/man1|/share/man/man1|g" ${worksrcpath}/Makefile
 
-    # reinplace %VERSION% and %COMPAT_VERSION% after applying patch-dlopen.diff
+    # reinplace %VERSION% and %COMPAT_VERSION%
     reinplace "s|%VERSION%|${version}|g" ${worksrcpath}/Makefile ${worksrcpath}/src/Makefile
     reinplace "s|%COMPAT_VERSION%|${compat_version}|g" ${worksrcpath}/Makefile ${worksrcpath}/src/Makefile
-    
+
     # reinplace %PREFIX% to fix @executable_path, cf. ticket no. 28726
     reinplace "s|%PREFIX%|${prefix}|g" ${worksrcpath}/src/Makefile
-
-    # for the pkgconfig file
-    reinplace "s|/usr/local|${prefix}|g" ${worksrcpath}/etc/lua.pc
 }
 
 configure {
@@ -60,17 +57,12 @@
 destroot.target install INSTALL_TOP=${destroot}${prefix}
 post-destroot {
     xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}
-    xinstall -m 0644 -W ${worksrcpath} COPYRIGHT HISTORY README ${destroot}${prefix}/share/doc/${name}
+    xinstall -m 0644 -W ${worksrcpath} README ${destroot}${prefix}/share/doc/${name}
     xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}/html
     foreach html [glob ${worksrcpath}/doc/*.html ${worksrcpath}/doc/*.gif] {
         xinstall -m 0644 ${html} ${destroot}${prefix}/share/doc/${name}/html
     }
-    xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}/test
-    foreach test [glob ${worksrcpath}/test/README ${worksrcpath}/test/*.lua] {
-        xinstall -m 0644 ${test} ${destroot}${prefix}/share/doc/${name}/test
-    }
-    xinstall -d -m 0755 ${destroot}${prefix}/lib/pkgconfig
-    xinstall -m 0644 ${worksrcpath}/etc/lua.pc ${destroot}${prefix}/lib/pkgconfig/lua.pc
+    xinstall -m 0644 ${filespath}/lua.pc ${destroot}${prefix}/lib/pkgconfig
 }
 
 livecheck.type  regex
diff -Nurr lua.orig/files/lua.pc lua/files/lua.pc
--- lua.orig/files/lua.pc	1969-12-31 21:00:00.000000000 -0300
+++ lua/files/lua.pc	2011-12-18 23:45:18.000000000 -0200
@@ -0,0 +1,31 @@
+# lua.pc -- pkg-config data for Lua
+
+# vars from install Makefile
+
+# grep '^V=' ../Makefile
+V= 5.2
+# grep '^R=' ../Makefile
+R= 5.2.0
+
+# grep '^INSTALL_.*=' ../Makefile | sed 's/INSTALL_TOP/prefix/'
+prefix= /opt/local
+INSTALL_BIN= ${prefix}/bin
+INSTALL_INC= ${prefix}/include
+INSTALL_LIB= ${prefix}/lib
+INSTALL_MAN= ${prefix}/man/man1
+INSTALL_LMOD= ${prefix}/share/lua/${V}
+INSTALL_CMOD= ${prefix}/lib/lua/${V}
+
+# canonical vars
+exec_prefix=${prefix}
+libdir=${exec_prefix}/lib
+includedir=${prefix}/include
+
+Name: Lua
+Description: An Extensible Extension Language
+Version: ${R}
+Requires:
+Libs: -L${libdir} -llua -lm
+Cflags: -I${includedir}
+
+# (end of lua.pc)
diff -Nurr lua.orig/files/patch-Makefile.diff lua/files/patch-Makefile.diff
--- lua.orig/files/patch-Makefile.diff	2011-12-19 08:17:42.000000000 -0200
+++ lua/files/patch-Makefile.diff	2011-12-18 22:19:45.000000000 -0200
@@ -1,20 +1,20 @@
---- Makefile.orig	2011-03-27 16:49:40.000000000 +0200
-+++ Makefile	2011-03-27 16:52:46.000000000 +0200
-@@ -43,7 +43,7 @@
+--- Makefile.orig	2011-12-18 22:04:50.000000000 -0200
++++ Makefile	2011-12-18 22:16:26.000000000 -0200
+@@ -41,7 +41,7 @@
  # What to install.
  TO_BIN= lua luac
- TO_INC= lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp
+ TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
 -TO_LIB= liblua.a
 +TO_LIB= liblua.a liblua.dylib
  TO_MAN= lua.1 luac.1
  
  # Lua version and release.
-@@ -64,6 +64,8 @@
+@@ -63,6 +63,8 @@
  	cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC)
  	cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB)
  	cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN)
 +	cd $(INSTALL_LIB) && ln -s liblua.dylib liblua.%VERSION%.dylib
 +	cd $(INSTALL_LIB) && ln -s liblua.dylib liblua.%COMPAT_VERSION%.dylib
  
- ranlib:
- 	cd src && cd $(INSTALL_LIB) && $(RANLIB) $(TO_LIB)
+ uninstall:
+ 	cd src && cd $(INSTALL_BIN) && $(RM) $(TO_BIN)
diff -Nurr lua.orig/files/patch-lua-5.1.4-3 lua/files/patch-lua-5.1.4-3
--- lua.orig/files/patch-lua-5.1.4-3	2011-12-19 08:17:42.000000000 -0200
+++ lua/files/patch-lua-5.1.4-3	1969-12-31 21:00:00.000000000 -0300
@@ -1,172 +0,0 @@
-# patch-lua-5.1.4-3 created 2011-03-16T19:06:31-0300
-# apply to a pristine copy of Lua 5.1.4 with:
-#	wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
-#	tar zxf lua-5.1.4.tar.gz
-#	cd lua-5.1.4/src
-#	wget http://www.lua.org/ftp/patch-lua-5.1.4-3
-#	patch < patch-lua-5.1.4-3
-# use curl -O -R if you don't have wget
-
---- src/lcode.c	2007/12/28 15:32:23	2.25.1.3
-+++ src/lcode.c	2011/01/31 14:53:16	2.25.1.5
-@@ -1,5 +1,5 @@
- /*
--** $Id: lcode.c,v 2.25.1.3 2007/12/28 15:32:23 roberto Exp $
-+** $Id: lcode.c,v 2.25.1.5 2011/01/31 14:53:16 roberto Exp $
- ** Code generator for Lua
- ** See Copyright Notice in lua.h
- */
-@@ -544,10 +544,6 @@
-       pc = NO_JUMP;  /* always true; do nothing */
-       break;
-     }
--    case VFALSE: {
--      pc = luaK_jump(fs);  /* always jump */
--      break;
--    }
-     case VJMP: {
-       invertjump(fs, e);
-       pc = e->u.s.info;
-@@ -572,10 +568,6 @@
-       pc = NO_JUMP;  /* always false; do nothing */
-       break;
-     }
--    case VTRUE: {
--      pc = luaK_jump(fs);  /* always jump */
--      break;
--    }
-     case VJMP: {
-       pc = e->u.s.info;
-       break;
---- src/ldblib.c	2008/01/21 13:11:21	1.104.1.3
-+++ src/ldblib.c	2009/08/04 18:50:18	1.104.1.4
-@@ -1,5 +1,5 @@
- /*
--** $Id: ldblib.c,v 1.104.1.3 2008/01/21 13:11:21 roberto Exp $
-+** $Id: ldblib.c,v 1.104.1.4 2009/08/04 18:50:18 roberto Exp $
- ** Interface from Lua to its debug API
- ** See Copyright Notice in lua.h
- */
-@@ -45,6 +45,7 @@
- 
- 
- static int db_getfenv (lua_State *L) {
-+  luaL_checkany(L, 1);
-   lua_getfenv(L, 1);
-   return 1;
- }
---- src/liolib.c	2008/01/18 17:47:43	2.73.1.3
-+++ src/liolib.c	2010/05/14 15:33:51	2.73.1.4
-@@ -1,5 +1,5 @@
- /*
--** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $
-+** $Id: liolib.c,v 2.73.1.4 2010/05/14 15:33:51 roberto Exp $
- ** Standard I/O (and system) library
- ** See Copyright Notice in lua.h
- */
-@@ -276,7 +276,10 @@
-     lua_pushnumber(L, d);
-     return 1;
-   }
--  else return 0;  /* read fails */
-+  else {
-+    lua_pushnil(L);  /* "result" to be removed */
-+    return 0;  /* read fails */
-+  }
- }
- 
- 
---- src/llex.c	2007/12/27 13:02:25	2.20.1.1
-+++ src/llex.c	2009/11/23 14:58:22	2.20.1.2
-@@ -1,5 +1,5 @@
- /*
--** $Id: llex.c,v 2.20.1.1 2007/12/27 13:02:25 roberto Exp $
-+** $Id: llex.c,v 2.20.1.2 2009/11/23 14:58:22 roberto Exp $
- ** Lexical Analyzer
- ** See Copyright Notice in lua.h
- */
-@@ -118,8 +118,10 @@
-   lua_State *L = ls->L;
-   TString *ts = luaS_newlstr(L, str, l);
-   TValue *o = luaH_setstr(L, ls->fs->h, ts);  /* entry for `str' */
--  if (ttisnil(o))
-+  if (ttisnil(o)) {
-     setbvalue(o, 1);  /* make sure `str' will not be collected */
-+    luaC_checkGC(L);
-+  }
-   return ts;
- }
- 
---- src/loadlib.c	2008/08/06 13:29:28	1.52.1.3
-+++ src/loadlib.c	2009/09/09 13:17:16	1.52.1.4
-@@ -1,5 +1,5 @@
- /*
--** $Id: loadlib.c,v 1.52.1.3 2008/08/06 13:29:28 roberto Exp $
-+** $Id: loadlib.c,v 1.52.1.4 2009/09/09 13:17:16 roberto Exp $
- ** Dynamic library loader for Lua
- ** See Copyright Notice in lua.h
- **
-@@ -639,7 +639,7 @@
-   lua_pushvalue(L, -1);
-   lua_replace(L, LUA_ENVIRONINDEX);
-   /* create `loaders' table */
--  lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1);
-+  lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
-   /* fill it with pre-defined loaders */
-   for (i=0; loaders[i] != NULL; i++) {
-     lua_pushcfunction(L, loaders[i]);
---- src/lstrlib.c	2008/07/11 17:27:21	1.132.1.4
-+++ src/lstrlib.c	2010/05/14 15:34:19	1.132.1.5
-@@ -1,5 +1,5 @@
- /*
--** $Id: lstrlib.c,v 1.132.1.4 2008/07/11 17:27:21 roberto Exp $
-+** $Id: lstrlib.c,v 1.132.1.5 2010/05/14 15:34:19 roberto Exp $
- ** Standard library for string operations and pattern-matching
- ** See Copyright Notice in lua.h
- */
-@@ -754,6 +754,7 @@
- 
- 
- static int str_format (lua_State *L) {
-+  int top = lua_gettop(L);
-   int arg = 1;
-   size_t sfl;
-   const char *strfrmt = luaL_checklstring(L, arg, &sfl);
-@@ -768,7 +769,8 @@
-     else { /* format item */
-       char form[MAX_FORMAT];  /* to store the format (`%...') */
-       char buff[MAX_ITEM];  /* to store the formatted item */
--      arg++;
-+      if (++arg > top)
-+        luaL_argerror(L, arg, "no value");
-       strfrmt = scanformat(L, strfrmt, form);
-       switch (*strfrmt++) {
-         case 'c': {
---- src/lvm.c	2007/12/28 15:32:23	2.63.1.3
-+++ src/lvm.c	2009/07/01 21:10:33	2.63.1.4
-@@ -1,5 +1,5 @@
- /*
--** $Id: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $
-+** $Id: lvm.c,v 2.63.1.4 2009/07/01 21:10:33 roberto Exp $
- ** Lua virtual machine
- ** See Copyright Notice in lua.h
- */
-@@ -133,6 +133,7 @@
- 
- void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
-   int loop;
-+  TValue temp;
-   for (loop = 0; loop < MAXTAGLOOP; loop++) {
-     const TValue *tm;
-     if (ttistable(t)) {  /* `t' is a table? */
-@@ -152,7 +153,9 @@
-       callTM(L, tm, t, key, val);
-       return;
-     }
--    t = tm;  /* else repeat with `tm' */ 
-+    /* else repeat with `tm' */
-+    setobj(L, &temp, tm);  /* avoid pointing inside table (may rehash) */
-+    t = &temp;
-   }
-   luaG_runerror(L, "loop in settable");
- }
diff -Nurr lua.orig/files/patch-src_Makefile.diff lua/files/patch-src_Makefile.diff
--- lua.orig/files/patch-src_Makefile.diff	2011-12-19 08:17:42.000000000 -0200
+++ lua/files/patch-src_Makefile.diff	2011-12-18 22:33:17.000000000 -0200
@@ -1,28 +1,28 @@
---- src/Makefile.orig	2011-03-27 16:49:54.000000000 +0200
-+++ src/Makefile	2011-03-27 16:55:30.000000000 +0200
-@@ -23,6 +23,7 @@
+--- src/Makefile.orig	2011-12-18 22:27:37.000000000 -0200
++++ src/Makefile	2011-12-18 22:32:25.000000000 -0200
+@@ -29,6 +29,7 @@
  PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris
  
  LUA_A=	liblua.a
 +LUA_DYLIB=	liblua.dylib
- CORE_O=	lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
- 	lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o  \
- 	lundump.o lvm.o lzio.o
-@@ -36,8 +37,8 @@
- LUAC_O=	luac.o print.o
+ CORE_O=	lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \
+ 	lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \
+ 	ltm.o lundump.o lvm.o lzio.o
+@@ -43,8 +44,8 @@
+ LUAC_O=	luac.o
  
- ALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O)
+ ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O)
 -ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
 -ALL_A= $(LUA_A)
 +ALL_T= $(LUA_DYLIB) $(LUA_A) $(LUA_T) $(LUAC_T)
 +ALL_A= $(LUA_DYLIB) $(LUA_A)
  
+ # Targets start here.
  default: $(PLAT)
- 
-@@ -48,14 +49,20 @@
+@@ -56,14 +57,20 @@
  a:	$(ALL_A)
  
- $(LUA_A): $(CORE_O) $(LIB_O)
+ $(LUA_A): $(BASE_O)
 +	@echo "Building static library"
  	$(AR) $@ $?
  	$(RANLIB) $@
@@ -30,24 +30,24 @@
 +$(LUA_DYLIB): $(CORE_O) $(LIB_O)
 +	@echo "Building dylib"
 +	$(CC) -o $@ $(MYLDFLAGS) -dynamiclib -install_name %PREFIX%/lib/liblua.dylib \
-+		-compatibility_version %COMPAT_VERSION% -current_version %VERSION% $^
++	-compatibility_version %COMPAT_VERSION% -current_version %VERSION% $^
 +
  $(LUA_T): $(LUA_O) $(LUA_A)
--	$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)
+-	$(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)
 +	$(CC) -o $@ -L. $(MYLDFLAGS) $(MYCFLAGS) $(MYLIBS) -llua lua.c
  
  $(LUAC_T): $(LUAC_O) $(LUA_A)
--	$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)
+-	$(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)
 +	$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_DYLIB) $(LIBS)
  
  clean:
  	$(RM) $(ALL_T) $(ALL_O)
-@@ -99,7 +106,7 @@
- 	$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"
+@@ -106,7 +113,7 @@
+ 	$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl -lreadline -lncurses"
  
  macosx:
--	$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline"
+-	$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline"
 +	$(MAKE) all MYCFLAGS="-fno-common -DLUA_USE_LINUX" MYLIBS="-lreadline"
- # use this on Mac OS X 10.3-
- #	$(MAKE) all MYCFLAGS=-DLUA_USE_MACOSX
  
+ mingw:
+ 	$(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \
