# -*- 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
PortGroup               golang 1.0

go.setup                github.com/oauth2-proxy/oauth2-proxy 7.8.2 v
go.offline_build        no
revision                0

homepage                https://oauth2-proxy.github.io/oauth2-proxy

description             A reverse proxy that provides authentication with \
                        Google, GitHub or other providers.

long_description        A reverse proxy and static file server that provides \
                        authentication using Providers (Google, GitHub, and \
                        others) to validate accounts by email, domain or group.

categories              security net
installs_libs           no
license                 MIT
maintainers             {gmail.com:herby.gillot @herbygillot} \
                        openmaintainer

checksums               rmd160  5a659a55b612c6155c5a60fd7db98324ddeda365 \
                        sha256  96f23df77e6b31425579edb631492e9fe3a014bb296c5a261eeb6b9ada5c7787 \
                        size    1403803

set o2p_conf_dir        ${prefix}/etc/${name}
set o2p_log_dir         ${prefix}/var/log/${name}
set o2p_share_dir       ${prefix}/share/${name}

set o2p_config_file     ${o2p_conf_dir}/${name}.cfg
set o2p_example_src     ${worksrcpath}/contrib/${name}.cfg.example
set o2p_example_file    ${o2p_share_dir}/${name}.cfg.example
set o2p_log_file        ${o2p_log_dir}/${name}.log
set o2p_user            oauth2proxy

build.cmd               make
build.pre_args          VERSION=${version}
build.args              ${name}

add_users               ${o2p_user} \
                        group=${o2p_user} \
                        realname="Oauth2-Proxy"

destroot.keepdirs-append \
                        ${destroot}${o2p_conf_dir} \
                        ${destroot}${o2p_log_dir}

post-extract {
    copy  ${filespath}/org.macports.${name}.plist \
          ${workpath}/org.macports.${name}.plist

    reinplace "s|@NAME@|${name}|g"      ${workpath}/org.macports.${name}.plist
    reinplace "s|@USER@|${o2p_user}|g"  ${workpath}/org.macports.${name}.plist
    reinplace "s|@GROUP@|${o2p_user}|g" ${workpath}/org.macports.${name}.plist
    reinplace "s|@PREFIX@|${prefix}|g"  ${workpath}/org.macports.${name}.plist

    reinplace "s|@CONF_FILE@|${o2p_config_file}|g" \
        ${workpath}/org.macports.${name}.plist

    reinplace "s|@LOGFILE@|${o2p_log_file}|g" \
        ${workpath}/org.macports.${name}.plist

    # Modify the Makefile to disable fetching dependencies during build
    reinplace {s|GO111MODULE=on|GO111MODULE=off|g} ${worksrcpath}/Makefile

    # Modify the Makefile to build the current worksrcpath rather than
    # referencing the project's go module specifically.
    reinplace -E {s|(.*build.*) github.com/oauth2-proxy/oauth2-proxy/v7(.*)|\1 .\2|} \
        ${worksrcpath}/Makefile
}


destroot {
    xinstall -m 0755 ${worksrcpath}/${name} ${destroot}${prefix}/bin/
    xinstall -d -m 0755 -o ${o2p_user} ${destroot}${o2p_log_dir}
    xinstall -d -m 0755 ${destroot}${o2p_conf_dir}
    xinstall -d -m 0755 ${destroot}${o2p_share_dir}
    copy ${o2p_example_src} ${destroot}${o2p_example_file}

    xinstall -d -m 0755 \
        ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}

    xinstall -m 0644 -o root -W ${workpath} org.macports.${name}.plist \
        ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}

    xinstall -d -m 0755 ${destroot}/Library/LaunchDaemons

    ln -s ${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist \
        ${destroot}/Library/LaunchDaemons/org.macports.${name}.plist
}

post-activate {
    if {![file exists ${o2p_config_file}]} {
        copy ${o2p_example_file} ${o2p_config_file}
    }
}

notes "
    Please edit the configuration for ${name} at: ${o2p_config_file}

    Once done, start the service with: \$ sudo port load ${name}

    ${name} will log to: ${o2p_log_file}

    To stop and remove the service, do: \$ sudo port unload ${name}
"
