# -*- 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

PortSystem          1.0
PortGroup           xcode 1.0
PortGroup           xcodeversion 1.0

name                TeXShop
version             5.57
revision            0
categories          aqua editors
license             GPL-2+
maintainers         {mps @Schamschula} openmaintainer
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}/texshop-64
use_zip             yes

dist_subdir         TeXShop/

# This is a temporary kludge. The new Xcode build system fails to
# destroot this for reasons that are poorly understood.
# Remove this when a better fix is known.
if {[vercmp ${xcodeversion} 10.0] >= 0} {
    build.pre_args      -UseNewBuildSystem=NO
    destroot.pre_args   -UseNewBuildSystem=NO
}

configure.cxx_stdlib    libstdc++

platform darwin {
    if { [vercmp ${macosx_deployment_target} >= 15.0]} {
        # See: https://trac.macports.org/ticket/70901
        macosx_deployment_target 14.0
    }

    if {${os.major} > 23} {
        # stealth update
        #dist_subdir ${name}/${version}_${revision}

        distname    texshopsource[strsed ${version} {/\.//}]

        checksums   rmd160  e32f03c53aa29679be2f6a288a5160b46233e473 \
                    sha256  37f6a9ab5f5ef78eee31243d856fd736e6135b0464a755c6d47daf44ff0d7cf6 \
                    size    181405663

        worksrcdir  ${name}-${version}

        minimum_xcodeversions   {21 13.4}

        # Don't build Sparkle
        # See: https://trac.macports.org/ticket/66382
        patchfiles-append \
                    patch-Sources-UseSparkle.h.diff \
                    patch-TeXShop.xcodeproj-project.pbxproj.diff

        post-patch {
            delete ${worksrcpath}/Sparkle.framework
        }
    }

    if {${os.major} > 19 && ${os.major} < 23} {
        version     4.79
        revision    1

        distname    texshopsource479

        checksums   rmd160  ba6ff730a0ab391b039bdac315c41404cd99959c \
                    sha256  dfa9f54b93502a6487c31adf39c47051483a4b4bd1f974d8b35e66a90c6c660d \
                    size    158553155

        worksrcdir  ${name}-${version}

        minimum_xcodeversions   {20 12.2}

        # Don't build Sparkle
        # See: https://trac.macports.org/ticket/66382
        patchfiles  patch-TeXShop-4.79.xcodeproj-project.pbxproj.diff \
                    patch-TeXShop-4.79-Sources-TSPreferences.diff

        post-patch {
            delete ${worksrcpath}/Sparkle.framework
        }

        livecheck.type  none
    }

    if {${os.major} > 15 && ${os.major} < 20} {
        version     4.44
        revision    0

        distname    texshopsource444

        checksums   rmd160  bcbccbd35ecbf9f09d38e43a7bc5392f1e0f419c \
                    sha256  e73f79a87cb02faa37b080fbdddec5edc384b7e80ff5b1d3828f86a3160da032 \
                    size    151971878

        minimum_xcodeversions   {15 8.0}

        worksrcdir  ${distname}

        post-patch {
            if {${os.major} < 17} {
                reinplace "s|#define HIGHSIERRAORHIGHER|// #define HIGHSIERRAORHIGHER|g" ${worksrcpath}/Sources/globals.h
            }
            if {${os.major} < 18} {
                reinplace "s|#define MOJAVEORHIGHER|// #define MOJAVEORHIGHER|g" ${worksrcpath}/Sources/globals.h
            }
        }

        livecheck.type  none
    }

    if {${os.major} > 10 && ${os.major} < 16} {
        version     4.15
        revision    0

        distname    texshopsource415

        checksums   rmd160  cbad7388bce6932b2c8a77a8553f22d48e2721a0 \
                    sha256  19c97d5a5b74a54dd02098b24c7336174b6c51c3a12f8fdb2aea4df363561d45 \
                    size    142699767

        minimum_xcodeversions   {11 4.4}

        worksrcdir  ${distname}

        livecheck.type  none
    }

    if {${os.major} < 11} {
        # Version for legacy systems.
        master_sites    ${homepage}/

        version     2.47
        revision    0

        distname    texshopsource247

        checksums   rmd160  cd02b93bafa72de885338d32e98c61de39e11aa6 \
                    sha256  cd8f9fd827d9b080423da0f1d9664bdbfdfebb4525bbb7542ee54c44e2f9ce19 \
                    size    75049777

        worksrcdir  texshopsource-2.47

        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
        destroot.target        ${name}

        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"

        livecheck.type  none
    }
}

build.target            ${name}

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

xcode.destroot.settings INSTALL_MODE_FLAG=755 {*}${xcode.build.settings}

post-destroot {
    file delete -force ${destroot}${applications_dir}/TeX.mdimporter

    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        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
    }
}

livecheck.type      regex
livecheck.regex     TeXShop \\(v (5\.\[0-9.\]+)\\)
