name: Generate TdApi.java

on:
  push:
    branches:
      - master
      - develop

jobs:
  build:
    strategy:
      matrix:
        include:
        - {os: ubuntu-22.04, arch: "linux/amd64", implementation: "tdlight"}
    runs-on: ${{ matrix.os }}
    steps:
      - name: Install ubuntu 22.04 dependencies
        if: matrix.os == 'ubuntu-22.04'
        uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: sudo make git zlib1g-dev libssl-dev gperf php-cli cmake openjdk-17-jdk clang-14 libc++-14-dev libc++abi-14-dev libgcc-11-dev libunwind-14-dev libclang-common-14-dev ccache maven
          version: 1.0
      - name: Install SSH key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.IG_SSH_KEY }}
          name: id_rsa
          known_hosts: ${{ secrets.IG_KNOWN_HOSTS }}
      - uses: actions/checkout@v3
        with:
          submodules: "recursive"
      - name: Setup Java (Snapshot)
        if: github.ref == 'refs/heads/develop'
        uses: actions/setup-java@v3
        with:
          java-version: 17
          distribution: temurin
          server-id: mchv-snapshot-distribution
          server-username: MAVEN_USERNAME
          server-password: MAVEN_PASSWORD
          cache: 'maven'
      - name: Setup Java (Release)
        if: github.ref != 'refs/heads/develop'
        uses: actions/setup-java@v3
        with:
          java-version: 17
          distribution: temurin
          server-id: mchv-release-distribution
          server-username: MAVEN_USERNAME
          server-password: MAVEN_PASSWORD
          cache: 'maven'
      - name: Set up Maven
        uses: stCarolas/setup-maven@v4.5
        with:
          maven-version: 3.8.2
      - name: Setup variables
        shell: bash
        run: |
          set -xeo pipefail
          # ====== Variables
          export REVISION=4.0.${{ github.run_number }}
          export GH_MATRIX_OS=${{ matrix.os }}
          export GH_MATRIX_ARCH=${{ matrix.arch }}
          export IMPLEMENTATION_NAME=${{ matrix.implementation }}

          echo "REVISION=$REVISION" >> $GITHUB_ENV
          echo "BUILD_TYPE=$BUILD_TYPE" >> $GITHUB_ENV
          echo "IMPLEMENTATION_NAME=$IMPLEMENTATION_NAME" >> $GITHUB_ENV
          if [[ ! -z "$PATH" ]]; then
            echo "PATH=$PATH" >> $GITHUB_ENV
          fi
          echo "JAVA_HOME=$JAVA_HOME" >> $GITHUB_ENV
          echo "JAVA_INCLUDE_PATH=$JAVA_INCLUDE_PATH" >> $GITHUB_ENV
          cat $GITHUB_ENV > github.env
      - name: Build
        shell: bash
        run: |
          set -xeo pipefail
          echo "REVISION: $REVISION"

          ./.github/workflows/scripts/build_api.sh
      - uses: actions/upload-artifact@v3
        with:
          name: tdlight-api-${{env.REVISION}}.jar
          path: api/target/tdlight-api-${{env.REVISION}}.jar
      - uses: actions/upload-artifact@v3
        with:
          name: tdlight-api-${{env.REVISION}}-legacy.jar
          path: api/target-legacy/tdlight-api-${{env.REVISION}}-legacy.jar
      - name: Deploy to Maven (Snapshot)
        if: github.ref != 'refs/heads/master'
        shell: bash
        run: |
          set -xeo pipefail
          echo "REVISION: $REVISION"

          echo "Building standard package"
          mvn -B -f api/pom.xml clean package deploy -Drevision="$REVISION"
          echo "Deploying legacy package"
          mvn -B -f api/pom.xml clean package -Dlegacy=true -Drevision="$REVISION"
          mvn -B org.apache.maven.plugins:maven-deploy-plugin:3.1.1:deploy-file -Durl=https://mvn.mchv.eu/repository/mchv-snapshot \
                                                                            -DrepositoryId=mchv-snapshot-distribution \
                                                                            -Dfile=api/target-legacy/tdlight-api-$REVISION-legacy.jar \
                                                                            -Dpackaging=pom \
                                                                            -DgroupId=it.tdlight \
                                                                            -DartifactId=tdlight-api \
                                                                            -Dversion=$REVISION \
                                                                            -Drevision=$REVISION \
                                                                            -Dclassifier=legacy \
                                                                            -Dlegacy=true
          echo "Done."
        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: |
          set -xeo pipefail
          echo "REVISION: $REVISION"

          echo "Deploying standard package"
          mvn -B -f api/pom.xml clean package deploy -Drevision="$REVISION"
          
          echo "Deploying legacy package"
          mvn -B -f api/pom.xml clean package -Dlegacy=true -Drevision="$REVISION"
          mvn -B org.apache.maven.plugins:maven-deploy-plugin:3.1.1:deploy-file -Durl=https://mvn.mchv.eu/repository/mchv \
                                                                            -DrepositoryId=mchv-release-distribution \
                                                                            -Dfile=api/target-legacy/tdlight-api-$REVISION-legacy.jar \
                                                                            -Dpackaging=pom \
                                                                            -DgroupId=it.tdlight \
                                                                            -DartifactId=tdlight-api \
                                                                            -Dversion=$REVISION \
                                                                            -Drevision=$REVISION \
                                                                            -Dclassifier=legacy \
                                                                            -Dlegacy=true
          
          
          echo "Deploying docs"
          cd "./api/target/apidocs"
          find . -name '*.html' -exec sed -i -r 's/<\/title>/<\/title>\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><style>\n\t#memberSummary_tabpanel{overflow-x: auto;}\n\tli.blockList{overflow-x: auto;}\n\ttd.colLast div{max-width:30vw;}\n\t#search{width: 400px;max-width: 65vw;}\n\t.title,.subTitle,pre,.inheritance,h1,h2,h3,h4,h5,.memberSummary,.memberSignature,.typeSummary,.blockList,.contentContainer{white-space:normal;word-break:break-word;}\n\tul{padding-left:10px}\n<\/style>/' {} \;
          git config --global init.defaultBranch master
          git init
          git checkout -b "tdlight"
          git remote add origin "https://${DOCS_DEPLOY_USERNAME}:${DOCS_DEPLOY_PASSWORD}@git.ignuranza.net/tdlight-team/tdlight-docs"
          git config user.email "andrea@warp.ovh"
          git config user.name "Andrea Cavalli"
          git fetch --all
          git reset --soft "origin/tdlight" || true
          git add -A
          git commit -m "Update javadocs"
          git push --set-upstream origin "tdlight"
          echo "Done."
        env:
          MAVEN_USERNAME: ${{ secrets.MCHV_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.MCHV_TOKEN }}
          DOCS_DEPLOY_USERNAME: ${{ secrets.DOCS_DEPLOY_USERNAME }}
          DOCS_DEPLOY_PASSWORD: ${{ secrets.DOCS_DEPLOY_PASSWORD }}

