# -*- 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           qt5 1.0
PortGroup           perl5 1.0
PortGroup           github 1.0
PortGroup           active_variants 1.1
PortGroup           conflicts_build 1.0
PortGroup           deprecated 1.0

deprecated.macports_support no

set shorthash       8238e839
# set fullhash      8238e839c90de35f7c64380d2c4e360192862a34
set majorversion    .28
set minorversion    .2
set lastcommit      20180201
set corerev         13
set pluginsrev      6
set metarev         0
github.setup        MythTV mythtv ${shorthash}
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
checksums           rmd160  0f468b5e0502afe2672f2e41df5f40cbdb2d43d4 \
                    sha256  09d3736e9fe0fb191cc7d0339d4c972e6dc8b297941491281e1e86f9652b13a0

name                mythtv${majorversion}
categories          multimedia
# i386 unsupported -- https://trac.macports.org/ticket/40337
supported_archs     x86_64
license             GPL-2
maintainers         nomaintainer
homepage            https://www.mythtv.org/
universal_variant   no

livecheck.version   ${majorversion}${minorversion}
livecheck.regex     "archive/v(0${majorversion}\[\\.\\d\]?)${extract.suffix}"

set nick            mythtv
set mythtvhomedir   ${prefix}/var/mythtvuser
set mythtvlogdir    ${prefix}/var/log/${nick}${majorversion}
set mythtvrundir    ${prefix}/var/run/${nick}${majorversion}
set mythtvpidfile   ${mythtvrundir}/${nick}
set plistdir        ${prefix}/Library/LaunchDaemons
set plistlabel      org.mythtv.mythbackend
set plistfile       org.mythtv.mythbackend.plist
set mythverstring   v0${majorversion}${minorversion}-${shorthash}-MacPorts
set mythbranch      fixes/0${majorversion}
set applescripts    {Myth_Frontend Myth_Filldatabase Myth_Setup Myth_Stop_Start}
set mysqlver        mariadb
set pythonbranch    2.7
set pythonver       python${pythonbranch}
set pythonbin       ${prefix}/bin/${pythonver}
set pymodver        py27
perl5.branches      5.34
# change to 1 to print reinplace warnings
set maintainer_mode 0
if {!$maintainer_mode} {
	set qflag "-q"
} else {
	set qflag ""
}

subport mythtv-core${majorversion} { }
subport mythtv-plugins${majorversion} { }

