# -*- 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                bacula
version             15.0.2
revision            0
categories          sysutils
license             {AGPL-3 OpenSSLException}
maintainers         nomaintainer

description         network based backup program
long_description    Bacula is a set of computer programs that permit you to \
                    manage backup, recovery, and verification of computer data across \
                    a network of computers of different kinds.

homepage            http://www.bacula.org/
master_sites        sourceforge

checksums           rmd160  b8016d60363baca6db83287ac02202c18984e52f \
                    sha256  55515c2a66af9a86b955daea4089378b864d051b2e6e30383bef36e693acea7a \
                    size    6524812

depends_build       port:pkgconfig
depends_lib         port:gawk \
                    port:gettext \
                    port:libiconv \
                    port:ncurses \
                    path:lib/libssl.dylib:openssl \
                    port:tcp_wrappers \
                    port:zlib \
                    port:lzo2

patchfiles          dynamic_lookup-11.patch

configure.ccache    no
configure.args      --mandir=${prefix}/share/man \
                    --docdir=${prefix}/share/doc/bacula \
                    --datarootdir=${prefix}/share \
                    --with-pid-dir=${prefix}/var/run \
                    --with-subsys-dir=${prefix}/var/run/subsys \
                    --sysconfdir=${prefix}/etc/${name} \
                    --with-libintl-prefix=${prefix} \
                    --with-openssl=${prefix} \
                    --with-libiconv-prefix=${prefix} \
                    --with-archivedir=/var/tmp \
                    --with-working-dir=${prefix}/var/bacula/working \
                    --with-included-gettext=${prefix} \
                    --enable-smartalloc \
                    --enable-largefile \
                    --enable-conio \
                    --without-sqlite3 \
                    --without-postgresql \
                    --without-mysql \
                    --with-tcp-wrappers

configure.cppflags-append   -I${prefix}/ncurses
configure.ldflags           "-framework CoreFoundation"

destroot.keepdirs   ${destroot}${prefix}/var/run/subsys \
                    ${destroot}${prefix}/var/bacula/working

# Don't overwrite preference files
post-destroot {
    file rename ${destroot}${prefix}/etc/bacula/bacula-fd.conf ${destroot}${prefix}/etc/bacula/bacula-fd.conf.example
    file rename ${destroot}${prefix}/etc/bacula/bconsole.conf ${destroot}${prefix}/etc/bacula/bconsole.conf.example
    file rename ${destroot}${prefix}/etc/bacula/mtx-changer.conf ${destroot}${prefix}/etc/bacula/mtx-changer.conf.example
}

startupitem.create  yes
startupitem.init    "BIN=${prefix}/sbin/bacula-fd\nPID=${prefix}/var/run/bacula-fd.*.pid"
startupitem.start   "\[ -x \${BIN} \] && \${BIN} -c ${prefix}/etc/bacula/bacula-fd.conf"
startupitem.stop    "\[ -r \${PID} \] && /bin/kill \$(cat \${PID})"

notes "To use the Bacula FD client, you'll need to configure \"${prefix}/etc/bacula/bacula-fd.conf\"."
notes-append "To use bconsole, you'll need to configure \"${prefix}/etc/bacula/bconsole.conf\"."

variant client_only conflicts mysql51 mysql55 postgresql16 sqlite3 description "Install bacula client (bacula-fd) only" {
    configure.args-append --enable-client-only
}

variant console_bat description "Install BAT console GUI application" {
    depends_build-append  port:tcp_wrappers \
                          port:makeicns
    depends_lib-append    port:qt4-mac \
                          port:qwt52
    configure.args-append --enable-bat

    patchfiles-append     patch-configure-qt4.diff

    post-patch {
        system "makeicns -in ${worksrcpath}/src/qt-console/images/bat_icon.png -out ${worksrcpath}/src/qt-console/images/bat_icon.icns"
    }
    post-destroot {
        file rename ${destroot}${prefix}/etc/bacula/bat.conf ${destroot}${prefix}/etc/bacula/bat.conf.example
        # for now, copy manually until upstream provider fixes bat.app install routines
        xinstall -m 755 -d ${destroot}${applications_dir}
        copy ${worksrcpath}/src/qt-console/bat.app ${destroot}${applications_dir}/Bacula-BAT.app
    }
    notes-append "To use the Bacula BAT GUI, you'll need to configure \"${prefix}/etc/bacula/bat.conf\"."
}

variant mysql51 conflicts client_only mysql55 postgresql16 sqlite3 description "Install bacula client and server with mysql 5.1 backend" {
    depends_lib-append    port:mysql51
    configure.args-append --with-mysql=${prefix}/include/mysql51/mysql
    configure.args-delete --without-mysql
    configure.args-append --enable-batch-insert
    configure.cppflags-append   -I${prefix}/include/mysql51/mysql

    patchfiles-append     patch-configure-mysql.diff
    post-patch {
                     reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/configure
    }
    notes-append "
       To use the Bacula Director, you'll need to perform the following steps:
         1. create user \"bacula\" on a mysql database
         2. execute \"${prefix}/etc/bacula/create_bacula_database -h <mysql_server> -u <db_admin_user> -p\"
         3. execute \"${prefix}/etc/bacula/make_bacula_tables -h <mysql_server> -u <db_admin_user> -p\"
         4. execute \"${prefix}/etc/bacula/grant_bacula_privileges -h <mysql_server> -u <db_admin_user> -p\"
         5. configure \"${prefix}/etc/bacula/bacula-dir.conf\" and set the catalog database settings for the bacula db user.
    "
}

