--- ../lua/Portfile	2012-06-11 04:45:08.000000000 -0700
+++ Portfile	2012-08-08 08:20:09.000000000 -0700
@@ -1,11 +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
+
+# $Id: Portfile 82709 2011-08-18 20:19:13Z ryandesign@macports.org $
 
 PortSystem          1.0
 
-name                lua
-conflicts           lua50
-set compat_version  5.1
-version             ${compat_version}.5
+name                lua52
+conflicts           lua lua50
+version             5.2.1
 revision            0
 categories          lang
 license             MIT
@@ -19,10 +20,11 @@
 
 homepage            http://www.lua.org
 master_sites        ${homepage}/ftp/
+distname            lua-${version}
 
-checksums           ${distfiles} \
-                    sha1    2b11c8e60306efb7f0734b747588f57995493db7 \
-                    rmd160  c867c8364295d3e4fb4e4d8ebb27fa2b2312cdef
+checksums           md5     ae08f641b45d737d12d30291a5e5f6e3 \
+                    sha1    6bb1b0a39b6a5484b71a83323c690154f86b2021 \
+                    rmd160  365eb77561b1f4f92c29f6d7f455f41e2b8393e0
 
 depends_lib         port:readline
 build.target        macosx
@@ -30,49 +32,39 @@
 test.run            yes
 test.env            DYLD_LIBRARY_PATH=./lib
 
-patchfiles          patch-lua-5.1.4-3 \
-                    patch-Makefile.diff \
-                    patch-src_Makefile.diff
+patchfiles          patch-Makefile.diff \
+                    patch-src_Makefile.diff \
+                    patch-lua-${version}.pc.diff
+patch.args          -f
 
 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 "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 {
-    reinplace -E "/^MYLDFLAGS=/s|\$| ${configure.ldflags} [get_canonical_archflags]|" ${worksrcpath}/src/Makefile
-    reinplace -E "/^CFLAGS=/s|\$| ${configure.cflags} ${configure.cppflags} [get_canonical_archflags]|" ${worksrcpath}/src/Makefile
-    reinplace "s|CC= .*\$|CC= ${configure.cc}|" ${worksrcpath}/src/Makefile
+    reinplace "s|@PREFIX@|${prefix}|" ${worksrcpath}/lua-${version}.pc
+    reinplace "s|@PREFIX@|${prefix}|" ${worksrcpath}/Makefile
+    reinplace "s|@CC@|${configure.cc}|" ${worksrcpath}/src/Makefile
+    reinplace "s|@CFLAGS@|${configure.cflags} [get_canonical_archflags]|" \
+        ${worksrcpath}/src/Makefile
+    reinplace "s|@LDFLAGS@|[get_canonical_archflags]|" \
+        ${worksrcpath}/src/Makefile
 }
 
 use_parallel_build  no
+use_configure       no
+build.target        macosx
 
-destroot.target install INSTALL_TOP=${destroot}${prefix}
 post-destroot {
+    xinstall -d -m 0755 ${destroot}${prefix}/lib/pkgconfig
+    xinstall -W ${worksrcpath} -m 0644 lua-${version}.pc \
+        ${destroot}${prefix}/lib/pkgconfig/lua-${version}.pc
     xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}
-    xinstall -m 0644 -W ${worksrcpath} COPYRIGHT HISTORY README ${destroot}${prefix}/share/doc/${name}
+    xinstall -W ${worksrcpath} -m 0644 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
+    foreach html [glob doc/*.html doc/*.gif] {
+        xinstall -W ${worksrcpath} -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
 }
 
 livecheck.type  regex
 livecheck.url   ${master_sites}
-livecheck.regex {lua-(\d+(?:\.\d+)*)}
+livecheck.regex "lua-(5.2(?:\\.\\d+)*)${extract.suffix}"
