# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Wei Congrui < crvv.mail at gmail dot com >
# Contributor: Carl George < arch at cgtx dot us >
# Contributor: Eric Engeström <eric at engestrom dot ch>
# Contributor: Andreas Linz <klingt.net at gmail dot com>
# Contributor: Akshay S Dinesh <asdofindia at gmail dot com>

pkgname=caddy
pkgver=1.0.5
_gitcommit=11ae1aa6b88e45b077dd97cb816fe06cd91cca67
_distcommit=9e93bfd85c97d71ab842a4a4b555d358295c914e
pkgrel=1
pkgdesc='HTTP/2 Web Server with Automatic HTTPS'
url='https://caddyserver.com'
arch=('x86_64')
license=('Apache')
depends=('glibc')
makedepends=('go-pie' 'git')
backup=('etc/caddy/caddy.conf')
source=("git+https://github.com/caddyserver/caddy#commit=${_gitcommit}?signed"
        caddy-${_distcommit}-index.html::https://raw.githubusercontent.com/caddyserver/dist/${_distcommit}/welcome/index.html
        caddy.service
        caddy.tmpfiles
        Caddyfile
        plugins.go)
sha256sums=('SKIP'
            '7668022a48b0cbf459190f0bbfbfb32ae066449a95e006367cac9e1befa80c5f'
            'c14ac8681e0434caf2c68e4a18dc59f8796fdffe9039f2e3c799ca64d37aa1ea'
            'c8f002f5ba59985a643600dc3c871e18e110903aa945ef3f2da7c9edd39fbd7a'
            'fb998b6de7bfe58f65c62eab37a4885e70833d19902da089766ad627a5f5a305'
            '69956ee6a54ee0469fdee77f6d07cccee61699b1ee24e2f94ef6017c7ec1118b')
validpgpkeys=(
  29D0817A67156E4F25DC24782A349DD577D586A5 # Matthew Holt <mholt@users.noreply.github.com>
)

pkgver() {
  cd ${pkgname}
  git describe --tags --match 'v*' | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
    cd ${pkgname}/caddy
    sed 's|/var/www/html|/srv/http|g' -i "${srcdir}/caddy-${_distcommit}-index.html"
    sed 's|Version: "unknown"|Version: "v'${pkgver}'"|' -i caddymain/run.go

    cat > main.go <<EOF
package main

import (
  "github.com/caddyserver/caddy/caddy/caddymain"
EOF
    if [ ${#_plugins[@]} -gt 0 ]; then
        echo "enabled plugins: ${_plugins[*]}"
        go run "${srcdir}/plugins.go" "${_plugins[@]}" >> main.go
    fi
    cat >> main.go <<EOF
)

func main() {
  caddymain.EnableTelemetry = false
  caddymain.Run()
}
EOF
}

build() {
  cd ${pkgname}/caddy
  go build -v \
    -trimpath \
    -ldflags "-extldflags ${LDFLAGS}" \
    .
}

package() {
  cd ${pkgname}/caddy
  install -Dm 755 caddy -t "${pkgdir}/usr/bin"
  install -Dm 644 "${srcdir}/caddy.service" -t "${pkgdir}/usr/lib/systemd/system"
  install -Dm 644 "${srcdir}/caddy.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/caddy.conf"
  install -Dm 644 "${srcdir}/Caddyfile" "${pkgdir}/etc/caddy/caddy.conf"
  install -Dm 644 "${srcdir}/caddy-${_distcommit}-index.html" "${pkgdir}/usr/share/caddy/index.html"
  install -d "${pkgdir}/etc/caddy/conf.d"
}

# carefully check before enabling any plugin, they are basically untrusted code
# the enabled tls.dns plugins are built by mholt and maintained in the same space
_plugins=(
#    'dns'
#    'docker'
#    'dyndns'
#    'hook.service'
#    'http.authz'
#    'http.awses'
#    'http.awslambda'
#    'http.cache'
#    'http.cgi'
#    'http.cors'
#    'http.datadog'
#    'http.expires'
#    'http.filter'
#    'http.forwardproxy'
#    'http.geoip'
#    'http.git'
#    'http.gopkg'
#    'http.grpc'
#    'http.ipfilter'
#    'http.jwt'
#    'http.locale'
#    'http.login'
#    'http.mailout'
#    'http.minify'
#    'http.nobots'
#    'http.prometheus'
#    'http.proxyprotocol'
#    'http.ratelimit'
#    'http.realip'
#    'http.reauth'
#    'http.restic'
#    'http.s3browser'
#    'http.supervisor'
#    'http.webdav'
#    'net'
#    'supervisor'
    'tls.dns.auroradns'
    'tls.dns.azure'
    'tls.dns.cloudflare'
    'tls.dns.cloudxns'
    'tls.dns.digitalocean'
    'tls.dns.dnsimple'
    'tls.dns.dnsmadeeasy'
    'tls.dns.dnspod'
    'tls.dns.duckdns'
    'tls.dns.dyn'
    'tls.dns.exoscale'
    'tls.dns.gandi'
    'tls.dns.gandiv5'
    'tls.dns.godaddy'
    'tls.dns.googlecloud'
    'tls.dns.lightsail'
    'tls.dns.linode'
    'tls.dns.namecheap'
    'tls.dns.namedotcom'
    'tls.dns.ns1'
    'tls.dns.otc'
    'tls.dns.ovh'
    'tls.dns.powerdns'
    'tls.dns.rackspace'
    'tls.dns.rfc2136'
    'tls.dns.route53'
    'tls.dns.vultr'
)

# vim: ts=2 sw=2 et: