distributable:
  url: https://github.com/caddyserver/caddy/archive/v{{version}}.tar.gz
  strip-components: 1
versions:
  github: caddyserver/caddy
build:
  dependencies:
    go.dev: '*'
    curl.se: '*'
  script:
    - run: |
        curl -L "$XCADDY" | tar zxf - --strip-components 1
        go run cmd/xcaddy/main.go build v{{version}} --output {{prefix}}/bin/caddy
      working-directory: xcaddy
  env:
    XCADDY: https://github.com/caddyserver/xcaddy/archive/refs/tags/v0.3.5.tar.gz
provides:
  - bin/caddy
test:
  dependencies:
    curl.se: '*'
    pkgx.sh: 1
  env:
    PORT1: $(pkgx get-port | tail -n1)
    PORT2: $(pkgx get-port | tail -n1)
  script:
    - caddy version | grep {{version}}
    # Create the Caddyfile
    - run: sed -e "s/PORT1/$PORT1/" -e "s/PORT2/$PORT2/" $FIXTURE > Caddyfile
      fixture: |
        {
          admin 127.0.0.1:PORT1
        }

        http://127.0.0.1:PORT2 {
          respond "Hello, Caddy!"
        }
    - cat Caddyfile
    # Run Caddy server in the background with its input redirected from an empty stream
    - run: echo | caddy run --config Caddyfile --adapter caddyfile &
    # Add a 5-second pause to allow time for the server to start up.
    - sleep 5
    - curl -s http://127.0.0.1:$PORT1/config/apps/http/servers/srv0/listen/0 | grep $PORT2
    - curl -s http://127.0.0.1:$PORT2 | grep "Hello, Caddy!"
