--- Portfile.orig	2008-07-02 18:42:11.000000000 -0400
+++ Portfile	2008-07-04 00:31:07.000000000 -0400
@@ -1,12 +1,12 @@
 # $Id: Portfile 33172 2008-01-20 17:34:57Z ryandesign@macports.org $
 
-PortSystem			1.0
-name				pgplot
-version				5.2.2
-revision			3
-categories			graphics devel
-maintainers			nomaintainer
-description			C/Fortran graphics library
+PortSystem	1.0
+name		pgplot
+version		5.2.2
+revision	4
+categories	graphics devel
+maintainers	nomaintainer
+description	C/Fortran graphics library
 long_description \
 	The PGPLOT Graphics Subroutine Library is a Fortran- or C-callable, \
 	device-independent graphics package for making simple scientific \
@@ -15,23 +15,24 @@
 	applications, the program can be device-independent, and the output can \
 	be directed to the appropriate device at run time.
 
-platforms			darwin
+platforms	darwin
 
-homepage			http://www.astro.caltech.edu/~tjp/pgplot/
-master_sites            ftp://ftp.astro.caltech.edu/pub/pgplot/:pgplot
+homepage	http://www.astro.caltech.edu/~tjp/pgplot/
+master_sites	ftp://ftp.astro.caltech.edu/pub/pgplot/:pgplot
 
-distfiles			${name}5.2.tar.gz:pgplot
+distfiles	${name}5.2.tar.gz:pgplot
 
-distname			${name}5.2:pgplot
+distname	${name}5.2:pgplot
 
-worksrcdir			${name}/build:pgplot
+worksrcdir	${name}/build:pgplot
 
-checksums			${name}5.2.tar.gz md5 e8a6e8d0d5ef9d1709dfb567724525ae
+checksums	${name}5.2.tar.gz md5 e8a6e8d0d5ef9d1709dfb567724525ae
 
-depends_lib			lib:libpng:libpng lib:libz:zlib bin:g77-dp-3.4:gcc34
+depends_lib	lib:libpng:libpng lib:libz:zlib
 
