name: continuous-integration/gh-actions/cli

on: [push, pull_request]

jobs:
  build-web:
    env:
      ZMQ_VERSION: v4.3.1
      CPPZMQ_VERSION: v4.3.0
      CFLAGS: -fPIC
      CXXFLAGS: -fPIC
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: recursive
    - name: remove bundled boost
      run: sudo rm -rf /usr/local/share/boost
    - name: set apt conf
      run: |
        echo "Acquire::Retries \"3\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
        echo "Acquire::http::Timeout \"120\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
        echo "Acquire::ftp::Timeout \"120\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
    - name: Install emsdk
      run: |
        git clone https://github.com/emscripten-core/emsdk.git
        cd emsdk
        git pull
        ./emsdk install 3.1.10
        ./emsdk activate 3.1.10

    - name: Install Haven dependencies
      run: |
        sudo apt update
        sudo apt-get --no-install-recommends --yes install python3 build-essential cmake pkg-config libboost-all-dev libssl-dev libunbound-dev libsodium-dev liblzma-dev libreadline6-dev libldns-dev libexpat1-dev doxygen graphviz libpgm-dev libudev-dev
        sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
    - name: Install libzmq
      run: |
        git clone https://github.com/zeromq/libzmq.git -b ${ZMQ_VERSION}
        cd libzmq
        ./autogen.sh
        ./configure
        make -j3
        sudo make install
        sudo ldconfig
    - name: Install cppzmq
      run: |
        git clone https://github.com/zeromq/cppzmq.git -b ${CPPZMQ_VERSION}
        cd cppzmq
        sudo mv *.hpp /usr/local/include
    - name: Install expat
      run: |
        wget https://github.com/libexpat/libexpat/releases/download/R_2_4_8/expat-2.4.8.tar.bz2
        tar -xf expat-2.4.8.tar.bz2
        cd expat-2.4.8
        ./configure --enable-static --disable-shared
        make
        sudo make install
    - name: Install unbound
      run: |
        cd ~/
        wget https://www.nlnetlabs.nl/downloads/unbound/unbound-1.17.0.tar.gz
        tar xzf unbound-1.17.0.tar.gz
        cd unbound-1.17.0
        ./configure --with-libexpat=/usr --with-ssl=/usr
        make
        sudo make install
    - name: build haven web libs
      run: |
        export EMSCRIPTEN=$PWD/emsdk/upstream/emscripten
        source ./emsdk/emsdk_env.sh
        ./bin/build_ci.sh
    - name: Upload artifact
      uses: actions/upload-artifact@v4
      with:
        name: libs
        path: dist/*
    - name: Publish
      uses: actions/setup-node@v1
      with:
        node-version: 16
        registry-url: https://registry.npmjs.org/
    - run: |
        echo //registry.npmjs.org/:_authToken=$NPM_TOKEN > .npmrc
        npm publish
