# -*- 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                mpstats
version             0.3
categories          sysutils macports
license             BSD
platforms           {darwin any}
supported_archs     noarch
maintainers         {cal @neverpanic} openmaintainer
description         submit statistics about your macports installation
long_description \
   This is a script and LaunchAgent which will run weekly to report \
   information about your system and installed ports to a server, which \
   publishes the aggregate statistics on the web. \
   \nThis helps us to make better decisions on which configurations we should \
   support and test more and which ports are most commonly used.

homepage            https://www.macports.org/
distfiles

set launchd_dir     ${prefix}/etc/${startupitem.location}/${startupitem.uniquename}/

startupitem.create   no
startupitem.type     launchd
startupitem.autostart \
                    yes

set fname mpstats
set confname stats

extract.mkdir       yes
extract {
    xinstall -m 644 -W ${filespath} mpstats.tcl mpstats.plist.default ${worksrcpath}
}

configure {
    reinplace "s|@PREFIX@|${prefix}|g" \
        ${worksrcpath}/mpstats.tcl \
        ${worksrcpath}/mpstats.plist.default
    reinplace "s|@LABEL@|${startupitem.uniquename}|g" \
        ${worksrcpath}/mpstats.plist.default
    reinplace "s|@CONFNAME@|${confname}|g" \
        ${worksrcpath}/mpstats.tcl
    reinplace "s|@PORTNAME@|${fname}|g" \
        ${worksrcpath}/mpstats.plist.default
}

build {}

destroot {
    xinstall -m 755 \
        ${worksrcpath}/mpstats.tcl \
        ${destroot}${prefix}/libexec/${fname}

    xinstall -m 755 -d \
        ${destroot}${launchd_dir}
    xinstall -m 444 \
        ${worksrcpath}/mpstats.plist.default \
        ${destroot}${launchd_dir}${startupitem.plist}.default

    xinstall -m 755 -d \
        ${destroot}${prefix}/etc/macports
    xinstall -m 444 \
        ${filespath}/${confname}.conf \
        ${destroot}${prefix}/etc/macports/${confname}.conf

    # install the plist, if startupitem.install is set
    if {[getuid] == 0 && ${startupitem.install}} {
        xinstall -m 755 -d ${destroot}/Library/${startupitem.location}
        # note this symlink *will* be broken at destroot time; we'll place the
        # correct file there on activation
        ln -sf "${launchd_dir}${startupitem.plist}" "${destroot}/Library/${startupitem.location}"
    } else {
        ln -sf ${launchd_dir}${startupitem.plist} ${destroot}${prefix}/etc/${startupitem.location}
    }
}

post-activate {
    set uuidfile ${prefix}/var/macports/stats-uuid
    if {![file exists ${uuidfile}] || [file size ${uuidfile}] == 0} {
        set uuid [exec /usr/bin/uuidgen]
        set fd [open ${uuidfile} w]
        puts $fd $uuid
        close $fd
    }

    xinstall -m 644 \
        ${launchd_dir}${startupitem.plist}.default \
        ${launchd_dir}${startupitem.plist}
    reinplace "s|@WEEKDAY@|[expr {int(7 * rand())}]|g" \
        ${launchd_dir}${startupitem.plist}
    reinplace "s|@HOUR@|[clock format [clock seconds] -format %H]|g" \
        ${launchd_dir}${startupitem.plist}
    reinplace "s|@MINUTE@|[clock format [expr {[clock seconds] + 120}] -format %M]|g" \
        ${launchd_dir}${startupitem.plist}
}

post-deactivate {
    delete ${launchd_dir}${startupitem.plist}
}

notes \
    "Installing this port automatically enables weekly reporting of data to the stats server. \
     Uninstall or deactivate this port if you want to stop providing data to MacPorts."