-patchfiles			patch-makemake patch-src_grgfil.f patch-drivers.list \
-					patch-pgdispd_proccom.c
+patchfiles	patch-makemake patch-src_grgfil.f \
+		patch-drivers.list patch-pgdispd_proccom.c \
+		patch-local.conf
 
 post-extract {
 	file mkdir ${worksrcpath}
@@ -43,19 +44,31 @@
 
 post-patch {
 	reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/../src/grgfil.f
+	reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@X11PREFIX@@|${x11prefix}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@FCOMPL@@|${fcompl}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@FC_OPT@@|${fc_opt}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@FD_OPT@@|${fd_opt}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@CCOMPL@@|${ccompl}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@CC_OPT@@|${cc_opt}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@CD_OPT@@|${cd_opt}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@MCOMPL@@|${mcompl}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@MC_OPT@@|${mc_opt}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@XLIB@@|${xlib}|g"     ${worksrcpath}/local.conf
+	reinplace "s|@@AQTINC@@|${aqtinc}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@AQTLIB@@|${aqtlib}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@SHARED_LIB@@|${shared_lib}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@SHARED_LD@@|${shared_ld}|g" ${worksrcpath}/local.conf
+	reinplace "s|@@SHARED_LIB_LIBS@@|${shared_lib_libs}|g" ${worksrcpath}/local.conf
 }
 
 configure {
 	cd ${worksrcpath}
-	system "../makemake .. ${ppMakeMakeTarget} ${ppCompilerSpec}"
+	system "../makemake .. darwin"
 }
 
-build.target		all
-
-post-build {
-	cd ${worksrcpath}
-	system "make cpg"
-}
+#build.target		all
+build.target		lib grfont.dat libcpgplot.a cpgplot.h
 
 destroot {
 	xinstall -m 755 -d ${destroot}${prefix}/share/${name} \
@@ -67,40 +80,91 @@
 	system "ranlib ${destroot}${prefix}/lib/libpgplot.a \
 		${destroot}${prefix}/lib/libcpgplot.a"
 	xinstall -m 644 -W ${worksrcpath} cpgplot.h ${destroot}${prefix}/include
-	xinstall -m 644 -W ${worksrcpath} pgplot.doc ../copyright.notice \
+	xinstall -m 644 -W ${worksrcpath} ../copyright.notice \
 		${destroot}${prefix}/share/doc/${name}
 }
 
+default_variants +x11 +aqt +shared +doc
+
 platform darwin {
-	global ppMakeMakeTarget ppCompilerSpec
-	set ppMakeMakeTarget	darwin
-	set ppCompilerSpec		g77_gcc
-	patchfiles-append		patch-sys_darwin_aaaread.me \
-							patch-sys_darwin_g77_gcc.conf
-	post-patch {
-		reinplace "s|@@PREFIX@@|${prefix}|g" \
-			${worksrcpath}/../sys_darwin/${ppCompilerSpec}.conf
-		reinplace "s|@@X11PREFIX@@|${x11prefix}|g" \
-			${worksrcpath}/../sys_darwin/${ppCompilerSpec}.conf
-	}
+	patchfiles-append	patch-sys_darwin_aaaread.me 
 }
 
-platform darwin i386 {
-	depends_lib-delete	bin:g77-dp-3.4:gcc34
-	depends_lib-append	port:gcc42
-
-	set ppCompilerSpec	gfortran_gcc_42
-
-	patchfiles-delete	patch-sys_darwin_g77_gcc.conf
-	patchfiles-append	patch-sys_darwin_gfortran_gcc_42.conf
+# default compiler is gcc43
+set default_compiler_port	port:gcc43
+depends_lib-append	${default_compiler_port}
+global fcompl ccompl
+global fc_opt fd_opt cc_opt cd_opt
+set fcompl gfortran-mp-4.3
+#set fc_opt "-Wall -O"
+set fc_opt ${configure.fflags}
+set fd_opt "-fno-backslash \$FFLAGC"
+set ccompl gcc-mp-4.3
+#set cc_opt "-Wall -DPG_PPU -O3"
+set cc_opt "${configure.cflags} -DPG_PPU"
+set cd_opt "\$CFLAGC"
+set shared_opt	"-dynamiclib -single_module"
+set shared_lib_libs_f	"-lgfortran"
+
+variant gcc34 description {use g77 Fortran compiler from gcc34} conflicts gcc42 gcc43 g95 {
+	depened_lib-delete	${default_compiler_port}
+#	depends_lib-append	port:gcc34
+	depends_lib-append	bin:g77-dp-3.4:gcc34
+	set fcompl		g77-dp-4.2
+	set ccompl		gcc
+	set shared_opt		"-dynamiclib -single_module"
+	set shared_lib_libs_f	"-L${prefix}/lib/gcc-3.4 -lg2c"
 }
 
-default_variants +x11 +aqt
-
-variant x11 {
-	depends_lib-append	lib:libX11:XFree86
+variant gcc42 description {use gcc42 compiler suite} conflicts gcc34 gcc43 g95 {
+	depends_lib-delete	${default_compiler_port}
+	depends_lib-append	port:gcc42
+	set fcompl		gfortran-mp-4.2
+	set ccompl		gcc-mp-4.2
+	set shared_opt		"-dynamiclib -single_module"
+	set shared_lib_libs_f	"-lgfortran"
+}
+
+#variant gcc43 description {use gcc43 compiler suite} conflicts gcc34 gcc42 g95 {
+#	depends_lib-delete	${default_compiler_port}
+#	depends_lib-append	port:gcc43
+#	set fcompl		gfortran-mp-4.3
+#	set ccompl		gcc-mp-4.3
+#	set shared_opt		"-dynamiclib -single_module"
+#	set shared_lib_libs_f	"-lgfortran"
+#}
+
+variant g95 description {use g95 Fortran compiler} conflicts gcc34 gcc42 gcc43 {
+	depends_lib-delete	${default_compiler_port}
+	depends_lib-append	port:g95
+	set fcompl		g95
+	set ccompl		gcc
+	set shared_opt		"-dynamiclib -single_module"
+	set shared_lib_libs_f	"-lf95"
+}
+
+global shared_lib shared_ld shared_lib_libs
+set shared_lib ""
+set shared_ld ""
+set shared_lib_libs ""
+variant shared description {create shared library} {
+	set shared_lib 	libpgplot.dylib
+	set shared_ld	"${ccompl} -o \$SHARED_LIB ${shared_opt} -install_name ${prefix}/lib/\$SHARED_LIB"
+	set shared_lib_libs "\$LIBS ${shared_lib_libs_f} -lSystem -lpng ${aqtlib}"
+	post-destroot {
+		xinstall -m 755 -W ${worksrcpath} libpgplot.dylib \
+			${destroot}${prefix}/lib
+	}
+	
+}
 
-	patchfiles-append	patch-drivers_x11.list
+global xlib
+set xlib ""
+variant x11 description {invoke X11 graphics devices} {
+	build.target-append	pgdisp pgxwin_server
+	depends_lib-append	lib:libX11:xorg
+	patchfiles-append	patch-drivers.list_x11
+	set xlib		"-L${x11prefix}/lib -lX11"
 
 	post-destroot {
 		xinstall -m 755 -W ${worksrcpath} pgdisp pgxwin_server \
@@ -108,28 +172,42 @@
 	}
 }
 
-variant aqt {
+global aqtinc aqtlib mcompl
+set aqtinc ""
+set aqtlib ""
+set mcompl ""
+set mc_opt ""
+variant aqt description {invoke Aquaterm graphics device} {
 # Variant that adds aquaterm driver support
 	depends_lib-append	port:aquaterm
 	patchfiles-append	patch-drivers.list_aqt \
-						patch-sys_darwin_${ppCompilerSpec}.conf_aqt \
-						patch-makemake_aqt
+				patch-makemake_aqt
+	set aqtinc		-I${prefix}/include/aquaterm
+	set aqtlib		"-Wl,-framework -Wl,Foundation -laquaterm"
+	set mcompl		${ccompl}
+	set mc_opt		"-DPG_PPU -O2 -I. ${aqtinc}"
 	
 	pre-patch {
 		file copy ${prefix}/share/aquaterm/pgplot/aqdriv.m ${workpath}/pgplot/drivers/
 	}
 
-	post-destroot {
-		xinstall -m 755 -W ${worksrcpath} libpgplot.dylib \
-			${destroot}${prefix}/lib
-	}
 }
 
 # Variant to install demo programs
-variant demos {
+variant demos description {install demo programs} {
+	build.target-append	prog cpgdemo
 	post-destroot {
 		foreach f [glob {pgdemo[1-9]} {pgdemo1[0-7]}] {
 			xinstall -m 755 -W ${worksrcpath} $f ${destroot}${prefix}/bin
 		}
+		xinstall -m 755 -W ${worksrcpath} cpgdemo ${destroot}${prefix}/bin
+	}
+}
+
+variant doc description {install documents} {
+	build.target-append	pgplot.doc
+	post-destroot {
+		xinstall -m 644 -W ${worksrcpath} pgplot.doc \
+			${destroot}${prefix}/share/doc/${name}
 	}
 }
