# -*- 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           github 1.0
PortGroup           qt5 1.0

github.setup        sqlitebrowser sqlitebrowser 3.13.1 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
set git-commit      5d84f53
# This line is for displaying commit in about panel only
revision            0

categories          databases
platforms           darwin linux
license             GPL-3
maintainers         {i0ntempest @i0ntempest} openmaintainer

description         GUI editor for SQLite databases
long_description    SQLite Database Browser is a visual tool used to create, design and edit \
                    database files compatible with SQLite. Its interface is based on Qt, and is \
                    meant to be used by users and developers who want to create databases or edit \
                    and search data using a familiar spreadsheet-like interface, without the need \
                    to learn complicated SQL commands. Controls and wizards are available for users \
                    to: \
                    * Create and compact database files \
                    * Create, define, modify and delete tables \
                    * Create, define and delete indexes \
                    * Browse, edit, add and delete records \
                    * Search records \
                    * Import and export records as text \
                    * Import and export tables from/to CSV files \
                    * Import and export databases from/to SQL dump files \
                    * Issue SQL queries and inspect the results \
                    * Examine a log of all SQL commands issued by the application. \
                    SQLite Database Browser is not a visual shell for the sqlite command line tool. \
                    It does not require familiarity with SQL commands.

homepage            http://sqlitebrowser.org

checksums           rmd160  7a1c5787fe1ee40475a093aa4d087e926fd9d6ac \
                    sha256  4f8db07262c9402f50f40dda36c36d6ce9081e3d74ab9ad51c43f31048fcf5ec \
                    size    4924197

# Exclude pre-release candidates
github.livecheck.regex \
                    {([0-9.]+)}

universal_variant   no

depends_lib-append  port:qscintilla-qt5

patchfiles-append   0001-Changes-for-MacPorts.patch

compiler.cxx_standard \
                    2011

# Uses QSysInfo::buildAbi and QSysInfo::currentCpuArchitecture
qt5.min_version 5.4

qt5.depends_build_component \
                    qttools

configure.args-append \
                    -DBUILD_STABLE_VERSION=1

post-patch {
    reinplace "s|\"Unknown\"|\"${git-commit}\"|" CMakeLists.txt
}

platform darwin {
    post-patch {
        reinplace "s|/usr/local/opt/qscintilla2/lib|${qt_libs_dir}|g" cmake/FindQScintilla.cmake
        reinplace "s|/usr/local/opt/qscintilla2/include|${qt_includes_dir}|g" cmake/FindQScintilla.cmake
    }
    configure.args-append \
                    -DQSCINTILLA_INCLUDE_DIR=${qt_includes_dir}
}

platform linux {
    cmake.install_rpath-prepend \
                    ${qt_libs_dir} ${prefix}/lib
    configure.args-append \
                    -DCMAKE_PREFIX_PATH=${prefix} \
                    -DQSCINTILLA_INCLUDE_DIR=${qt_includes_dir} \
                    -DQT_LIBRARY_DIR=${qt_libs_dir}
}

variant sqlite3 conflicts sqlcipher description {Use sqlite3 as db engine} {
    depends_lib-append port:sqlite3
}

variant sqlcipher conflicts sqlite3 description {Use sqlcipher as db engine} {
    depends_lib-append port:sqlcipher
    configure.args-append -Dsqlcipher=1
}

if {[info procs cmake.save_configure_cmd] ne ""} {
    # create a .macports-$subport-configure.cmd file containing the cmake invocation details
    # (provided by a pending update to cmake-1.1
    cmake.save_configure_cmd "log too"
}

if (![variant_isset sqlcipher]) {
    default_variants  +sqlite3
}

platform darwin {
    destroot {
        set appname "DB Browser for SQLite"
        set contents_folder "${build.dir}/${appname}.app/Contents"
        set resources_folder "${contents_folder}/Resources/"
        # steps from ${worksrcpath}/installer/macos/notarize.sh
        xinstall -m 755 -d ${resources_folder}
        xinstall -m 644 {*}[glob ${worksrcpath}/LICENSE*] ${resources_folder}
        xinstall -m 644 ${worksrcpath}/installer/macos/macapp.icns ${resources_folder}
        system -W ${contents_folder} "/usr/libexec/PlistBuddy -c \"Set :CFBundleIconFile macapp.icns\" Info.plist"
        system -W ${build.dir} "/usr/bin/codesign --sign - \"${appname}.app\""
        copy "${build.dir}/${appname}.app" ${destroot}${applications_dir}
        ln -s "${applications_dir}/${appname}.app/Contents/MacOS/${appname}" ${destroot}${prefix}/bin/${name}
    }
}