if {$subport eq "mythtv-core${majorversion}"} {
    # https://trac.macports.org/ticket/69196
    known_fail          yes

    revision            ${corerev}
    name                mythtv-core${majorversion}
    version             0${majorversion}${minorversion}-Fixes-${lastcommit}

    description         personal video recorder (PVR) and media centre system
    long_description    The ultimate Digital Video Recorder and home media \
                        center hub. Think of it as a Free and Open Source alternative \
                        to Windows Media Center or Tivo.

    depends_lib-append  port:bzip2 \
                        port:exiv2 \
                        path:lib/pkgconfig/libass.pc:libass \
                        port:libbluray \
                        port:libcdio \
                        port:libdvdcss \
                        port:libiconv \
                        port:libxml2 \
                        port:fftw-3 \
                        port:fftw-3-single \
                        port:freetype \
                        port:lame \
                        path:lib/libssl.dylib:openssl \
                        port:faac \
                        port:x264 \
                        port:x265 \
                        port:qt5-mysql-plugin \
                        port:qt5-qtscript \
                        port:qt5-qtwebkit \
                        port:taglib \
                        port:zlib \
                        port:${pymodver}-future \
                        port:${pymodver}-mysql \
                        port:${pymodver}-lxml \
                        port:${pymodver}-requests-cache \
                        port:${pymodver}-urlgrabber \
                        port:p${perl5.major}-dbd-mysql \
                        port:p${perl5.major}-http-request-ascgi \
                        port:p${perl5.major}-lwp-useragent-determined \
                        port:p${perl5.major}-io-socket-inet6 \
                        port:p${perl5.major}-date-manip \
                        port:p${perl5.major}-net-upnp
    
    depends_build-append \
                        port:yasm
    
    conflicts           mythtv-core.25 mythtv-core.26 mythtv-core.27
    conflicts_build     hdhomerun

    depends_run         port:logrotate \
                        port:dejavu-fonts

    require_active_variants \
                        qt5-mysql-plugin mariadb55 mysql56
    require_active_variants \
                        p${perl5.major}-dbd-mysql mariadb {mariadb10_0 mariadb10_1 mysql5 mysql51 mysql55 mysql56 mysql57}
    require_active_variants \
                        ${pymodver}-mysql mariadb55 {mysql51 mysql55 mysql56}
    
    pre-fetch {
        if {${os.platform} eq "darwin" && ${os.major} < 13} {
            ui_error "${name} @${version} requires OS X 10.9 or greater."
            return -code error "incompatible OS X version"
        }
    }
    
    post-extract {
        file mkdir ${worksrcpath}/macports
        foreach {applescript} ${applescripts} {
            copy ${filespath}/${applescript}.applescript ${worksrcpath}/macports/${applescript}.applescript
        }
        copy ${filespath}/${plistfile} ${worksrcpath}/macports/${plistfile}
        copy ${filespath}/logrotate.conf ${worksrcpath}/macports/logrotate.conf
        copy ${filespath}/logrotate.mythtv ${worksrcpath}/macports/logrotate.mythtv
        copy ${filespath}/my.cnf ${worksrcpath}/macports/my.cnf
        copy ${filespath}/mythconverg_init.sql ${worksrcpath}/macports/mythconverg_init.sql
    }
    
    # revert rpath linking stuff as it is non-functional in MacPorts
    patchfiles-append   patch-rpath_linking.diff
    # report a couple more items of interest
    patchfiles-append   patch-configure_verbosity.diff
    # If MacPorts libdvdnav is installed, Myth #Includes an old version of libmythdvdnav ?!?
    # occurs following ffmpeg re-sync, see https://github.com/MythTV/mythtv/commit/4f02b2f98a60940449bca05eab67ac764fc1e1e7#diff-282748943dcb8e362702042df95a1d98
    patchfiles-append   patch-dvdnav_include_path.diff
    # if mesa is installed, prevent Myth from finding/using it
    patchfiles-append   patch-configure_opengl_check.diff
    # https://code.mythtv.org/trac/ticket/12857
    patchfiles-append   patch-mythwidgets_osx_focus.diff
    # https://code.mythtv.org/trac/ticket/13001
    patchfiles-append   patch_01_typlayer_qsp_ua_detection.diff
    # backport ffmpeg upstream patch for compatibility with x264 build 153+
    patchfiles-append   patch-x264-build-153.diff

    post-patch {
        set sedPath ${worksrcpath}/mythtv/
        ui_info "Make Myth utils use MacPorts Perl"
        reinplace -locale C "s|check_cmd perl -e|check_cmd ${perl5.bin} -e|" \
            ${sedPath}configure
    
        reinplace -locale C "s|#!/usr/bin/env perl|#!${perl5.bin}|" \
            ${sedPath}programs/scripts/internetcontent/topdocumentaryfilm.pl  \
            ${sedPath}programs/scripts/internetcontent/twit.tv.pl
    
        reinplace -locale C "s|#!/usr/bin/perl|#!${perl5.bin}|" \
            ${sedPath}bindings/perl/Makefile.PL  \
            ${sedPath}contrib/maintenance/optimize_mythdb.pl  \
            ${sedPath}contrib/user_jobs/mythlink.pl  \
            ${sedPath}programs/scripts/database/mythconverg_backup.pl \
            ${sedPath}programs/scripts/database/mythconverg_restore.pl
    
        reinplace -locale C "s|/usr/local/bin|${prefix}/lib/${mysqlver}/bin|"  \
            ${sedPath}programs/scripts/database/mythconverg_backup.pl
    
        ui_info "Record Myth version strings"
        reinplace -locale C "s#\${SOURCE_VERSION}#${mythverstring}#g" ${sedPath}version.sh
        reinplace -locale C "s#\${BRANCH}#${mythbranch}#g" ${sedPath}version.sh
    
        ui_info "Make Myth scripts use MacPorts Python  <<<<"
        # Hack to install Python bindings in ${frameworks_dir}/Python.framework/Versions/2.7
        reinplace -locale C "s|\$(ROOT_FLAGS) \$(PREFIX_FLAGS)|\$(ROOT_FLAGS) \--prefix=\"${frameworks_dir}/Python.framework/Versions/${pythonbranch}\"|" \
            ${sedPath}/bindings/python/Makefile
        reinplace -locale C "s|/usr/local|${prefix}|" ${worksrcpath}/mythtv/bindings/python/MythTV/static.py
        fs-traverse f ${sedPath}/bindings/python/ {
            if {[string equal [ file extension ${f} ] ".py" ]} {
                reinplace {*}$qflag -locale C "s|^#! */usr/bin/env python\$|#!${pythonbin}|" ${f}
            }
        }
        fs-traverse f ${sedPath}/programs/scripts/ {
            if {[string equal [ file extension ${f} ] ".py" ]} {
                reinplace {*}$qflag -locale C "s|^#! */usr/bin/env python\$|#!${pythonbin}|" ${f}
            }
        }
        foreach f [glob -directory ${sedPath}/programs/scripts/hardwareprofile *.py] {
        # fix even though they're currently non-functional on OS X
            reinplace {*}$qflag -locale C "s|^#!/usr/bin/python\$|#!${pythonbin}|" ${f}
        }
    
        foreach {applescript} ${applescripts} {
            reinplace {*}$qflag -locale C "s|@PREFIX@|${prefix}|g" ${worksrcpath}/macports/${applescript}.applescript
            reinplace {*}$qflag -locale C "s|@APPLICATIONS_DIR@|${applications_dir}|" ${worksrcpath}/macports/${applescript}.applescript
            reinplace {*}$qflag -locale C "s|@MYTHTVLOGDIR@|${mythtvlogdir}|" ${worksrcpath}/macports/${applescript}.applescript
            reinplace {*}$qflag -locale C "s|@MYTHTVPIDFILE@|${mythtvpidfile}|" ${worksrcpath}/macports/${applescript}.applescript
        }
        reinplace -locale C "s|@PREFIX@|${prefix}|g" ${worksrcpath}/macports/${plistfile}
        reinplace -locale C "s|@PLISTLABEL@|${plistlabel}|g" ${worksrcpath}/macports/${plistfile}
        reinplace -locale C "s|@PYTHONVER@|${pythonver}|" ${worksrcpath}/macports/${plistfile}
        reinplace -locale C "s|@MYTHTVLOGDIR@|${mythtvlogdir}|" ${worksrcpath}/macports/${plistfile}
        reinplace -locale C "s|@MYTHTVPIDFILE@|${mythtvpidfile}|" ${worksrcpath}/macports/${plistfile}
        reinplace -locale C "s|@PREFIX@|${prefix}|g" ${worksrcpath}/macports/logrotate.conf
        reinplace -locale C "s|@PREFIX@|${prefix}|g" ${worksrcpath}/macports/my.cnf
        reinplace -locale C "s|@MYTHTVLOGDIR@|${mythtvlogdir}|g" ${worksrcpath}/macports/logrotate.mythtv
    }
    # Block various compilers:
    #
    # (shamelessly stolen from Qt4-mac, thanks Michael!)
    # * Some older versions of CLANG do not honor the LIBRARY_PATH
    # environment variable, which is required for compiling
    # this port when using MacPorts.  The versions seem to be: MacPorts
    # CLANG 3.0 or earlier, and Apple CLANG 318.0.61 or older (Xcode 4.3.3 and prior).
    #
    # See http://code.mythtv.org/trac/ticket/11070
    # Run-time failures experienced with certain versions of Clang.  Unable to discern cause.
    # Most recent version of clang seems OK, let's try again
    
    compiler.fallback-append apple-gcc-4.2
    compiler.blacklist-append { clang <= 500.2.79 } \
        macports-clang*
    
    # Clear all MacPorts flags...MythTV is smart enough to set up its own.
    # (ala qt4-mac/Portfile)
    configure.cflags
    configure.cppflags
    configure.cxxflags
    configure.objcflags
    configure.ldflags
    
    configure.universal_cflags
    configure.universal_cppflags
    configure.universal_cxxflags
    configure.universal_ldflags
    
    configure.cc_archflags
    configure.cxx_archflags
    configure.ld_archflags
    
    # make pkg-config act like on Linux, no '-I/opt/local/include' in cflags
    configure.env-append    PKG_CONFIG_SYSTEM_INCLUDE_PATH=${prefix}/include
    
    configure.dir       ${worksrcpath}/mythtv
    configure.args      --compile-type=release \
                        --qmake=${qt_qmake_cmd} \
                        --cc=${configure.cc} \
                        --cxx=${configure.cxx} \
                        --extra-ldflags='-framework ApplicationServices' \
                        --python=${pythonbin} \
                        --disable-mythlogserver \
                        --disable-ceton \
                        --disable-firewire \
                        --disable-audio-jack --disable-indev=jack \
                        --disable-audio-pulseoutput \
                        --disable-gnutls \
                        --disable-libcec \
                        --disable-libpulse \
                        --disable-libvpx \
                        --disable-libxcb \
                        --disable-libxvid \
                        --disable-lzma \
                        --disable-openssl \
                        --disable-qtdbus \
                        --disable-sdl \
                        --disable-systemd_notify \
                        --disable-videotoolbox \
                        --disable-xlib \
                        --disable-xrandr \
                        --disable-xv \
                        --enable-iconv \
                        --enable-fft \
                        --enable-libbluray \
                        --enable-libcrypto \
                        --enable-libmp3lame \
                        --enable-libass \
                        --enable-libx264 --enable-libfaac --enable-nonfree \
                        --enable-libx265 \
                        --enable-securetransport \
                        --enable-vbox \
                        --enable-vda \
                        --enable-zlib
    
    # VideoToolBox: Myth does not currently use this acceleration API even though the
    # underlying ffmpeg does.  Maybe someday...
    
    # --extra-ldflags should not be necessary, see
    # https://code.mythtv.org/trac/ticket/12596
    
    if {${configure.ccache} != "yes"} {
        configure.args-append --disable-ccache
    }
    
    if {${configure.distcc} != "yes"} {
        configure.args-append --disable-distcc
    }

    build.dir           ${worksrcpath}/mythtv
    # make Myth's copy of ffmpeg build verbosely
    build.env-append    V=1
    
    pre-build {
        # workaround broken linker flags, see
        # https://trac.macports.org/ticket/40136
        # 0.28, problem sometimes appears with ffserver, other ffmpeg libs OK?!?        #reinplace -locale C "s|LDFLAGS=  |LDFLAGS= -headerpad_max_install_names |" ${worksrcpath}/mythtv/external/FFmpeg/config.mak
        reinplace -locale C "s|LDFLAGS-ffserver=|LDFLAGS-ffserver=-headerpad_max_install_names |" ${worksrcpath}/mythtv/external/FFmpeg/config.mak
    
        ui_info "Build Myth AppleScript helper apps"
        # Compile applescrips
        foreach {applescript} ${applescripts} {
            system "osacompile -x -o ${worksrcpath}/macports/${applescript}.app ${worksrcpath}/macports/${applescript}.applescript"
        }
        # Myth custom icon for a couple of the applets
        copy -force ${configure.dir}/programs/mythfrontend/mythfrontend.icns ${worksrcpath}/macports/Myth_Frontend.app/Contents/Resources/applet.icns
        copy -force ${configure.dir}/programs/mythfrontend/mythfrontend.icns ${worksrcpath}/macports/Myth_Stop_Start.app/Contents/Resources/applet.icns
    }
    
    # All tests fail with messages like:
    # dyld: Library not loaded: @rpath//opt/local/lib/libmythbase-0.27.0.dylib
    #   Referenced from: /opt/local/var/macports/build/_Users_craigtreleaven_MacPortsTemp_mythtv-core-devel/mythtv-core.27/work/mythtv-8b542e20/mythtv/libs/././libmyth/test/test_audioconvert/test_audioconvert
    #   Reason: image not found
    # ../programs/scripts/unittests.sh: line 15: 41587 Trace/BPT trap          ./$RUNNABLE
    # Need to investigate why rpath not working under MacPorts...
    
    # test.run            yes
    
    destroot.destdir    INSTALL_ROOT=${destroot}
    
    post-destroot {
    # make some directories myth will need
        xinstall -d -m 755 ${destroot}${mythtvhomedir}
        xinstall -d -m 777 ${destroot}${mythtvlogdir}
        xinstall -d -m 777 ${destroot}${mythtvlogdir}/old
        xinstall -d -m 755 ${destroot}${prefix}/etc/logrotate.d
        xinstall -d -m 744 ${destroot}${mythtvrundir}
    
        destroot.keepdirs \
            ${destroot}${mythtvhomedir} \
            ${destroot}${mythtvlogdir} \
            ${destroot}${mythtvlogdir}/old \
            ${destroot}${mythtvrundir}
    
    # install logrotate.mythtv
        xinstall -m 400 ${worksrcpath}/macports/logrotate.mythtv ${destroot}${prefix}/etc/logrotate.d
    
    # install the launchd plist for the backend
        ui_info "Installing startupitem/launchd plist..."
        xinstall -d -m 0755 ${destroot}${plistdir}
        xinstall ${worksrcpath}/macports/${plistfile} ${destroot}${plistdir}
    
    # install scripts we want in share/contrib
        ui_info "Installing share/$name/contrib scripts..."
    
        xinstall -d -m 755 ${destroot}${prefix}/share/${nick}/database
        xinstall -d -m 755 ${destroot}${prefix}/share/${nick}/contrib
    
        xinstall -m 755 \
            ${configure.dir}/database/mc.sql \
            ${worksrcpath}/macports/mythconverg_init.sql \
            ${worksrcpath}/macports/my.cnf \
            ${destroot}${prefix}/share/${nick}/database
        xinstall -m 755 \
            ${configure.dir}/contrib/maintenance/optimize_mythdb.pl \
            ${configure.dir}/contrib/user_jobs/mythlink.pl \
            ${destroot}${prefix}/share/${nick}/contrib
    
    # install some simple Applescripts to launch common myth apps
        ui_info "Installing Applescript helpers..."
        # compile the Applescripts
        xinstall -m 755 -d ${destroot}${applications_dir}/MythTV
    
        foreach {applescript} ${applescripts} {
            copy ${worksrcpath}/macports/${applescript}.app ${destroot}${applications_dir}/MythTV/${applescript}.app
        }
    
    # Myth custom icon for a couple of the applets
        copy -force ${configure.dir}/programs/mythfrontend/mythfrontend.icns ${destroot}${applications_dir}/MythTV/Myth_Frontend.app/Contents/Resources/applet.icns
        copy -force ${configure.dir}/programs/mythfrontend/mythfrontend.icns ${destroot}${applications_dir}/MythTV/Myth_Stop_Start.app/Contents/Resources/applet.icns
    
    # Fix up library entries in dylibs and executables ...
    # need to loop on arch's if/when build universal ?!?
        set libdir ${prefix}/lib
    
        ui_info "Correcting library entries in dylibs..."
    
        foreach {dylib} [glob -types f -tails -directory ${destroot}${libdir} *.dylib] {
            # glob gives us both files and links to files
            if { [file type ${destroot}${libdir}/${dylib}] == "file" } {
                ui_debug "... for ${dylib}"
                system "install_name_tool -id ${libdir}/${dylib} ${destroot}${libdir}/${dylib}"
    
                set otool [lrange [split [exec otool -L ${destroot}${libdir}/${dylib}] \n] 1 end]
    
                foreach {otoolentry} ${otool} {
                    # fix entries like "libmythswscale.dylib" to "${prefix}/lib/libmythswscale.dylib"
                    # assume anything without a leading slash needs fixing
                    set firstchar [string range [string trimleft ${otoolentry}] 0 0]
                    if  { ${firstchar} != "/" }  {
                        set lib [lindex ${otoolentry} 0]
                        ui_debug "      Change ${lib}  to  ${libdir}/[file tail ${lib}]"
                        system "install_name_tool -change \
                            ${lib} \
                            ${libdir}/[file tail ${lib}] \
                            ${destroot}${libdir}/${dylib}"
                    }
                }
            }
        }
        set bindir ${prefix}/bin
        ui_info "Correcting library entries in executables..."
    
        foreach {prog} [glob -types f -tails -directory ${destroot}${bindir} *] {
            # glob gives us files and links to files but should be no links in bin dir
            ui_debug "... for ${prog}"
    
            set otool [lrange [split [exec otool -L ${destroot}${bindir}/${prog}] \n] 1 end]
    
            foreach {otoolentry} ${otool} {
                # fix entries like "libmythswscale.dylib" to "${prefix}/lib/libmythswscale.dylib"
                # assume anything without a leading slash needs fixing
                set firstchar [string range [string trimleft ${otoolentry}] 0 0]
                if  { ${firstchar} != "/" }  {
                    set lib [lindex ${otoolentry} 0]
                    ui_debug "      Change ${lib} to ${libdir}/[file tail ${lib}]"
                    system "install_name_tool -change \
                        ${lib} \
                        ${libdir}/[file tail ${lib}] \
                        ${destroot}${bindir}/${prog}"
                }
            }
        }
    
    # fix lib entries in filters
        set fltdir ${prefix}/lib/mythtv/filters
        set liblist [glob -types f -tails -directory ${destroot}${libdir} -- *.dylib]
        ui_info "Correcting library entries in plugins..."
    
        foreach {flt} [glob -types f -tails -directory ${destroot}${fltdir} *] {
            # glob gives us files and links to files but should be no links in filters dir
            ui_debug "... for ${flt}"
    
            set otool [lrange [split [exec otool -L ${destroot}${fltdir}/${flt}] \n] 1 end]
    
            foreach {otoolentry} ${otool} {
                # fix entries like "libmythswscale.dylib" to "${prefix}/lib/libmythswscale.dylib"
                # assume anything without a leading slash needs fixing
                set firstchar [string range [string trimleft ${otoolentry}] 0 0]
                if  { ${firstchar} != "/" }  {
                    set lib [lindex ${otoolentry} 0]
                    if { [ lsearch $liblist ${lib} ] > 0 } {
                        set goodprefix ${libdir}
                    } else {
                        set goodprefix ${fltdir}
                    }
                    ui_debug "      Change ${lib} to ${goodprefix}/[file tail ${lib}]"
                    system "install_name_tool -change \
                        ${lib} \
                        ${goodprefix}/[file tail ${lib}] \
                        ${destroot}${fltdir}/${flt}"
                }
            }
        }

    # move mythwikiscripts to a more useful spot
    move ${destroot}${frameworks_dir}/Python.framework/Versions/${pythonbranch}/bin/mythwikiscripts \
        ${destroot}${prefix}/bin/

    }
    
    universal_variant           no

    set fontdir ${prefix}/share/mythtv/fonts

    post-activate {
        # make logrotate ready to go
        if {![file exists ${prefix}/etc/logrotate.conf]} {
            file copy ${destroot}logrotate.conf ${prefix}/etc/logrotate.conf
        }
        # delete leftover file, if any
        if {[file exists /Library/LaunchDaemons/${plistfile}]} {
            file delete -force /Library/LaunchDaemons/${plistfile}
        }
        # If "startupitem.install" is set to "no" in macports.conf then do not link.
        if {${startupitem.install} != "no"} {
            ln -s ${plistdir}/${plistfile} /Library/LaunchDaemons
        } else {
            notes-append "
            Installation of the startup item was prohibited due to a setting."
        }
        ui_debug "Make fonts available to Myth"
        system -W ${fontdir} "ln -sfv ${prefix}/share/fonts/dejavu-fonts/DejaVuSans*.ttf ."
        system -W ${fontdir} "ln -sfv ${prefix}/share/fonts/liberation-fonts/LiberationSans*.ttf ."
    }
    
    pre-deactivate {
         file delete -force ${plistdir}/${plistfile}
         file delete -force ${fontdir}/DejaVuSans*.ttf
         file delete -force ${fontdir}/LiberationSans*.ttf
    }

    notes "############################################################################
#
# See http://www.mythtv.org/wiki/Myth_for_Mac_with_MacPorts for information
# essential to finishing the initial installation of MythTV!
#
# NB -
# For troubleshooting, all Myth-related logs are stored in
# ${mythtvlogdir}
#
############################################################################"
}

