# -*- 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 88778 2012-01-11 16:23:52Z ryandesign@macports.org $

PortSystem          1.0

name                logrotate
version             3.8.1
categories          sysutils
maintainers         pixilla openmaintainer
platforms           darwin

description         Rotates, compresses, and mails system logs

long_description    The logrotate utility is designed to simplify the administration of log files \
                    on a system which generates a lot of log files. Logrotate allows for the \
                    automatic rotation compression, removal and mailing of log files. Logrotate \
                    can be set to handle a log file \daily, weekly, monthly or when the log file \
                    gets to a certain size.

homepage            https://fedorahosted.org/logrotate/
master_sites        https://fedorahosted.org/releases/l/o/logrotate/

checksums           rmd160  cec3bc6a4cb4226b31696977935818695027b0f8 \
                    sha256  c12471e70ae8bc923bd5c4f25e8fd6483b68c6301f3cd79f7cfe37bc5b370169

patchfiles          patch-Makefile.diff

depends_run         port:popt port:gettext

post-extract {
    touch ${worksrcpath}/.depend
}

post-patch {
    reinplace "s|@@PREFIX@@|${prefix}|g" \
        ${worksrcpath}/examples/logrotate-default
}

use_configure       no

variant universal {}

build.env-append    CC="${configure.cc} [get_canonical_archflags]" \
                    CXX="${configure.cxx} [get_canonical_archflags]" \
                    CPP="${configure.cpp} [get_canonical_archflags]" \
                    BASEDIR="${prefix}" \
                    POPT_DIR="${prefix}" \
                    STATEFILE="${prefix}/var/run/logrotate/logrotate.status" \
                    DEFAULT_MAIL_COMMAND="/usr/sbin/mail"

destroot.args       INSTALL="install" \
                    BINDIR="${destroot}${prefix}/sbin" \
                    MANDIR="${destroot}${prefix}/share/man"
destroot.keepdirs   ${destroot}${prefix}/etc/logrotate.d \
                    ${destroot}${prefix}/var/run/logrotate
post-destroot {
    xinstall -d ${destroot}${prefix}/share/${name}
    set fp [open ${destroot}${prefix}/share/${name}/logrotate.conf.example w]
    puts $fp \
"# see \"man logrotate\" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# Drop log rotation information into this directory
include ${prefix}/etc/logrotate.d"
    close $fp

    set fp [open ${destroot}${prefix}/share/${name}/org.macports.logrotate.plist.example w]
    puts $fp \
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
	<key>Disabled</key>
	<true/>
	<key>Label</key>
	<string>com.pixilla.logrotate</string>
	<key>ProgramArguments</key>
	<array>
		<string>${prefix}/sbin/logrotate</string>
		<string>${prefix}/etc/logrotate.conf</string>
	</array>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>5</integer>
		<key>Minute</key>
		<integer>30</integer>
	</dict>
</dict>
</plist>"
    close $fp
    if {[variant_isset startupitem]} {
        xinstall -d ${destroot}${prefix}/etc/LaunchDaemons/org.macports.logrotate
        xinstall -d ${destroot}/Library/LaunchDaemons
        xinstall -m 644 ${destroot}${prefix}/share/${name}/org.macports.logrotate.plist.example \
            ${destroot}${prefix}/etc/LaunchDaemons/org.macports.logrotate/org.macports.logrotate.plist
        ln -sf "${prefix}/etc/LaunchDaemons/org.macports.logrotate/org.macports.logrotate.plist" "${destroot}/Library/LaunchDaemons/org.macports.logrotate.plist"
    }
}

variant bzip2 conflicts gzip description {Use bzip2 compression by default} {
    build.env-append \
                    COMPRESS_COMMAND="${prefix}/bin/bzip2" \
                    COMPRESS_EXT=".bz2" \
                    UNCOMPRESS_COMMAND="${prefix}/bin/bunzip2"
    depends_run-append \
                    port:bzip2
}

variant gzip conflicts bzip2 description {Use gzip compression by default} {
    build.pre_args-append \
                    COMPRESS_COMMAND="${prefix}/bin/gzip" \
                    COMPRESS_EXT=".gz" \
                    UNCOMPRESS_COMMAND="${prefix}/bin/gunzip"
    depends_run-append \
                    port:gzip
}

variant startupitem description {Install launchd plist} {
}

if {![variant_isset bzip2]} {
    default_variants-append \
                    +gzip
    variant_set     gzip
}

livecheck.url       $homepage
livecheck.type      regex
livecheck.regex     "(?!${name}).*${name}-((?!${extract.suffix}).*)${extract.suffix}"

notes \
"

To use logrotate:

  * If ${prefix}/etc/logrotate.conf does not exist copy the example into place.

    \$ sudo cp ${prefix}/share/${name}/logrotate.conf.example ${prefix}/etc/logrotate.conf

  * Logrotate scripts added to ${prefix}/etc/logrotate.d are read by default.

    \$ sudo ${prefix}/sbin/${name} ${prefix}/etc/logrotate.conf

  * With the startupitem variant (+startupitem) you can activate a scheduled log 
    rotation with this command.

    \$ sudo port load Logrotate

  * To create your own startup item with non-default intervals, build logrotate
    without the startupitem variant (-startupitem), copy the example into place
    and edit to your liking and load with launchctl when finished.

    \$ sudo cp ${prefix}/share/${name}/org.macports.logrotate.plist.example /Library/LaunchDaemons/org.macports.logrotate.plist

    \$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.logrotate.plist
    
"
