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

name                kchmviewer
version             7.7
revision            1

categories          devel
description         A CHM (Winhelp) files viewer written for Qt.
long_description    kchmviewer is a reader for CHM (Winhelp) and EPUB \
                    files using the Qt toolkit. \
                    The main point of kchmviewer is compatibility with \
                    non-English chm files, including most international charsets.

maintainers         nomaintainer
platforms           darwin linux
license             GPL-3

homepage            https://sourceforge.net/projects/kchmviewer
distname            ${name}-${version}
master_sites        sourceforge:project/${name}/${name}/${version}

checksums           rmd160  3b072a3225c5a4c796a9406232c3f696285d9aa4 \
                    sha256  27cbac45c786b1718550a87e6f86010e161302b426c6396ff2a3091b913b17dd \
                    size    345952

# kchmviewer's KDE variant still requires KDE4 and will thus not be shipped for now.
# The pure Qt variant is obtained by using QMake instead of CMake.
if {${os.platform} ne "darwin" || ${os.major} > 11} {
    PortGroup       qmake5 1.0

    # kchmviewer is transitioning to QtWebEngine, but that adaptation is not yet functional.
    # (and WebKit is a much more appropriate choice anyway.)
    qt5.depends_component \
                    qtwebkit
} else {
    # Fallback for older systems without Qt5:
    PortGroup       qmake 1.0

}

depends_build-append \
                    port:dos2unix

depends_lib-append  port:libzip \
                    port:chmlib

post-extract {
    # force consistent line-endings
    system -W ${worksrcpath} "find . \\\( -name \"*.cpp\" -o -name \"*.h\" -o -name \"*.pro\" \\\) -exec dos2unix '{}' \";\""
}

patch.pre_args-append   -N
patch.pre_args-replace  -p0 -p1
# make it build and work as intended.
# Some of these patches have been submitted upstream:
# https://sourceforge.net/p/kchmviewer/patches/9/
patchfiles-append   patch-release-build.diff \
                    patch-libzip-incpath.diff \
                    patch-use-webkit-and-dbus.diff \
                    patch-avoid-private-member.diff \
                    patch-main-mac.diff \
                    patch-encoding-no-abort.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" \
                    ${worksrcpath}/lib/libebook/libebook.pro \
                    ${worksrcpath}/src/src.pro
}

# zip.h: error: wrong number of arguments specified for ‘deprecated’ attribute
compiler.blacklist-append   *gcc-4.0 *gcc-4.2

configure.cppflags-append   -I${prefix}/lib/libzip/include
configure.pre_args-append   ${worksrcpath}/${name}.pro
configure.args-append       -recursive
configure.dir               ${workpath}/build
build.dir                   ${configure.dir}
post-patch {
    xinstall -m 755 -d ${configure.dir}
}

platform darwin {
    destroot {
        xinstall -m 755 -d ${destroot}${qt_apps_dir}
        file copy ${build.dir}/bin/kchmviewer.app ${destroot}${qt_apps_dir}
        if {[info procs qt5.add_app_wrapper] ne ""} {
            qt5.add_app_wrapper kchmviewer
        }
    }
}

platform linux {
    destroot {
        xinstall -m 755 ${build.dir}/bin/kchmviewer ${destroot}${prefix}/bin
    }
}

livecheck.regex /${name}-(\[0-9.\]+)${extract.suffix}