variant mysql55 conflicts client_only mysql51 postgresql16 sqlite3 description "Install bacula client and server with mysql 5.5 backend" {
    depends_lib-append    port:mysql55
    configure.args-append --with-mysql=${prefix}/include/mysql55/mysql
    configure.args-delete --without-mysql
    configure.args-append --enable-batch-insert
    configure.cppflags-append   -I${prefix}/include/mysql55/mysql

    patchfiles-append     patch-configure-mysql.diff
    post-patch {
                     reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/configure
    }
    notes-append "
       To use the Bacula Director, you'll need to perform the following steps:
         1. create user \"bacula\" on a mysql database
         2. execute \"${prefix}/etc/bacula/create_bacula_database -h <mysql_server> -u <db_admin_user> -p\"
         3. execute \"${prefix}/etc/bacula/make_bacula_tables -h <mysql_server> -u <db_admin_user> -p\"
         4. execute \"${prefix}/etc/bacula/grant_bacula_privileges -h <mysql_server> -u <db_admin_user> -p\"
         5. configure \"${prefix}/etc/bacula/bacula-dir.conf\" and set the catalog database settings for the bacula db user.
    "
}

variant postgresql16 conflicts mysql51 mysql55 sqlite3 client_only description "Install bacula client and server with postgresql 16 backend" {
    depends_lib-append    port:postgresql16
    configure.args-append --with-postgresql
    configure.args-delete --without-postgresql
    configure.args-append --enable-batch-insert
    configure.env-append  PATH=${prefix}/lib/postgresql16/bin:$env(PATH)

    notes-append "
       To use the Bacula Director, you'll need to perform the following steps:
         1. create user \"bacula\" on a PostgreSQL database
         2. execute \"${prefix}/etc/bacula/create_bacula_database -h <postgresql-server> -U <db_admin_user>\"
         3. execute \"${prefix}/etc/bacula/make_bacula_tables -h <postgresql-server> -U <db_admin_user>\"
         4. execute \"${prefix}/etc/bacula/grant_bacula_privileges -h <postgresql-server> -U <db_admin_user> -p\"
         5. configure \"${prefix}/etc/bacula/bacula-dir.conf\" and set the catalog database settings for the bacula db user.
    "
}

variant sqlite3 conflicts client_only mysql51 mysql55 postgresql16 description "Install bacula client and server with sqlite 3 backend" {
    depends_lib-append    port:sqlite3
    configure.args-append --with-sqlite3=${prefix}
    configure.args-delete --without-sqlite3
    configure.args-append --enable-batch-insert
    notes-append "
       To use the Bacula Director, you'll need to perform the following steps:
         1. execute \"${prefix}/etc/bacula/create_bacula_database\"
         2. execute \"${prefix}/etc/bacula/make_bacula_tables\"
         3. configure \"${prefix}/etc/bacula/bacula-dir.conf\" and set the catalog database settings for the bacula db user.
    "
}

default_variants +console_bat

if {![variant_isset mysql51] && ![variant_isset mysql55] && ![variant_isset postgresql16] && ![variant_isset sqlite3]} {
    default_variants-append +client_only
} else {
    # is a server installation

    # Add the server daemons to the startupitem wrapper
    startupitem.init-append    "BIN_DIR=${prefix}/sbin/bacula-dir\nPID_DIR=${prefix}/var/run/bacula-dir.*.pid\n"
    startupitem.init-append    "BIN_SD=${prefix}/sbin/bacula-sd\nPID_SD=${prefix}/var/run/bacula-sd.*.pid\n"
    startupitem.start-append   "\[ -x \${BIN_DIR} -a -f ${prefix}/etc/bacula/bacula-dir.conf \] && \${BIN_DIR} -c ${prefix}/etc/bacula/bacula-dir.conf"
    startupitem.start-append   "\[ -x \${BIN_SD} -a -f ${prefix}/etc/bacula/bacula-sd.conf \] && \${BIN_SD} -c ${prefix}/etc/bacula/bacula-sd.conf"
    startupitem.stop-append    "\[ -r \${PID_DIR} \] && /bin/kill \$(cat \${PID_DIR})"
    startupitem.stop-append    "\[ -r \${PID_SD} \] && /bin/kill \$(cat \${PID_SD})"

    # Don't overwrite existing preference files
    post-destroot {
        file rename ${destroot}${prefix}/etc/bacula/bacula-dir.conf ${destroot}${prefix}/etc/bacula/bacula-dir.conf.example
        file rename ${destroot}${prefix}/etc/bacula/bacula-sd.conf ${destroot}${prefix}/etc/bacula/bacula-sd.conf.example
    }
    notes-append "To use the Bacula Storage Daemon, you'll need to configure \"${prefix}/etc/bacula/bacula-sd.conf\"."
    notes-append "If you are upgrading from a 9.x version of Bacula you must update the database with \"${prefix}/etc/bacula/update_bacula_tables\"."
}

pre-deactivate {
    if {[file exists /Library/LaunchDaemons/org.macports.bacula.plist]} {
         system "launchctl unload /Library/LaunchDaemons/org.macports.bacula.plist"
    }
}

livecheck.type           regex
livecheck.url            ${homepage}
livecheck.regex          {Release ([0-9\.]+)}
