# -*- 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                   cmake 1.1
PortGroup                   app 1.0
PortGroup                   legacysupport 1.1

# https://trac.macports.org/ticket/68033
legacysupport.newest_darwin_requires_legacy 16

if {${os.platform} eq "darwin" && ${os.major} < 14} {
    PortGroup               qt4 1.0

} else {
    PortGroup               qt5 1.0

    # qt5.min_version       ??  # Project does not indicate
    qt5.depends_component   qttools
}

name                        avidemux
version                     2.8.1
revision                    8

categories                  multimedia
platforms                   macosx
license                     GPL-2
maintainers                 nomaintainer
homepage                    https://avidemux.sourceforge.net

description                 video editor designed for simple cutting, filtering and encoding tasks
long_description            Avidemux is a free video editor designed for simple cutting, \
                            filtering and encoding tasks. It supports many file types, \
                            including AVI, DVD compatible MPEG files, MP4 and ASF, using \
                            a variety of codecs. Tasks can be automated using projects, \
                            job queue and powerful scripting capabilities.

master_sites                sourceforge:project/avidemux/avidemux/${version}
distname                    ${name}_${version}

checksums                   rmd160  c4c3591c880eb54f52f3cbcec601363839c8f295 \
                            sha256  77d9bdca8683ce57c192b69d207cfab7cf92a7759ce0f63fa37b5c8e42ad3da2 \
                            size    26126065

patchfiles                  patch-admPlugins_VapourSynth.cmake.diff \
                            patch-cpuchecks.diff

# https://github.com/mean00/avidemux2/pull/500
patchfiles-append           0001-ADM_imageSave.cpp-add-missing-header.patch \
                            0002-ADM_coreUtils.h-fix-symbols-export.patch \
                            0003-Fix-missing-ADM_swap64.patch

# remove VERSION file from ffmpeg source, that conflicts with newer compilers
# that have a <version> header. The VERSION file has been removed in ffmpeg 6.0
# which will be used in the next release of avidemux
patchfiles-append           patch-avidemux-remove-VERSION.diff

# fix c++11-narrowing error using upstream patch
# https://github.com/mean00/avidemux2/commit/65878cb39f3d9a2e51cb248cdab9724824f80558
patchfiles-append           65878cb39f3d9a2e51cb248cdab9724824f80558.patch


depends_build-append        port:bash \
                            port:gmake \
                            path:bin/pkg-config:pkgconfig \
                            port:yasm

depends_lib-append          port:fontconfig \
                            port:freetype \
                            port:fribidi \
                            path:lib/pkgconfig/libass.pc:libass \
                            port:libmad \
                            port:zlib

default_variants            +aac +gettext +lame +opus +sqlite +twolame +vorbis +vpx +x264 +x265 +xvid

# only build with one job (otherwise it fails in avidemux_core)
use_parallel_build          no

# ffmpeg requires a C11 compiler, assume avidemux does too
compiler.c_standard         2011
compiler.cxx_standard       2011

cmake.set_cxx_standard      yes

configure.cxxflags-append   -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=1

configure.args-append       -DCMAKE_C_COMPILER=${configure.cc} \
                            -DCMAKE_C_FLAGS="${configure.cflags}" \
                            -DCMAKE_CXX_COMPILER=${configure.cxx} \
                            -DCMAKE_CXX_FLAGS="${configure.cxxflags}" \
                            -DFAKEROOT=${destroot} \
                            -DAVIDEMUX_SOURCE_DIR=${worksrcpath}

# some default settings
configure.args-append       -DGTK=OFF \
                            -DUSE_EXTERNAL_LIBASS=ON \
                            -DUSE_EXTERNAL_LIBMAD=ON

# disable most options, enable them in variants
configure.args-append       -DAFTEN=OFF \
                            -DARTS=OFF \
                            -DASAN=OFF \
                            -DDCAENC=OFF -DLIBDCA=OFF \
                            -DESD=OFF \
                            -DFAAC=OFF -DFAAD=OFF \
                            -DFONTCONFIG=ON -DFREETYPE2=ON \
                            -DFRIDIBI=ON \
                            -DGETTEXT=OFF \
                            -DJACK=OFF \
                            -DLAME=OFF \
                            -DLIBVORBIS=OFF -DVORBIS=OFF \
                            -DOPENCORE_AMRNB=OFF -DOPENCORE_AMRWB=OFF \
                            -DOPUS=OFF -DOPUS_ENCODER=OFF \
                            -DTWOLAME=OFF \
                            -DVAPOURSYNTH=OFF \
                            -DVPXDEC=OFF -DVPXENC=OFF\
                            -DX264=OFF \
                            -DX265=OFF \
                            -DXVID=OFF

