--- Portfile.orig	2014-06-03 21:00:59.000000000 -0400
+++ Portfile	2014-07-30 16:59:35.000000000 -0400
@@ -4,22 +4,29 @@
 PortSystem          1.0
 
 name                open-cobol
-version             1.1
+version             2.0
+revision            1
 categories          lang
 maintainers         gwmail.gwu.edu:egall openmaintainer
 license             GPL-2+
-description         OpenCOBOL
-long_description    OpenCOBOL is an open-source COBOL compiler.
+description         OpenCOBOL, aka GNU Cobol
+long_description    ${description}, is a free and open-source COBOL \
+                    compiler.
 
 homepage            http://www.opencobol.org/
 platforms           darwin
 
-master_sites        sourceforge:project/open-cobol/open-cobol/${version}/
+master_sites        sourceforge:project/${name}/${name}/${version}/ \
+                    sourceforge
 
-checksums           md5    e38c898e01d50a7ca6bb3e41cfcb0c64 \
-                    sha1   a833f011cd0f56d0aa3d97a56258ddd5141b594c \
-                    rmd160 931cd83d5ebbfae66b6e2601bb100e07a10d61a6
+distname            ${name}-${version}-feb2012
+worksrcdir          ${name}-${version}
 
+checksums           md5     58287e902f3b64dbac110d570c4aba4a \
+                    sha1    5088e2ebc2549b4ca9f7daba4e309682d8067cde \
+                    rmd160  39a1341a48b7e4addfaa545064849fe3dd29c881
+
+# Mostly taken from the Fink package for open-cobol:
 depends_lib-append  port:gmp \
                     port:libtool \
                     port:db44 \
@@ -29,7 +36,86 @@
                     port:gettext \
                     port:mpfr
 
-configure.args-append     --infodir=${prefix}/share/info
-configure.cppflags-append -I${prefix}/include/gnugetopt -Wno-pointer-sign \
-                          -I${prefix}/include/db44
-configure.ldflags-append  -lgnugetopt -L${prefix}/lib/db44
+# Patch to fix https://sourceforge.net/p/open-cobol/bugs/73/ until
+# upstream gets around to fixing it themselves:
+patchfiles-append   patch-libcob_Makefile.am.diff
+patch.args-append   --backup
+
+# since we are patching an automake Makefile, need to regenerate it:
+post-patch {
+    # use_automake does not automatically call aclocal for us like
+    # use_autoreconf does, so need to call it manually:
+    ui_debug "running '${prefix}/bin/aclocal --force --warnings=all -I m4 --install' in ${worksrcpath}"
+    system -W ${worksrcpath} "${prefix}/bin/aclocal --force --warnings=all -I m4 --install"
+}
+
+use_automake        yes
+automake.args       --add-missing --copy --force-missing --warnings=all
+
+# also mostly originally taken from Fink, but with some additions:
+configure.args-append     --infodir=${prefix}/share/info \
+                          --with-libiconv-prefix=${prefix} \
+                          --with-libintl-prefix=${prefix}
+configure.cppflags-append -I${prefix}/include/gnugetopt \
+                          -I${prefix}/include/db44 \
+                          -Wno-pointer-sign
+configure.ldflags-append  -L${prefix}/lib/db44
+
+# try to override various environment variables that might contain an '-R',
+# and try to force libiconv to be used properly:
+configure.env-append      COBC_LIBS='${configure.ldflags}' \
+                          INTLLIBS='-lintl -liconv' \
+                          LIBCOB_LIBS='-lm -lgmp -lmpfr -lncursesw -ldb' \
+                          LIBINTL='-lintl -liconv' \
+                          LIBS='-lgnugetopt' \
+                          LTLIBICONV='-L/opt/local/lib -liconv' \
+                          LTLIBINTL='-L/opt/local/lib -lintl -liconv'
+
+# temporarily disable ccache as I test as non-root:
+configure.ccache    no
+
+# the configure script adds the '-pipe' flag anyway, so get rid of the
+# duplicate unconditionally:
+configure.pipe      no
+
+# see:
+#https://sourceforge.net/p/open-cobol/discussion/help/thread/e1b4af35/#427c
+test.run            yes
+test.target         check
+# (tests 111, 112, 217 and 269 fail for me if I set
+# "configure.compiler=clang" from the command line, otherwise all tests
+# pass with gcc)
+
+post-test {
+    if {[file exists ${prefix}/share/${name}/config/default.conf]} {
+        ui_debug "${worksrcpath}/cobc/cobc -x ${filespath}/hello.cob"
+        system -W ${worksrcpath} "./cobc/cobc -x ${filespath}/hello.cob 2>&1 || echo \"cobc failed\""
+    } else {
+        ui_warn "cannot run cobc without an installed default.conf"
+    }
+    if {[file exists ${worksrcpath}/cobc/hello]} {
+        ui_debug "${worksrcpath}/cobc/hello"
+        system -W ${worksrcpath} "./hello"
+    } else {
+        ui_warn "cobc failed to produce any output"
+    }
+    ui_debug "${worksrcpath}/bin/cob-config --libs"
+    system -W ${worksrcpath} "./bin/cob-config --libs"
+}
+
+# https://lists.macosforge.org/pipermail/macports-users/2014-May/035518.html
+if {[string match *clang* ${configure.compiler}]} {
+    configure.args-append --disable-rpath
+    configure.cppflags-append -std=gnu89
+    pre-configure {
+        ui_warn "${name} has issues with clang"
+    }
+}
+
+variant debug description {enable debugging} {
+    configure.args-append --enable-debug \
+                          --enable-param-check
+    # '--enable-debug' will add a '-g' of its own, but we need to override
+    # the '-Os' that MacPorts would otherwise want to add as well:
+    configure.optflags    -g
+}
