# -*- 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                esorepo
version             1.1
revision            2
categories          science
license             GPL-2+
platforms           any
supported_archs     noarch
maintainers         eso.org:usd-help

description         ESO repository of software for astronomical data reduction

long_description    \
    The European Southern Observatory (ESO) maintains and distributes a number \
    of open source software packages (e.g. data reduction pipelines and front-end \
    tools) for the astronomical science community. This port provides a convenient \
    configuration mechanism to add an additional repository URL to allow further \
    installation of ESO software via MacPorts.

homepage            http://www.eso.org/sci/software

# port has no distfiles
distfiles
use_configure no

build {}

destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/${name}
    xinstall -m 644 ${filespath}/eso-pubkey.pem ${destroot}${prefix}/share/${name}/eso-pubkey.pem
}

pre-activate {
    set config_file [open ${prefix}/etc/macports/sources.conf r]
    set config_data [read ${config_file}]
    close ${config_file}
    set already_in_sources 0
    if {[regexp -line {^ftp://ftp\.eso\.org/pub/dfs/pipelines/repositories/stable/macports/ports\.tar$} ${config_data}]} {
        set already_in_sources 1
        ui_error "${prefix}/etc/macports/sources.conf already contains a reference to ftp://ftp.eso.org/pub/dfs/pipelines/repositories/stable/macports/ports.tar."
    }
    set config_file [open ${prefix}/etc/macports/archive_sites.conf r]
    set config_data [read ${config_file}]
    close ${config_file}
    set already_in_archive_sites 0
    if {[regexp -line {^urls\s+ftp://ftp\.eso\.org/pub/dfs/pipelines/repositories/stable/macports/packages$} ${config_data}]} {
        set already_in_archive_sites 1
        ui_error "${prefix}/etc/macports/archive_sites.conf already contains a reference to ftp://ftp.eso.org/pub/dfs/pipelines/repositories/stable/macports/packages."
    }
    set config_file [open ${prefix}/etc/macports/pubkeys.conf r]
    set config_data [read ${config_file}]
    close ${config_file}
    set already_in_pubkeys 0
    # Check for the public key reference with a loop rather than regexp, since ${prefix} may contain one of '[](){}?.\'
    foreach line [split ${config_data} "\n"] {
        if {"${line}" == "${prefix}/share/${name}/eso-pubkey.pem"} {
            set already_in_pubkeys 1
            ui_error "${prefix}/etc/macports/pubkeys.conf already contains a reference to ${prefix}/share/${name}/eso-pubkey.pem."
            break
        }
    }
    if {${already_in_sources} || ${already_in_archive_sites} || ${already_in_pubkeys}} {
        ui_error "A number of references to the ESO repository have already been configured."
        ui_error "Thus, either the repository is already accessible, in which case this port is"
        ui_error "not necessary, or the configuration is not complete."
        ui_error "If you want to add the ESO repository to MacPorts with this port then remove"
        ui_error "all references mentioned above and try install this port again."
        return -code error "ESO repository might have already been configured manually."
    }
    set config_file [open ${prefix}/etc/macports/sources.conf a]
    puts ${config_file} "# ESO MacPorts Portfile source repository (automatically added by ${name})."
    puts ${config_file} "ftp://ftp.eso.org/pub/dfs/pipelines/repositories/stable/macports/ports.tar"
    close ${config_file}
    set config_file [open ${prefix}/etc/macports/archive_sites.conf a]
    puts ${config_file} "# ESO MacPorts binary package repository (automatically added by ${name})."
    puts ${config_file} "name             ESO repository"
    puts ${config_file} "type             tbz2"
    puts ${config_file} "prefix           /opt/local"
    puts ${config_file} "applications_dir /Applications/MacPorts"
    puts ${config_file} "frameworks_dir   /opt/local/Library/Frameworks"
    puts ${config_file} "urls             ftp://ftp.eso.org/pub/dfs/pipelines/repositories/stable/macports/packages"
    close ${config_file}
    set config_file [open ${prefix}/etc/macports/pubkeys.conf a]
    puts ${config_file} "# ESO RSA public signature key (automatically added by ${name})."
    puts ${config_file} "${prefix}/share/${name}/eso-pubkey.pem"
    close ${config_file}
}

post-deactivate {
    reinplace -E "/^# ESO MacPorts Portfile source repository \\(automatically added by ${name}\\)\\.\[\[:space:]]*$/{ \
        N;/\\nftp:\\/\\/ftp\\.eso\\.org\\/pub\\/dfs\\/pipelines\\/repositories\\/stable\\/macports\\/ports\\.tar\[\[:space:]]*$/d;}" \
        ${prefix}/etc/macports/sources.conf
    reinplace -E "/^# ESO MacPorts binary package repository \\(automatically added by ${name}\\)\\.\[\[:space:]]*$/{ \
        N;N;N;N;N;N;/\\nurls \[\[:space:]]*ftp:\\/\\/ftp\\.eso\\.org\\/pub\\/dfs\\/pipelines\\/repositories\\/stable\\/macports\\/packages\[\[:space:]]*$/d;}" \
        ${prefix}/etc/macports/archive_sites.conf
    reinplace -E "/^# ESO RSA public signature key \\(automatically added by ${name}\\)\\.\[\[:space:]]*$/{ \
        N;/\\n\[^#]*\\/share\\/${name}\\/eso-pubkey\\.pem\[\[:space:]]*$/d;}" \
        ${prefix}/etc/macports/pubkeys.conf
}

notes "\
    Please run 'sudo port sync' to synchronize with the ESO repository.\n\n\
    WARNING: This port adds additional sources, binary download servers and\
    cryptographic keys to your MacPorts installation. It is technically\
    possible for ESO to provide modified binary packages signed with their key\
    for ports that are not part of their repository. Additionally, ESO's FTP\
    server will be checked for available binary packages for each\
    installation, which may give ESO metadata about what you are trying to\
    install. If you do not want this, you can always uninstall this port."