if {${os.platform} eq "darwin" && ${os.major} < 14} {
    configure.args-append   -DENABLE_QT4=ON
} else {
    configure.args-append   -DENABLE_QT5=ON

    configure.env-append    QT5_DIR=${qt_dir} QTDIR=${qt_dir}
}

if {${configure.build_arch} in [list ppc ppc64]} {
    configure.args-append   -DADM_CPU_ALTIVEC=ON
}

# make bundled copy of ffmpeg build verbosely
# configure.env-append       V=1

# build {
configure {
    # mimic bootStrap.bash Process() behaviour
    proc process {work src module build extra} {
        # make build dir
        file mkdir ${work}/${build}
    
        # configure
        configure.post_args     ${extra} ${src}/${module}
        configure.dir           ${work}/${build}
        command_exec configure
        
        # build
        build.dir               ${work}/${build}
        command_exec build
        
        # install to destroot
        destroot.dir            ${work}/${build}
        command_exec destroot
    }

    # build avidemux core
    process ${workpath} ${worksrcpath} avidemux_core build_core ""
    # build avidemux GTK UI
    #process ${workpath} ${worksrcpath} avidemux/gtk build_gtk "-DCMAKE_EXE_LINKER_FLAGS=-Wl,-framework,Cocoa"
    # build avidemux QT UI
    process ${workpath} ${worksrcpath} avidemux/qt4 build_qt4 ""
    # build avidemux cli UI
    process ${workpath} ${worksrcpath} avidemux/cli build_cli ""
    # build plugins
    process ${workpath} ${worksrcpath} avidemux_plugins build_pluginsCommon "-DPLUGIN_UI=ALL"
}

# build and destroot is handled above
build {}
destroot {}

variant aac description {Support for faac/faad audio codecs} {
    depends_lib-append  port:faac
    depends_lib-append  port:faad2
    configure.args-replace  -DFAAC=OFF -DFAAC=ON
    configure.args-replace  -DFAAD=OFF -DFAAD=ON
}
variant dts description {Support for DTS (libdca) audio codec} {
    depends_lib-append  port:libdca
    configure.args-replace  -DDCAENC=OFF -DDCAENC=ON
    configure.args-replace  -DLIBDCA=OFF -DLIBDCA=ON
}
variant esound description {Support for ESD audio device} {
    depends_lib-append  port:esound
    configure.args-replace  -DESD=OFF -DESD=ON
}
variant gettext description {Support for gettext} {
    depends_lib-append  port:gettext
    configure.args-replace  -DGETTEXT=OFF -DGETTEXT=ON
}
variant jack description {Support for Jack audio device} {
    # Do not make a default until this bug is fixed:
    # https://github.com/jackaudio/jack2/issues/950
    depends_lib-append  port:jack
    configure.args-replace  -DJACK=OFF -DJACK=ON
}
variant lame description {Support for LAME audio codec} {
    depends_lib-append  port:lame
    configure.args-replace  -DLAME=OFF -DLAME=ON
}
variant opus description {Support for Opus audio codec} {
    depends_lib-append  port:libopus
    configure.args-replace  -DOPUS=OFF -DOPUS=ON
    configure.args-replace  -DOPUS_ENCODER=OFF -DOPUS_ENCODER=ON
}
variant sqlite description {Support for SQLite3} {
    depends_lib-append  port:sqlite3
}
variant twolame description {Support for TwoLAME audio codec} {
    depends_lib-append  port:twolame
    configure.args-replace  -DTWOLAME=OFF -DTWOLAME=ON
}
variant vorbis description {Support for Vorbis audio codec} {
    depends_lib-append  port:libvorbis
    configure.args-replace  -DVORBIS=OFF -DVORBIS=ON
    configure.args-replace  -DLIBVORBIS=OFF -DLIBVORBIS=ON
}
variant vpx description {Support for VP8 video codec} {
    depends_lib-append  path:lib/pkgconfig/vpx.pc:libvpx
    configure.args-replace  -DVPXDEC=OFF -DVPXDEC=ON
    configure.args-replace  -DVPXENC=OFF -DVPXENC=ON
}
variant x264 description {Support for x264 video codec} {
    depends_lib-append  port:x264
    configure.args-replace  -DX264=OFF -DX264=ON
}
variant x265 description {Support for x265 video codec} {
    depends_lib-append  port:x265
    configure.args-replace  -DX265=OFF -DX265=ON
}
variant xvid description {Support for XviD MPEG-4 video codec} {
    depends_lib-append  port:XviD
    configure.args-replace  -DXVID=OFF -DXVID=ON
}

# .app stuff
app.name                    Avidemux
app.executable              Avidemux2.8
app.use_launch_script       yes
app.icon                    cmake/osx/avidemux.icns

livecheck.url   http://www.fosshub.com/Avidemux.html
livecheck.regex "${name}_(\\d+(?:\\.\\d+)*)${extract.suffix}"
