# -*- 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 97116 2012-08-27 21:11:19Z ryandesign@macports.org $

PortSystem      1.0
PortGroup       xcode 1.0

name            TeXShop
conflicts       TeXShop3
version         2.47
set major       [lindex [split ${version} .] 0]
categories      aqua editors
license         GPL-2+
maintainers     nomaintainer
description     TeX previewer for Mac OS X
long_description   \
    TeXShop is a TeX previewer for Mac OS X, written in Cocoa.  \
    Since pdf is a native file format on OS X, TeXShop uses     \
    "pdftex" and "pdflatex" rather than "tex" and "latex" to    \
    typeset, these programs in the standard teTeX distribution  \
    of TeX produce pdf output instead of dvi output.
homepage        http://pages.uoregon.edu/koch/texshop/
master_sites    ${homepage}
use_zip         yes
distname        texshopsource[strsed ${version} {g/\.//}]
worksrcdir      texshopsource-${version}

checksums       rmd160  cd02b93bafa72de885338d32e98c61de39e11aa6 \
                sha256  cd8f9fd827d9b080423da0f1d9664bdbfdfebb4525bbb7542ee54c44e2f9ce19

# force 32-bit, ticket #20291
supported_archs i386 ppc

post-extract    {
    set sparkle "Sparkle.framework/Versions/A/Sparkle"
    set ogrekit "OgreKit.framework/Versions/A/OgreKit"
    if {![variant_isset universal]} {
        # thin the bundled frameworks
        system "cd ${worksrcpath} && lipo -thin ${configure.build_arch} $sparkle -output $sparkle"
        system "cd ${worksrcpath} && lipo -thin ${configure.build_arch} $ogrekit -output $ogrekit"
    } elseif {${os.major} == 8} {
        # Tiger chokes on the x86_64
        system "cd ${worksrcpath} && lipo -remove x86_64 $sparkle -output $sparkle"
    }
    reinplace "s|defaultConfigurationName = Debug|defaultConfigurationName = Release|" ${worksrcpath}/${name}.xcodeproj/project.pbxproj
    reinplace "s|GCC_VERSION_i386 = 4\.0|GCC_VERSION_i386 = [lindex [split ${configure.objc} -] 1]|g" ${worksrcpath}/3rdparty/OgreKit/OgreKit.xcodeproj/project.pbxproj
    reinplace "s|GCC_VERSION_ppc = 3\.3|GCC_VERSION_ppc = [lindex [split ${configure.objc} -] 1]|g" ${worksrcpath}/3rdparty/OgreKit/OgreKit.xcodeproj/project.pbxproj
    reinplace "s|GCC_VERSION = 4\.0|GCC_VERSION = [lindex [split ${configure.objc} -] 1]|" ${worksrcpath}/TeXShop.xcodeproj/project.pbxproj
    reinplace "s|GCC_VERSION = 4\.0|GCC_VERSION = [lindex [split ${configure.objc} -] 1]|" ${worksrcpath}/3rdparty/TeX-mdimporter/TeX.xcodeproj/project.pbxproj
}

patchfiles		patch-TSDocument.m.diff

compiler.blacklist  clang

build.target    ${name}

xcode.project   ${name}.xcodeproj
xcode.configuration Release
xcode.build.settings    FRAMEWORK_SEARCH_PATHS=${worksrcpath} \
                        CONFIGURATION_BUILD_DIR=${worksrcpath}/build

destroot.target     ${name}
eval xcode.destroot.settings INSTALL_MODE_FLAG=755 ${xcode.build.settings}
post-destroot        {
    file delete -force ${destroot}${applications_dir}/TeX.mdimporter

    xinstall -m 755 ${filespath}/lilypond.engine ${destroot}${applications_dir}/${name}.app/Contents/Resources/${name}/Engines/Lilypond.engine
    reinplace s|@@PREFIX@@|${prefix}|g ${destroot}${applications_dir}/${name}.app/Contents/Resources/${name}/Engines/Lilypond.engine
}

notes "If you intend to use the Lilypond engine, please copy ${applications_dir}/${name}.app/Contents/Resources/${name}/Engines/Lilypond.engine to ~/Library/${name}/Engines/Lilypond.engine"

platform darwin {
    if {${os.major} >= 11} {
        universal_variant   no
        supported_archs     i386
        set os_note "${name} ${version} is designed for OS X 10.6 Snow Leopard and older; for OS X 10.7 Lion and newer you may prefer to use the TeXShop3 port."
        notes-append "\n\n${os_note}"
        pre-fetch {
            ui_msg ${os_note}
        }
    }
}

livecheck.type      regex
livecheck.regex     TeXShop \\(v (${major}\.\[0-9.\]+)\\)
