distributable:
  url: https://dlcdn.apache.org/httpd/httpd-2.4.57.tar.gz
  strip-components: 1

versions:
  url: https://dlcdn.apache.org/httpd/
  match: /httpd-\d+\.\d+\.\d+.tar.gz/
  strip:
    - /^httpd-/
    - /\.tar\.gz$/

dependencies:
  apache.org/apr: '>=1.3.0'
  apache.org/apr-util: '>=1.3.0'
  github.com/google/brotli: '*'
  nghttp2.org: '*'
  openssl.org: '*'
  pcre.org/v2: '*'
  gnome.org/libxml2: '*'
  zlib.net: '*'
  libexpat.github.io: '*'
  
runtime:
  env:
      HTTPD_ROOT: '{{prefix}}'
      APACHE_CONFDIR: '{{prefix}}/conf'

build:
  dependencies:
    gnu.org/libtool: '*'
    gnu.org/autoconf: '*'
  script: |
    ./configure $ARGS
    make
    make install

    HTTPD_CONF=$(tea httpd -V 2>/dev/null | grep SERVER_CONFIG_FILE | sed 's/.*"\(.*\)"/\1/')
    port=8080
    while lsof -i:$port >/dev/null 2>&1; do
      ((port++))
    done
    if [ "$port" -ne 8080 ]; then
      sed -i '' "s/Listen 8080/Listen $port/" {{prefix}}/$HTTPD_CONF
    fi
    sed -i.bak 's/#ServerName www.example.com:8080/ServerName localhost:'"$port"'/g' {{prefix}}/$HTTPD_CONF

    cd "{{prefix}}/bin"
    sed -i.bak \
        -e "s_{{prefix}}_\$(cd \$(dirname \$0)/.. \&\& pwd)_g" \
        -e "s/^HTTPD=\'\(.*\)\'$/HTTPD=\"\1\"/" \
        apachectl envvars envvars-std
    sed -i.bak \
      -e 's_{{prefix}}_$(cd $(dirname $0)/.. && pwd)_g' \
      -e 's/^HTTPD='\''\(.*\)'\''$/HTTPD="$(cd $(dirname $0)\/.. \&\& pwd)\/bin\/httpd"/' \
      apachectl envvars envvars-std

    rm apachectl.bak envvars.bak envvars-std.bak
    cd ../conf
    sed -i.bak \
        -e 's_{{prefix}}_\${HTTPD\_ROOT}_' \
        httpd.conf
    rm httpd.conf.bak

  env:
    ARGS:
      - --prefix="{{prefix}}"
      - --enable-layout="Apache"
      - --enable-mpms-shared=all
      - --enable-mods-shared=all
      - --enable-authnz-fcgi
      - --enable-so
      - --enable-ssl
      - --enable-cgi
      - --enable-pie
      - --enable-suexec
      - --with-suexec-caller=_www
      - --with-suexec-bin={{prefix}}/suexec
      - --with-port=8080
      - --with-sslport=8443
      - --with-apr={{deps.apache.org/apr.prefix}}
      - --with-apr-util={{deps.apache.org/apr-util.prefix}}
      - --with-brotli={{deps.github.com/google/brotli.prefix}}
      - --with-libxml2={{deps.gnome.org/libxml2.prefix}}
      - --with-mpm=prefork
      - --with-nghttp2={{deps.nghttp2.org.prefix}}
      - --with-ssl={{deps.openssl.org.prefix}}
      - --with-pcre={{deps.pcre.org/v2.prefix}}
      - --with-z={{deps.zlib.net.prefix}}
      - --disable-lua
      - --disable-luajit

provides:
  - bin/ab
  - bin/apachectl
  - bin/checkgid
  - bin/fcgistarter
  - bin/htcacheclean
  - bin/htdbm
  - bin/htdigest
  - bin/htpasswd
  - bin/httpd
  - bin/httxt2dbm
  - bin/logresolve
  - bin/rotatelogs
  - bin/suexec

test: 
  script: |
    httpd -v | grep {{version}}
    apachectl -t -f "$APACHE_CONFDIR/httpd.conf"
