# -*- 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
# $Id: Portfile 88141 2011-12-20 17:48:33Z pixilla@macports.org $

PortSystem          1.0

name                pure-ftpd
set pretty_name     Pure-FTPd
version             1.0.35

categories          net
platforms           darwin
maintainers         pixilla
license             BSD

homepage            http://pureftpd.org/
description         ${pretty_name} is a fast, production-quality, standard-conformant FTP (SSL/TLS) \
                    server, based upon Troll-FTPd.

long_description    ${pretty_name} has been designed to be secure in default configuration\; it has no\
                    known vulnerabilities. Features include: virtual domains, built-in 'ls', \
                    anti-warez system, configurable ports for passive downloads, FXP protocol, \
                    bandwidth throttling, ratios, LDAP / MySQL / PostgreSQL-based authentication,\
                    fortune files, Apache-like log files, text / HTML / XML real-time status report,\
                    virtual users, virtual quotas, privilege separation, SSL/TLS and more.

master_sites        ftp://ftp.pureftpd.org/pure-ftpd/releases/ \
                    ftp://ftp.fr.pureftpd.org/pure-ftpd/releases/ \
                    http://download.pureftpd.org/pub/pure-ftpd/releases/

use_bzip2           yes

checksums           rmd160  525ae8c4564b7a8f29ec09b882eacaa7a025a911 \
                    sha256  cbbdf6185480491618817710ba4a19bd58e2854c61e643967bd0841f89d0e79c
                    
livecheck.type      regex
livecheck.url       http://download.pureftpd.org/pub/${name}/releases/
livecheck.regex     ${name}-(\[0-9.\]+)\\.tar

configure.args      --with-everything \
                    --with-pam \
                    --with-virtualchroot \
                    --with-paranoidmsg \
                    --with-bonjour \
                    --without-inetd \
                    --mandir=${prefix}/share/man \
                    --infodir=${prefix}/share/info \
                    --sysconfdir=${prefix}/etc/${name}/conf \
                    --localstatedir=${prefix}/var

destroot.destdir    prefix=${destroot}${prefix} \
                    mandir=${destroot}${prefix}/share/man

destroot.keepdirs   ${destroot}${prefix}/etc/${name}/ssl \
                    ${destroot}${prefix}/etc/${name}/conf \
                    ${destroot}${prefix}/var/log/${name}

post-destroot {
    file mkdir ${destroot}${prefix}/etc/${name}/ssl
    file mkdir ${destroot}${prefix}/etc/${name}/conf
    file mkdir ${destroot}${prefix}/var/log/${name}
    file mkdir ${destroot}${prefix}/share/doc/${name}
    
    # Install the notes and readme files
    xinstall -m 644 -W ${worksrcpath} \
        AUTHORS ChangeLog CONTACT COPYING FAQ HISTORY INSTALL NEWS README \
        pureftpd-ldap.conf pureftpd-mysql.conf pureftpd-pgsql.conf pureftpd.schema \
        README.Authentication-Modules README.Configuration-File \
        README README.Authentication-Modules README.Configuration-File README.Contrib \
        README.Donations README.LDAP README.PGSQL README.TLS \
        README.MySQL README.Virtual-Users README.MySQL README.PGSQL README.TLS README.Virtual-Users \
        THANKS \
        ${destroot}${prefix}/share/doc/${name}
    
    # Install in the modified README.MacOS-X file
    xinstall -m 644 ${filespath}/README.MacOS-X ${destroot}${prefix}/share/doc/${name}/README.MacOS-X
    reinplace "s|@PREFIX@|${prefix}|g" ${destroot}${prefix}/share/doc/${name}/README.MacOS-X
    
    # Copy in the sample launchd plists item
    file copy ${filespath}/org.pure-ftpd.ftpd.plist.basic.sample.in ${destroot}${prefix}/share/doc/${name}/org.pure-ftpd.ftpd.plist.basic.sample
    reinplace "s|@PREFIX@|${prefix}|g" ${destroot}${prefix}/share/doc/${name}/org.pure-ftpd.ftpd.plist.basic.sample
    
    # Copy in the sample pure-ftpd pam file
    file copy ${filespath}/pure-ftpd.pam.10.5.sample ${destroot}${prefix}/share/doc/${name}/pure-ftpd.pam.10.5.sample
    file copy ${filespath}/pure-ftpd.pam.10.6.sample ${destroot}${prefix}/share/doc/${name}/pure-ftpd.pam.10.6.sample
   
}

