# $Id: Portfile,v 1.10 2006/06/30 20:20:01 takanori Exp $
PortSystem		1.0

name			pTeX
version			20060626
epoch			${version}
categories		print textproc
maintainers		takanori@opendarwin.org
description		Japanese TeX (pTeX) processing environment
long_description	${description}
platforms		darwin macosx
homepage		http://www.nn.iij4u.or.jp/~tutimura/tex/ptetex.html
master_sites		ftp://ftp.lab.kdd.co.jp/CTAN/systems/unix/teTeX/3.0/distrib/:tetex \
			ftp://ftp.meisei-u.ac.jp/pub/CTAN/systems/unix/teTeX/3.0/distrib/:tetex \
			ftp://ftp.nara.wide.ad.jp/pub/TeX/CTAN/systems/unix/teTeX/3.0/distrib/:tetex \
			ftp://ftp.riken.go.jp/pub/tex-archive/systems/unix/teTeX/3.0/distrib/:tetex \
			ftp://ftp.u-aizu.ac.jp/pub/tex/CTAN/systems/unix/teTeX/3.0/distrib/:tetex \
			ftp://ftp.yz.yamagata-u.ac.jp/pub/CTAN/systems/unix/teTeX/3.0/distrib/:tetex \
			ftp://dante.ctan.org/tex-archive/systems/unix/teTeX/3.0/distrib/:tetex \
			ftp://cam.ctan.org/tex-archive/systems/unix/teTeX/3.0/distrib/:tetex \
			ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/3.0/distrib/:tetex \
			http://tutimura.ath.cx/~nob/tex/ptetex/ptetex3/:ptetex3 \
			ftp://ftp.miko.org/pub/mirror/ptetex3/:ptetex3
distfiles		tetex-src-3.0.tar.gz:tetex \
			tetex-texmf-3.0po.tar.gz:tetex \
			ptetex3-${version}.tar.gz:ptetex3

checksums	tetex-src-3.0.tar.gz md5 944a4641e79e61043fdaf8f38ecbb4b3 \
				     sha1 7637789f7f4929694aed1b89820f5bad4753e8fc \
				     rmd160 15a139f5f36993e4ed3583260e175cfb13ce7bcc \
		tetex-texmf-3.0po.tar.gz md5 ed9d30d9162d16ac8d5065cde6e0f6fa \
					 sha1 1be97f57a26a6e9b72ebfd932e45914a959aff16 \
					 rmd160 a1e87733fa3cbef04e39a690ed8549aeaaddb241 \
		ptetex3-${version}.tar.gz md5 0c31f9989fb86da6a1a8757edeb86337 \
					  sha1 98437d965b6b664d7f5023257b809fde52fb4a08 \
					  rmd160 dbad83666e6eab3e55ad640c2e225467ff81331f

default_variants	+hiragino +otf

depends_lib	lib:libX11:XFree86 lib:libgd:gd2 lib:libiconv:libiconv lib:libjpeg:jpeg \
		lib:libz:zlib lib:libpng:libpng lib:libncurses:ncurses lib:libXm:openmotif
depends_run	port:texinfo port:texi2html

extract.only	ptetex3-${version}.tar.gz
worksrcdir	ptetex3-${version}

configure.args	--without-texinfo --without-texi2html --with-system-gd --with-system-zlib --with-system-pnglib --with-system-ncurses --with-xdvi-x-toolkit=motif
configure	{
		system "echo MD5CHECK=md5check  >> ${worksrcpath}/my_option"
		system "echo SRC_DIR=${distpath} >> ${worksrcpath}/my_option"
		system "echo REAL_PREFIX=${prefix} >> ${worksrcpath}/my_option"
		system "echo REAL_DATA=${prefix}/share >> ${worksrcpath}/my_option"
		system "echo CONF_OPTION=\\\"\\\$CONF_OPTION ${configure.args}\\\" >> ${worksrcpath}/my_option"

		system "echo CPPFLAGS=\\\"-I${prefix}/include\\\" >> ${worksrcpath}/my_option"
		system "echo LDFLAGS=\\\"-L${prefix}/lib\\\" >> ${worksrcpath}/my_option"
		system "echo LD_LIBRARY_PATH=\\\"\\\$LD_LIBRARY_PATH:${prefix}/lib\\\" >> ${worksrcpath}/my_option"
		system "echo export CPPFLAGS LDFLAGS LD_LIBRARY_PATH >> ${worksrcpath}/my_option"
}

pre-build	{
		if {[variant_isset macosx]} {
		   # make sure to use Hiragino fonts.
		   set SystemLibraryFontsDir /System/Library/Fonts
		   set FontList { "ヒラギノ角ゴ Pro W3.otf"
		       		  "ヒラギノ角ゴ Pro W6.otf"
				  "ヒラギノ角ゴ Std W8.otf"
				  "ヒラギノ丸ゴ Pro W4.otf"
				  "ヒラギノ明朝 Pro W3.otf"
				  "ヒラギノ明朝 Pro W6.otf" }
		   foreach f ${FontList} {
		      if {![file exists ${SystemLibraryFontsDir}/${f}]} {
		      	 ui_msg "Please place \"${f}\" in ${SystemLibraryFontsDir} and restart the build."
		      	 exit 1
		      }
		   }
		} else {
		  # perhaps works but not recommended.
		}
}