if {$subport eq "mythtv-plugins${majorversion}"} {
    name                mythtv-plugins${majorversion}
    version             0${majorversion}${minorversion}-Fixes-${lastcommit}
    revision            ${pluginsrev}
    description         MythTV plugins for slideshows, music and weather
    long_description    {*}${description}  \
                        Each plugin is a variant.  The non-default plugins currently do not \
                        work on OS X, such as MythNews, MythBrowser and MythNetVision.
    dist_subdir         mythtv-core${majorversion}

    post-patch {
        set sedPath ${worksrcpath}/mythplugins/
        reinplace -locale C "s|check_cmd perl -e|check_cmd ${perl5.bin} -e|" \
            ${sedPath}configure
        reinplace -locale C "s|python=\"python\"|python=\"${pythonbin}\"|" \
            ${sedPath}configure
        # do fixups regardless of whether variant is selected or not
        foreach fixfile [exec find ${sedPath}/mythweather/mythweather/scripts -name "*.p?"] {
            reinplace {*}$qflag "s;/usr/bin/perl;${perl5.bin};g" ${fixfile}
            reinplace {*}$qflag "s;/usr/share/mythtv;${perl5.bin};g" ${fixfile}
        }
    }

    conflicts_build     ffmpeg \
                        metaio

    depends_lib-append  port:mythtv-core${majorversion} \
                        path:lib/libssl.dylib:openssl
    # NB mythplugins/configure reads ${prefix}/include/mythtv/mythconfig.mak
    # and sets several values from that, including cc and cxx

    configure.dir       ${worksrcpath}/mythplugins

    # make pkg-config act like on Linux, no '-I/opt/local/include' in cflags
    configure.env-append    PKG_CONFIG_SYSTEM_INCLUDE_PATH=${prefix}/include

    configure.args      --compile-type=release \
                        --qmake=${qt_qmake_cmd} \
                        --python=${pythonbin} \
                        --disable-mytharchive \
                        --disable-mythgame \
                        --disable-mythzoneminder \
                        --disable-mythbrowser \
                        --disable-mythnetvision \
                        --disable-mythgallery \
                        --disable-mythmusic \
                        --disable-mythnews \
                        --disable-mythweather

    build.dir           ${worksrcpath}/mythplugins
    destroot.destdir    INSTALL_ROOT=${destroot}

    if {[variant_isset mythnetvision] || [variant_isset mythweather]} {
    # for both MythNetvision and MythWeather (Canada)
        depends_lib-append      port:p${perl5.major}-perlmagick
    }

    # Linking error, see https://code.mythtv.org/trac/ticket/12839
    # also need to patch 3 occurrences of mysql/mysql.h to mariadb/mysql/mysql.h
    #variant mythzoneminder description {Interface with a ZoneMinder video surveillance system} {
    #    configure.args-delete   --disable-mythzoneminder
    #    configure.args-append   --enable-mythzoneminder
    #}

    variant mytharchive description {Create DVDs from recorded shows and any video files available on Myth.} {
        configure.args-delete   --disable-mytharchive
        configure.args-append   --enable-mytharchive
    }

    variant mythbrowser description {WebKit-based browser within Myth.} {
        configure.args-delete   --disable-mythbrowser
    #   configure.args-append   --enable-mythbrowser
    }

    variant mythnetvision description {Find and play internet videos within Myth.} {
        depends_lib-append      port:${pymodver}-oauth
        configure.args-delete   --disable-mythnetvision
    #   configure.args-append   --enable-mythnetvision
    }

    variant mythgallery description {Myth photo browser and slideshow plugin.} {
        depends_lib-append      port:libexif \
                                port:dcraw
        configure.args-delete   --disable-mythgallery
        configure.args-append   --enable-mythgallery --enable-opengl --enable-new-exif
    }

    variant mythmusic description {Myth plugin to play and manage music files.} {
        depends_lib-append      port:libvorbis \
                                port:flac \
                                port:taglib \
                                port:lame
    #                           port:cdio port:fftw-3  -- included in mythtv-core
        configure.args-delete   --disable-mythmusic
        configure.args-append   --enable-mythmusic --enable-fftw  --enable-cdio
    }

    variant mythnews description {Check news sources through RSS feeds with this Myth plugin.} {
        configure.args-delete   --disable-mythnews
    #   configure.args-append   --enable-mythnews
    }

    variant mythweather description {Weather information for your chosen locations via Myth plugin.} {
        depends_lib-append      port:p${perl5.major}-date-manip\
                                port:p${perl5.major}-xml-simple \
                                port:p${perl5.major}-xml-xpath \
                                port:p${perl5.major}-image-size \
                                port:p${perl5.major}-datetime-format-iso8601 \
                                port:p${perl5.major}-soap-lite \
                                port:p${perl5.major}-json
        configure.args-delete   --disable-mythweather
    #   configure.args-append   --enable-mythweather
    }
    default_variants +mythgallery +mythmusic +mythweather

}

if {$subport eq "mythtv${majorversion}"} {
    version             0${majorversion}${minorversion}-Fixes-${lastcommit}
    revision            ${metarev}
    description         installs complete MythTV system
    long_description    \
    Installs a complete MythTV system including the frontend, backend, and database \
    server software plus MythWeb

    depends_lib         port:mythtv-plugins${majorversion} \
                        port:mythweb${majorversion}

    depends_run         port:mariadb-server
    
    distfiles
    installs_libs no
    use_configure no
    build {}
    destroot {
        xinstall -m 0755 ${filespath}/README_mythtv${majorversion}_MacPorts.txt \
            ${destroot}${prefix}/share/mythtv-readme
    }

    pre-pkg {
        xinstall -m 0755 ${filespath}/preinstall ${filespath}/postinstall \
            ${package.scripts}/
        reinplace -locale C "s|@PREFIX@|${prefix}|g" \
            ${package.scripts}/preinstall \
            ${package.scripts}/postinstall
        long_description-append .  Install prefix ${prefix}
    }
}
