# $Id: Portfile 82553 2011-08-15 19:22:39Z jmr@macports.org $

PortSystem            1.0
name                  backuppc
categories            sysutils
version                3.1.0
revision              1
maintainers           jameskyle
homepage              http://backuppc.sourceforge.net/
description           BackupPC is a high-performance, enterprise-grade system for backing up Linux, WinXX and Mac OS X PCs and laptops to a server's disk.
long_description      ${description} BackupPC is highly configurable and easy to install and maintain.

platforms             darwin

master_sites          sourceforge

depends_lib            port:perl5.12 \
                      port:p5.12-compress-raw-zlib \
                      port:p5.12-archive-zip \
                      port:p5.12-file-rsyncp \
                      port:samba3 \
                      port:gnutar \
                      port:mod_perl2 \
                      port:rsync \
                      port:openssh \
                      port:gzip \
                      port:bzip2 \
                      port:par2

distname              BackupPC-${version}

checksums             md5     84b4471852ef910768eae9963ef932d2 \
                      sha1    3bd6d637f4f08f2fda44a12668b91d47c4abb0a7 \
                      rmd160  22d4cb6dd76f846fde489a301852f50e8db1ae1d

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

patchfiles            patch-configure.pl.diff \
                      patch-lib-backuppc-cgi-view.pm.diff

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

set hostname [exec "hostname"]

pre-configure {

  ui_debug  "Hostname: ${hostname}"
  configure.args-append --hostname ${hostname}

}

configure {
  reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/configure.pl
  reinplace "s|@@PREFIX@@|${prefix}/|g" ${worksrcpath}/lib/BackupPC/CGI/View.pm


  addgroup backuppc
  set gid [existsgroup backuppc]
  adduser backuppc gid=${gid} realname=BackupPC
}

build {}

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 "cd ${worksrcpath} && ${prefix}/bin/perl5.12 configure.pl ${configure.args}"
  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@@|${hostname}|g" ${destroot}${prefix}/var/wwwbackup/conf/httpd.conf
  reinplace "s|@@PREFIX@@|${prefix}|g" ${destroot}/Library/LaunchDaemons/net.sourceforge.backuppc.plist





}

post-activate {
  set finish_message "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.
  "

  ui_msg ${finish_message}

}