# Variants
variant uploadscript description "Configures ${name} to support post processing of uploaded files" {
    configure.args-append --with-uploadscript
}
variant mysql5 conflicts postgresql82 postgresql83 postgresql84 description "Use MySQL 5 for privileges database" {
    depends_lib-append path:bin/mysql_config5:mysql5
    configure.args-append --with-mysql
    configure.env-append PATH=${prefix}/lib/mysql5/bin:$env(PATH)
}

variant postgresql82 conflicts mysql5 postgresql83 postgresql84 description "Use PostgreSQL 8.2 for privileges database" {
    depends_lib-append port:postgresql82
    configure.args-append --with-pgsql
    configure.env-append PATH=${prefix}/lib/postgresql82/bin:$env(PATH)
}

variant postgresql83 conflicts mysql5 postgresql82 postgresql84 description "Use PostgreSQL 8.3 for privileges database" {
    depends_lib-append port:postgresql83
    configure.args-append --with-pgsql
    configure.env-append PATH=${prefix}/lib/postgresql83/bin:$env(PATH)
}

variant postgresql84 conflicts mysql5 postgresql82 postgresql83 description "Use PostgreSQL 8.4 for privileges database" {
    depends_lib-append port:postgresql84
    configure.args-append --with-pgsql
    configure.env-append PATH=${prefix}/lib/postgresql84/bin:$env(PATH)
}

variant tls description "Encryption of ftp control and data channels using SSL/TLS" {
    depends_lib-append      port:openssl
    configure.args-append   --with-tls \
                            --with-certfile=${prefix}/etc/${name}/ssl/pure-ftpd.pem
}

# Language variants, should only be able to choose one.
set variants {
    lang_brazilian
    lang_czech
    lang_danish
    lang_dutch
    lang_french
    lang_french_funny
    lang_german
    lang_italian
    lang_korean
    lang_norwegian
    lang_polish
    lang_romanian
    lang_russian
    lang_simplified_chinese
    lang_slovak
    lang_spanish
    lang_swedish
    lang_trad_chinese
}

foreach variant_name $variants {
    # Quick way to remove the working variant from the list
    set the_conflicts [lsearch -all -inline -not -exact $variants $variant_name]
    # String replace away the lang_ part, as that is not part of the name used for the compile args
    set variant_configure_name [string map {"lang_" ""} $variant_name]
    set variant_line "variant $variant_name conflicts $the_conflicts description \"Use $variant_name for server messages\" {
        configure.args-append --with-language=$variant_configure_name }"
    eval $variant_line

}

# Notify the user how to launch the ftpd
post-activate {
    ui_msg "========================================================================"
    ui_msg "${pretty_name} documentation is located in: ${prefix}/share/doc/${name}"
    ui_msg ""
    
    ui_msg "You can start ${pretty_name} from the command line with:"
    ui_msg "    sudo ${prefix}/sbin/${name} &"
    
    ui_msg "A future release of ${pretty_name} will no longer support"
    ui_msg "xinetd, and has been removed from this version.  If you are using that"
    ui_msg "method to start ${pretty_name} now, please switch to using launchd(8)."
    ui_msg ""
    
    ui_msg "A sample launchd plist has been created to get you started."
    ui_msg "    1) org.pure-ftpd.ftpd.plist.basic.sample"
    ui_msg "        Basic but secure chrooted ftp server"
    ui_msg ""
    
    ui_msg "To install the launchd item, issue the following commands:"
    ui_msg "cd ${prefix}/share/doc/${name}/"
    ui_msg "sudo cp org.pure-ftpd.ftpd.plist.basic.sample /Library/LaunchDaemons/org.pure-ftpd.ftpd.plist"
    ui_msg "  - and then load the launchd item - "
    ui_msg "sudo launchctl load -w /Library/LaunchDaemons/org.pure-ftpd.ftpd.plist"
    ui_msg ""
    
    ui_msg "If you intend to use pure-FTPd with PAM, meaning you want to authenticate against Mac OS X"
    ui_msg "user accounts, please copy the pure-ftpd.pam.10.x.sample file to /etc/pam.d/pure-ftpd"
    ui_msg "cd ${prefix}/share/doc/${name}/"
    ui_msg "sudo cp pure-ftpd.pam.10.x.sample /etc/pam.d/pure-ftpd"
    
    ui_msg "You can now test the server with:"
    ui_msg "    ftp localhost"
    ui_msg "You should see a Welcome to ${pretty_name} message."
    ui_msg ""
    
    ui_msg "See ${homepage} for more information."
    ui_msg "========================================================================"
}
