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

github.setup        backuppc backuppc 4.4.0
revision            0
categories          sysutils
maintainers         nomaintainer
license             GPL-3+
platforms           {darwin any}
supported_archs     noarch
homepage            https://backuppc.github.io/backuppc/

description         BackupPC is a high-performance, enterprise-grade system for backing up Linux, WinXX and Mac OS X PCs and laptops to a server.
long_description    {*}${description} BackupPC is highly configurable and easy to install and maintain.

github.tarball_from releases

perl5.require_variant   yes
perl5.conflict_variants yes
perl5.branches          5.28 5.30 5.32 5.34
perl5.default_branch    5.34
perl5.create_variants   ${perl5.branches}
require_active_variants mod_perl2 ${perl5.variant}

depends_lib         port:bzip2 \
                    port:gnutar \
                    port:gzip \
                    port:mod_perl2 \
                    port:openssh \
                    port:p${perl5.major}-backuppc-xs \
                    port:p${perl5.major}-cgi \
                    port:p${perl5.major}-encode \
                    port:p${perl5.major}-file-listing \
                    port:p${perl5.major}-getopt-long \
                    port:par2 \
                    path:bin/rsync:rsync \
                    port:samba3

distname            BackupPC-${version}

checksums           rmd160  8bb40d0fc07305df5765bac7ae6962815d5756fd \
                    sha256  8bc04cd513d47629508bd2fd6f455ced38316974913638e971f44e37786c6365 \
                    size    657309

patchfiles          patch-configure.pl.diff

post-patch {
    reinplace "s|@@PERL@@|perl${perl5.major}|g" ${worksrcpath}/configure.pl
    reinplace "s|@@PERL_FULL@@|${perl5.bin}|g"  ${worksrcpath}/configure.pl
}

add_users           backuppc group=backuppc realname=BackupPC

configure.args      --batch \
                    --backuppc-user=backuppc \
                    --cgi-dir ${prefix}/var/wwwbackup/cgi-bin \
                    --data-dir ${prefix}/var/backups \
                    --dest-dir ${destroot} \
                    --html-dir ${prefix}/var/wwwbackup/htdocs/backuppc \
                    --html-dir-url /backuppc \
                    --install-dir ${prefix}  \
                    --config-dir ${prefix}/etc/BackupPC \
                    --log-dir ${prefix}/var/log/BackupPC \
                    --hostname localhost

use_configure       no
build {}

destroot.violate_mtree  yes

destroot.keepdirs   ${destroot}${prefix}/var/backups \
                    ${destroot}${prefix}/var/backups/cpool \
                    ${destroot}${prefix}/var/backups/log \
                    ${destroot}${prefix}/var/backups/trash \
                    ${destroot}${prefix}/var/backups/pool \
                    ${destroot}${prefix}/var/backups/pc \
                    ${destroot}${prefix}/var/backups/ \
                    ${destroot}${prefix}/var/log/BackupPC \
                    ${destroot}${prefix}/var/wwwbackup/htdocs\
                    ${destroot}${prefix}/var/wwwbackup/logs

