name: ytld-sub Release
on:
  push:
    # publish only when pushed to master
    branches:
      - master
  pull_request:
env:
  ACTIONS_STEP_DEBUG: true
  ACTIONS_RUNNER_DEBUG: true
jobs:
  version:
    name: version
    runs-on: ubuntu-latest
    outputs:
      pypi_version: ${{ steps.set_outputs.outputs.pypi_version }}
      local_version: ${{ steps.set_outputs.outputs.local_version }}
      init_contents: ${{ steps.set_outputs.outputs.init_contents }}

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          ref: master

      - name: Set date and commit hash variables
        run: |
          echo "DATE=$(date +'%Y.%m.%d')" >> $GITHUB_ENV
          echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - name: Count number of commits on master on day, minus 1 to account for post-push
        run: |
          echo "DATE_COMMIT_COUNT=$(($(git rev-list --count master --since='${{ env.DATE }} 00:00:00')-1))" >> $GITHUB_ENV
      - name: Set pypi and local version values
        run: |
          echo "LOCAL_VERSION=${{ env.DATE }}+${{ env.COMMIT_HASH }}" >> $GITHUB_ENV
          if [ ${{ env.DATE_COMMIT_COUNT }} -le "0" ]
          then
            echo "PYPI_VERSION=${{ env.DATE }}" >> $GITHUB_ENV
          else
            echo "PYPI_VERSION=${{ env.DATE }}.post${{ env.DATE_COMMIT_COUNT }}" >> $GITHUB_ENV
          fi

      - name: Test versions
        run: |
          echo "${{ env.PYPI_VERSION }}"
          echo "${{ env.LOCAL_VERSION }}"

      - id: set_outputs
        run: |
          echo "pypi_version=${{ env.PYPI_VERSION }}" >> "$GITHUB_OUTPUT"
          echo "local_version=${{ env.LOCAL_VERSION }}" >> "$GITHUB_OUTPUT"
          echo 'init_contents=__pypi_version__ = "${{ env.PYPI_VERSION }}";__local_version__ = "${{ env.LOCAL_VERSION }}"' >> "$GITHUB_OUTPUT"

  ##################################################################################################

  build-linux:
    name: build-linux
    needs:
      - version
    strategy:
      matrix:
        arch: [ "aarch64", "x86_64" ]
    runs-on: ubuntu-latest
    container:
      image: quay.io/pypa/manylinux_2_28_x86_64
    steps:
      - uses: actions/checkout@v3
      - name: Write version to init file
        run: |
          echo '${{ needs.version.outputs.init_contents }}' > src/ytdl_sub/__init__.py
      - name: Install base packages
        run: |
          cat /etc/os-release
          dnf update -y
          dnf install -y epel-release tar wget make gcc openssl-devel bzip2-devel libffi-devel zlib-devel
      - name: Install Python
        run: |
          wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tar.xz
          tar -xf Python-3.10.10.tar.xz
          cd Python-3.10.10 && ./configure --with-ensurepip=install --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
          make -j 8
          make altinstall
          python3.10 --version
          python3.10 -m ensurepip --upgrade
      - name: Build Package
        run: |
          python3.10 -m pip install -e .
          python3.10 -m pip install pyinstaller
          # Build executable
          pyinstaller ytdl-sub.spec
          mkdir -p /opt/builds
          chmod ugo+rwx dist/ytdl-sub
          dist/ytdl-sub -h
          mv dist/ytdl-sub /opt/builds/ytdl-sub_${{ matrix.arch }}

      - name: Upload build
        uses: actions/upload-artifact@v3
        with:
          name: ytdl-sub_${{ matrix.arch }}
          path: /opt/builds/ytdl-sub_${{ matrix.arch }}

  ##################################################################################################

  build-windows:
    name: build-windows
    needs:
      - version
    runs-on: windows-2019
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.10"
      - name: Write version to init file
        run: |
          echo '${{ needs.version.outputs.init_contents }}'> src/ytdl_sub/__init__.py
      - name: Build Package
        run: |
          python -m pip install -e .
          python -m pip install pyinstaller
          pyinstaller ytdl-sub.spec
          .\dist\ytdl-sub.exe -h

      - name: Upload build
        uses: actions/upload-artifact@v3
        with:
          name: ytdl-sub_exe
          path: .\dist\ytdl-sub.exe

  ##################################################################################################

  github-release:
    name: github-release
    needs:
      - version
      - build-linux
      - build-windows
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Write version to init file
        run: |
          echo '${{ needs.version.outputs.init_contents }}' > src/ytdl_sub/__init__.py

      - name: Restore exe build
        uses: actions/download-artifact@v3
        with:
          name: ytdl-sub_exe
          path: /opt/builds

      - name: Restore aarch64 build
        uses: actions/download-artifact@v3
        with:
          name: ytdl-sub_aarch64
          path: /opt/builds

      - name: Restore x86_64 build
        uses: actions/download-artifact@v3
        with:
          name: ytdl-sub_x86_64
          path: /opt/builds

      - name: Rename x86_64 build
        run: |
          mv /opt/builds/ytdl-sub_x86_64 /opt/builds/ytdl-sub

      - name: Inspect build directory, rename x86_64 build
        run: |
          ls -lh /opt/builds

      - name: Create Release
        if: ${{
          github.ref == 'refs/heads/master'
          && !contains(github.event.head_commit.message, '[DEV]')
          && !contains(github.event.head_commit.message, '[DOCS]')
          }}
        id: create_release
        uses: softprops/action-gh-release@v1
        with:
          name: ytdl-sub ${{ needs.version.outputs.pypi_version }}
          tag_name: ${{ needs.version.outputs.pypi_version }}
          body: ${{ github.event.head_commit.message }}
          draft: false
          prerelease: false
          files: |
            /opt/builds/ytdl-sub
            /opt/builds/ytdl-sub_aarch64
            /opt/builds/ytdl-sub.exe

  pypi-publish:
    name: pypi-publish
    needs:
      - version
    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - name: Write version to init file
        run: |
          echo '${{ needs.version.outputs.init_contents }}' > src/ytdl_sub/__init__.py
      - name: Build wheel
        run: |
          # Build wheel
          pip install -e .
          pip install build
          python3 -m build

      - name: Publish distribution 📦 to PyPI
        if: ${{
          github.ref == 'refs/heads/master'
          && !contains(github.event.head_commit.message, '[DEV]')
          && !contains(github.event.head_commit.message, '[DOCS]')
          }}
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
