name: Docker multi-arch build

on:
  push:
    branches:
      - master
      - develop

jobs:
  build:
    name: Build (${{ matrix.type }} on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          # todo: re-enable
          #- { os: ubuntu-20.04, type: "linux-386-ssl1" }
          - { os: ubuntu-20.04, type: "linux-amd64-ssl1" }
          - { os: ubuntu-20.04, type: "linux-arm64-ssl1" }
          #- { os: ubuntu-20.04, type: "linux-s390x-ssl1" }
          #- { os: ubuntu-20.04, type: "linux-ppc64le-ssl1" }
          - { os: ubuntu-22.04, type: "linux-amd64-ssl3" }
          #- { os: ubuntu-22.04, type: "linux-arm64-ssl3" }
          #- { os: windows-2019, type: "windows-amd64", vcpkgCommitId: "6ca56aeb457f033d344a7106cb3f9f1abf8f4e98", triplet: "x64-windows-static" }
          #- { os: macos-12, type: "macos-amd64" }
    steps:
      - name: Checkout current repo
        uses: actions/checkout@v2
        with:
          submodules: "recursive"

      - name: Get version
        run: |
          # Get latest commit short hash
          HASH_VERSION=$(git rev-parse --short HEAD)

          # Strip git ref prefix from version
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
          
          # Strip "v" prefix from tag name
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
          
          # Use Docker `latest` tag convention
          [ "$VERSION" == "master" ] && VERSION=latest

          HASH_VERSION=$(echo "$HASH_VERSION" | awk '{print tolower($0)}')
          VERSION=$(echo "$VERSION" | awk '{print tolower($0)}')
          TYPE=${{ matrix.type }}
          SAFE_TYPE=${TYPE///}  # linux-amd64 -> linux-amd64

          REVISION=${{ github.run_number }}
          GH_MATRIX_OS=${{ matrix.os }}
          GH_MATRIX_TYPE=${{ matrix.type }}

          # Store variable for future use
          echo "HASH_VERSION=$HASH_VERSION" >> $GITHUB_ENV
          echo "VERSION=$VERSION" >> $GITHUB_ENV
          echo "TYPE=$TYPE" >> $GITHUB_ENV
          echo "SAFE_TYPE=$SAFE_TYPE" >> $GITHUB_ENV
          echo "SSL_TYPE=$SSL_TYPE" >> $GITHUB_ENV
          echo "REVISION=$REVISION" >> $GITHUB_ENV
          echo "GH_MATRIX_OS=$GH_MATRIX_OS" >> $GITHUB_ENV
          echo "GH_MATRIX_TYPE=$GH_MATRIX_TYPE" >> $GITHUB_ENV

          # Print debug info
          echo "hash version: $HASH_VERSION"
          echo "version: $VERSION"
          echo "safe arch: $SAFE_ARCH"
          echo "ssl type: $SSL_TYPE"

          echo "Variables:"
          
          # Save env to file
          cat $GITHUB_ENV > github.env

      - name: Cache Maven
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-v3-maven-${{ env.SAFE_TYPE }}-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-v3-maven-${{ env.SAFE_TYPE }}-

      - name: Cache packages cache
        uses: actions/cache@v3
        with:
          path:  ~/tdlight-java-natives/tdlight-java-natives/.cache
          key: ${{ runner.os }}-v3-packages-${{ env.SAFE_TYPE }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-v3-packages-${{ env.SAFE_TYPE }}-

      - name: Cache ccache
        uses: actions/cache@v3
        with:
          path: ~/.ccache
          key: ${{ runner.os }}-v2-ccache-${{ env.SAFE_TYPE }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-v2-ccache-${{ env.SAFE_TYPE }}-

      - name: Setup Java (Snapshot)
        if: github.ref == 'refs/heads/develop'
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: temurin
          server-id: mchv-snapshot-distribution
          server-username: MAVEN_USERNAME
          server-password: MAVEN_PASSWORD
      - name: Setup Java (Release)
        if: github.ref != 'refs/heads/develop'
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: temurin
          server-id: mchv-release-distribution
          server-username: MAVEN_USERNAME
          server-password: MAVEN_PASSWORD

      - name: Setup PHP with Readline extension
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.0'
          extensions: readline
      - name: Install ubuntu 20.04 dependencies
        if: matrix.os == 'ubuntu-20.04'
      - uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: make git zlib1g-dev libssl-dev gperf php-cli cmake default-jdk clang-10 libc++-dev libc++abi-dev
          version: 1.0
      - name: Install ubuntu 22.04 dependencies
        if: matrix.os == 'ubuntu-22.04'
      - uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: make git zlib1g-dev libssl-dev gperf php-cli cmake default-jdk clang-14 libc++-dev libc++abi-dev
          version: 1.0
      - name: Install MSVC
        if: matrix.os == 'windows-2019'
        uses: ilammy/msvc-dev-cmd@v1
        with:
          toolset: 14.0
          vs-version: '[16.4,16.5)'
      - name: Setup cmake
        if: matrix.os == 'windows-2019'
        uses: lukka/get-cmake@latest
      - name: Set up Maven
        uses: stCarolas/setup-maven@v4.2
        with:
          maven-version: 3.8.2
      - name: Setup NASM
        if: matrix.os == 'windows-2019'
        uses: ilammy/setup-nasm@v1
      - name: Restore artifacts, or setup vcpkg (do not install any package)
        if: matrix.os == 'windows-2019'
        uses: lukka/run-vcpkg@v10.2
        with:
          # Just install vcpkg for now, do not install any ports in this step yet.
          setupOnly: true
          # Location of the vcpkg submodule in the Git repository.
          vcpkgDirectory: '${{ github.workspace }}/vcpkg'
          vcpkgGitCommitId: '${{ matrix.vcpkgCommitId}}'
          # Since the cache must be invalidated when content of the vcpkg.json file changes, let's
          # compute its hash and append this to the computed cache's key.
          appendedCacheKey: ${{ hashFiles( '**/vcpkg_manifest/vcpkg.json' ) }}
          vcpkgTriplet: ${{ matrix.triplet }}
          # Ensure the vcpkg artifacts are cached, they are generated in the 'CMAKE_BINARY_DIR/vcpkg_installed' directory.
          additionalCachedPaths: ${{ github.workspace }}/vcpkg
      - name: Install vcpkg deps
        if: matrix.os == 'windows-2019'
        run: |
          $VCPKG_DIR/vcpkg install gperf openssl:x64-windows-static zlib:x64-windows-static
        shell: bash
      - name: Build
        shell: bash
        run: |
          echo "REVISION: $REVISION"
          
          source ./.github/workflows/scripts/build-${SAFE_TYPE}.sh
      - id: getfilename
        shell: bash
        run: echo "::set-output name=file::$(cd generated/target; ls tdli*-natives-*.jar)"
      - id: getfilepath
        shell: bash
        run: echo "::set-output name=file::$(ls generated/target/tdli*-natives-*.jar)"
      - uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.getfilename.outputs.file }}
          path: ${{ steps.getfilepath.outputs.file }}
      - name: Deploy to Maven (Snapshot)
        if: github.ref == 'refs/heads/develop'
        shell: bash
        run: |
          echo "REVISION: $REVISION"
          echo "SSL TYPE: $SSL_TYPE"

          source ./scripts/continuous-integration/github-workflows/deploy-snapshot.sh
        env:
          MAVEN_USERNAME: ${{ secrets.MCHV_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.MCHV_TOKEN }}
      - name: Deploy to Maven (Release)
        if: github.ref == 'refs/heads/master'
        shell: bash
        run: |
          echo "REVISION: $REVISION"
          echo "SSL TYPE: $SSL_TYPE"

          source ./scripts/continuous-integration/github-workflows/deploy-release.sh
        env:
          MAVEN_USERNAME: ${{ secrets.MCHV_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.MCHV_TOKEN }}