build		{
		file mkdir ${workpath}/temp

		system "cd ${worksrcpath} && make PREFIX=${workpath}/temp"
		if {[variant_isset otf]} {
		   system "cd ${worksrcpath} && make PREFIX=${workpath}/temp otf"
		}
		if {[variant_isset babel]} {
		   system "cd ${worksrcpath} && make PREFIX=${workpath}/temp babel"
		}
		system "cd ${worksrcpath} && make PREFIX=${workpath}/temp fonty"
}

destroot	{
		system "cp -R ${workpath}/temp/teTeX/ ${destroot}${prefix}"

		file delete ${destroot}${prefix}/info/dir
		file delete ${destroot}${prefix}/share/info
		file rename ${destroot}${prefix}/info ${destroot}${prefix}/share/info
		file delete -force ${destroot}${prefix}/share/man
		file rename ${destroot}${prefix}/man ${destroot}${prefix}/share/man

		if {[variant_isset hiragino]} {
		   system "perl -pi -e \"s|^kanjiEmbed noEmbed$|kanjiEmbed hiragino|, s|^(KanjiMap morisawa.map)$|#! \\1\\nKanjiMap morisawa-hiraginoEmbed.map|\" ${destroot}${prefix}/share/texmf-config/web2c/updmap.cfg"
		}
		file copy ${destroot}${prefix}/share/texmf/fonts/map/dvipdfm/morisawa.map ${destroot}${prefix}/share/texmf/fonts/map/dvipdfm/morisawa-hiraginoEmbed.map
		system "perl -pi -e \"s|Ryumin-Light|HiraMinPro-W3.otf|, s|GothicBBB-Medium|HiraKakuPro-W3.otf|, s|FutoMinA101-Bold|HiraMinPro-W6.otf|, s|FutoGoB101-Bold|HiraKakuPro-W6.otf|, s|Jun101-Light|HiraMaruPro-W4.otf|\" ${destroot}${prefix}/share/texmf/fonts/map/dvipdfm/morisawa-hiraginoEmbed.map"

		# Modify mktex.opt to force use of varfonts
		reinplace "s|MT_FEATURES=appendonlydir|MT_FEATURES=appendonlydir:varfonts|g" ${destroot}${prefix}/share/texmf/web2c/mktex.opt

		# Add a directory for local enhancements
		file mkdir ${destroot}${prefix}/share/texmf-local
		system "touch ${destroot}${prefix}/share/texmf-local/.ptetex3-${version}"
}

post-destroot	{
		xinstall -m 755 -d ${destroot}${prefix}/share/doc/ptetex3
		foreach f {ChangeLog LICENSE README*} {
			eval xinstall -m 644 [glob ${worksrcpath}/${f}] ${destroot}${prefix}/share/doc/ptetex3
		}
}

post-activate	{
		system "ranlib ${prefix}/lib/libkpathsea.a"

		system "${prefix}/bin/fmtutil-sys --all"
		system "${prefix}/bin/mktexlsr"
		system "${prefix}/bin/updmap-sys"
}

#Embed Hiragino fonts in PDF
variant hiragino {}

#Use otf.sty
variant otf {}

#Use babel
variant babel {}

variant euc conflicts sjis utf8 {
	pre-configure { system "echo KANJI_CODE=EUC  >> ${worksrcpath}/my_option" }
}
variant sjis conflicts euc utf8 {
	pre-configure { system "echo KANJI_CODE=SJIS  >> ${worksrcpath}/my_option" }
}
variant utf8 conflicts euc sjis {
	pre-configure { system "echo KANJI_CODE=UTF8  >> ${worksrcpath}/my_option" }
}

variant nox11 conflicts motif xaw xaw3d nextaw {
	depends_lib-delete lib:libX11:XFree86 lib:libgd:gd2 lib:libiconv:libiconv lib:libjpeg:jpeg lib:libXm:openmotif
	# gd2 requires X11
	configure.args-delete --with-system-gd --with-xdvi-x-toolkit=motif
	configure.args-append --without-system-gd --without-x --without-xdvik
}
variant motif conflicts nox11 xaw xaw3d nextaw {}
variant xaw conflicts nox11 motif xaw3d nextaw {
	depends_lib-delete lib:libXm:openmotif
	depends_lib-append lib:libXaw:XFree86
	configure.args-delete --with-xdvi-x-toolkit=motif
	configure.args-append --with-xdvi-x-toolkit=xaw
}
variant xaw3d conflicts nox11 motif xaw nextaw {
	depends_lib-delete lib:libXm:openmotif
	depends_lib-append lib:libXaw3d:Xaw3d
	configure.args-delete --with-xdvi-x-toolkit=motif
	configure.args-append --with-xdvi-x-toolkit=xaw3d
}
variant nextaw conflicts nox11 motif xaw xaw3d {
	depends_lib-delete lib:libXm:openmotif
	depends_lib-append lib:libneXtaw:neXtaw
	configure.args-delete --with-xdvi-x-toolkit=motif
	configure.args-append --with-xdvi-x-toolkit=neXtaw
}

platform macosx {}