destroot {
    # destroot for the web skeleton
    xinstall -d -m 770 -g backuppc -o backuppc ${destroot}${prefix}/var/wwwbackup
    xinstall -d -m 770 -g backuppc -o backuppc ${destroot}${prefix}/var/wwwbackup/cgi-bin
    xinstall -d -m 770 -g backuppc -o backuppc ${destroot}${prefix}/var/wwwbackup/conf
    xinstall -m 600 -g backuppc -o backuppc ${filespath}/httpd.conf ${destroot}${prefix}/var/wwwbackup/conf/

    xinstall -m 600 -g backuppc -o backuppc ${filespath}/mime.types ${destroot}${prefix}/var/wwwbackup/conf
    xinstall -m 600 -g backuppc -o backuppc ${filespath}/magic ${destroot}${prefix}/var/wwwbackup/conf

    xinstall -d -m 770 -g backuppc -o backuppc ${destroot}${prefix}/var/wwwbackup/htdocs
    xinstall -m 600 -g backuppc -o backuppc ${filespath}/favicon.ico ${destroot}${prefix}/var/wwwbackup/htdocs/

    xinstall -d -m 770 -g backuppc -o backuppc ${destroot}${prefix}/var/wwwbackup/logs

    # destroot for backuppc
    xinstall -d -m 770 -g backuppc -o backuppc ${destroot}${prefix}/var/backups
    xinstall -d -m 770 -g backuppc -o backuppc ${destroot}${prefix}/var/log/BackupPC

    # create backup directory
    system -W ${worksrcpath} "${prefix}/bin/perl${perl5.major} configure.pl ${configure.args}"
    # done with a patch for now:
    #xinstall -d -m 755 ${destroot}${prefix}/share/doc/
    #move ${destroot}${prefix}/doc ${destroot}${prefix}/share/doc/BackupPC

    # install the launch scripts
    xinstall -d -m 755 ${destroot}/Library/LaunchDaemons

    xinstall ${filespath}/net.sourceforge.backuppc.plist \
            ${destroot}/Library/LaunchDaemons/

    # Install extras
    xinstall -d -m 755 ${destroot}${prefix}/share/BackupPC/extras
    xinstall ${filespath}/backuppc_config_template.rb \
            ${destroot}${prefix}/share/BackupPC/extras
    xinstall ${filespath}/backuppc_auth.conf ${destroot}${prefix}/share/BackupPC/extras
    reinplace "s|@@PREFIX@@|${prefix}|g" ${destroot}${prefix}/share/BackupPC/extras/backuppc_auth.conf

    xinstall ${filespath}/org.macports.backuppc.apache2.plist \
            ${destroot}/Library/LaunchDaemons/
    reinplace "s|@@PREFIX@@|${prefix}|g" ${destroot}/Library/LaunchDaemons/org.macports.backuppc.apache2.plist
    reinplace "s|@@CONFPATH@@|${prefix}/var/wwwbackup/conf/|g" ${destroot}/Library/LaunchDaemons/org.macports.backuppc.apache2.plist
    reinplace "s|@@PREFIX@@|${prefix}|g" ${destroot}${prefix}/var/wwwbackup/conf/httpd.conf
    reinplace "s|@@HOSTNAME@@|localhost|g" ${destroot}${prefix}/var/wwwbackup/conf/httpd.conf
    reinplace "s|@@PREFIX@@|${prefix}|g" ${destroot}/Library/LaunchDaemons/net.sourceforge.backuppc.plist
}

notes "There are several more things you will need to do:

    - Browse through the config file, ${prefix}/etc/BackupPC/config.pl,
      and make sure all the settings are correct.  In particular,
      you will need to set \$Conf{CgiAdminUsers} so you have
      administration privileges in the CGI interface.

    - Change the \$Conf{}

    - Edit the list of hosts to backup in ${prefix}/etc/BackupPC/hosts.

    - Read the documentation in ${prefix}/share/doc/BackupPC/BackupPC.html.
      Please pay special attention to the security section.

    - Verify that the CGI script BackupPC_Admin runs correctly.  You might
      need to change the permissions or group ownership of BackupPC_Admin.
      If this is an upgrade and you are using mod_perl, you will need
      to restart Apache.  Otherwise it will have stale code.

    - BackupPC should be ready to start.  Don't forget to run it
      as user backuppc!  The installation also contains an
      /Library/LaunchDaemons/net.sourceforge.backuppc.plist launchd
      so that BackupPC can auto-start on boot. To enable this daemon
      execute the following command:
      cd /Library/LaunchDaemons
      sudo launchctl load net.sourceforge.backuppc.plist

    - The data directory is set to ${prefix}/var/backups. Please ensure
      You have enough space for your backups at this location. You may
      wish to create a custom mount point point for you backup volume
      if necessary.

    - To hide the backuppc user in the login window, you may execute

      defaults write /Library/Preferences/com.apple.loginwindow

    - Please see the ${prefix}/share/BackupPC/extras directory for client
      setup scripts, sample apache.conf files, and more.
    "
