distributable:
  url: https://github.com/facebook/wangle/archive/refs/tags/v{{version.raw}}.tar.gz
  strip-components: 1

versions:
  github: facebook/wangle
  strip: /v/

dependencies:
  boost.org: '*'
  google.com/double-conversion: ^3
  github.com/facebookincubator/fizz: '*'
  fmt.dev: ^10
  facebook.com/folly: '*'
  gflags.github.io: '*'
  google.com/glog: '<0.7' # glog 0.7.0 cannot be simply included
  libevent.org: '*'
  libsodium.org: '*'
  lz4.org: ^1
  openssl.org: ^1.1
  google.github.io/snappy: '*'
  facebook.com/zstd: ^1
  darwin:
    sourceware.org/bzip2: '*'
    zlib.net: '*'

build:
  dependencies:
    cmake.org: ^3
  working-directory: wangle
  script:
    - cmake . -DBUILD_SHARED_LIBS=ON $ARGS
    - make install
    - make clean

    - cmake . -DBUILD_SHARED_LIBS=OFF $ARGS
    - make
    - cp lib/libwangle.a {{prefix}}/lib

    - run: |
        sed -E -i.bak \
          -e "s:{{pkgx.prefix}}:\$\{_IMPORT_PREFIX\}/../../..:g" \
          -e '/^  INTERFACE_INCLUDE_DIRECTORIES/ s|/v([0-9]+)(\.[0-9]+)*[a-z]?/include|/v\1/include|g' \
          -e '/^  INTERFACE_LINK_LIBRARIES/ s|/v([0-9]+)(\.[0-9]+)*[a-z]?/lib|/v\1/lib|g' \
        wangle-targets.cmake
        rm wangle-targets.cmake.bak
      working-directory: '{{prefix}}/lib/cmake/wangle'
  env:
    ARGS:
      - -DCMAKE_INSTALL_PREFIX={{prefix}}
      - -DCMAKE_BUILD_TYPE=Release
      - -DBUILD_TESTS=OFF

test:
  dependencies:
    curl.se: '*'
  script:
    - c++ $CXXFLAGS 'EchoClient.cpp' -o EchoClient
    - c++ $CXXFLAGS 'EchoServer.cpp' -o EchoServer

    # This should all work; but it doesn't in GHA.
    # We should FIXME.
    # # Find a free port
    # - FREE_PORT=0
    # - run: |
    #     FREE_PORT=49152
    #     while lsof -i:$FREE_PORT >/dev/null 2>&1; do
    #       ((FREE_PORT++))
    #     done

    # # Start the EchoServer
    # - run: |
    #     ./EchoServer -port "${FREE_PORT}" &
    #     SERVER_PID=$!
    # - sleep 10

    # # Send test lines to the EchoClient
    # - run: |
    #     ./EchoClient -port "${FREE_PORT}" > out << EOF || true
    #     Hello from tea!
    #     Another test line.
    #     EOF
    #     CLIENT_BACKGROUND_PID=$!

    # - sleep 20

    # # Check the output for the test lines
    # - grep "Hello from tea!" out
    # - grep "Another test line." out
  env:
    CXXFLAGS:
      - -std=c++17
      - -lgflags
      - -lglog
      - -lfolly
      - -lfizz
      - -lwangle
      - -lssl
      - -lcrypto
      - -lfmt
      - -ldouble-conversion
      - -levent
      - -lboost_context
    darwin:
      CXXFLAGS:
        - -lc++abi
    linux:
      CXXFLAGS:
        - -ldl
        - -lpthread
        - -latomic
