name: Build

on:
  workflow_call:
    inputs:
      build-commit:
        type: string
        required: true
      push-version-commit:
        type: boolean
        default: true
  workflow_dispatch:
    inputs:
      build-commit:
        description: 'SHA of commit being built'
        type: string
        required: false
      push-version-commit:
        description: 'Update master if set'
        type: boolean
        default: true

jobs:
  build_unix:
    runs-on: ubuntu-latest
    outputs:
      version_suffix: ${{ steps.version_suffix.outputs.version_suffix }}
      ytdl_version: ${{ steps.bump_version.outputs.ytdl_version }}
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      sha256_bin: ${{ steps.sha256_bin.outputs.sha256_bin }}
      sha512_bin: ${{ steps.sha512_bin.outputs.sha512_bin }}
      sha256_tar: ${{ steps.sha256_tar.outputs.sha256_tar }}
      sha512_tar: ${{ steps.sha512_tar.outputs.sha512_tar }}

    steps:
    - name: Check out
      uses: actions/checkout@v3
      with:
        ref: refs/heads/master
        fetch-depth: 0
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
          python-version: '3.9'
    - name: Install packages
      run: sudo apt-get -y install zip pandoc man
    - name: Bump version
      id: bump_version
      run: |
        ytdl_version=$(python devscripts/update-version.py)
        echo "ytdl_version=$ytdl_version" >> "$GITHUB_OUTPUT"
        sha=${{ inputs.build-commit }}
        sed -i -rn -e '/^ *RELEASE_GIT_HEAD *=/z;$s/$/&'"\nRELEASE_GIT_HEAD = '${sha}'/;p" youtube_dl/version.py
        x=${sha#?????????}; sha=${sha%$x}
        echo "Version = $ytdl_version${sha+ [}${sha}${sha+]}"
        make issuetemplates
    - name: Push to release
      id: push_release
      run: |
        git config --global user.name github-actions
        git config --global user.email github-actions@example.com
        git add -u
        git commit -m "Release (nightly) ${{ steps.update_version.outputs.version }}" \
          -m "Created by: ${{ github.event.sender.login }}" -m ":ci skip all"
        git push origin --force master:release
        echo "head_sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
    - name: Update master
      if: ${{ inputs.push-version-commit }}
      run: git push origin master
    - name: Get Changelog      
      id: get_changelog
      run: |
        changelog=$(cat Changelog.md | grep -oPz '(?s)(?<=### ${{ steps.bump_version.outputs.ytdl_version }}\n{2}).+?(?=\n{2,3}###)') || true
        echo "changelog<<EOF" >> $GITHUB_ENV
        echo "$changelog" >> $GITHUB_ENV
        echo "EOF" >> $GITHUB_ENV

    - name: Run Make
      run: make all tar
    - name: Get SHA2-256SUMS for youtube-dl
      id: sha256_bin
      run: echo "sha256_bin=$(sha256sum youtube-dl | awk '{print $1}')" >> "$GITHUB_OUTPUT"
    - name: Get SHA2-256SUMS for youtube-dl.tar.gz
      id: sha256_tar
      run: echo "sha256_tar=$(sha256sum youtube-dl.tar.gz | awk '{print $1}')" >> "$GITHUB_OUTPUT"
    - name: Get SHA2-512SUMS for youtube-dl
      id: sha512_bin
      run: echo "sha512_bin=$(sha512sum youtube-dl | awk '{print $1}')" >> "$GITHUB_OUTPUT"
    - name: Get SHA2-512SUMS for youtube-dl.tar.gz
      id: sha512_tar
      run: echo "sha512_tar=$(sha512sum youtube-dl.tar.gz | awk '{print $1}')" >> "$GITHUB_OUTPUT"

    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ steps.bump_version.outputs.ytdl_version }}
        release_name: youtube-dl ${{ steps.bump_version.outputs.ytdl_version }}
        commitish: ${{ steps.push_release.outputs.head_sha }}
        body: This is a nightly build of youtube-dl.
        draft: false
        prerelease: false
    - name: Upload youtube-dl Unix binary
      id: upload-release-asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./youtube-dl
        asset_name: youtube-dl
        asset_content_type: application/octet-stream
    - name: Upload Source tar
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./youtube-dl.tar.gz
        asset_name: youtube-dl-${{ steps.bump_version.outputs.ytdl_version }}.tar.gz
        asset_content_type: application/gzip

  build_windows:
    runs-on: windows-2022
    needs: build_unix
    env:
      PYCRYPTO: pycrypto-2.6.1-cp34-none-win32
    outputs:
      sha256_win: ${{ steps.sha256_win.outputs.sha256_win }}
      sha512_win: ${{ steps.sha512_win.outputs.sha512_win }}

    steps:
    - uses: actions/checkout@v3
      with:
        ref: refs/heads/release
    # reason to choose 3.4: https://media.discordapp.net/attachments/807245652072857613/942409077701619742/unknown.png
    - name: Set up Python 3.4
      uses: actions/setup-python@v4
      with:
        python-version: '3.4'
        architecture: 'x86'
    - name: Install packages
      # https://setuptools.pypa.io/en/latest/history.html#v44-0-0
      # https://pypi.org/project/py2exe/0.9.2.2/
      # https://pip.pypa.io/en/stable/news/#v19-2
      # https://wheel.readthedocs.io/en/stable/news.html
      run: python -m pip install --upgrade "pip<19.2" "setuptools<44" "wheel<0.34.0" py2exe==0.9.2.2
    # - name: Run PyInstaller Script
    #   run: python -m PyInstaller --onefile --console --distpath dist/ -n youtube-dl youtube_dl\__main__.py
    - name: Cache PyCrypto
      id: cache_pc
      uses: actions/cache@v3
      with:
        key: ${{ env.PYCRYPTO }}
        path: ./${{ env.PYCRYPTO }}
    - name: Acquire PyCrypto
      if: ${{ ! steps.cache_pc.outputs.cache-hit }}
      shell: bash
      run: |
        mkdir -p ./${PYCRYPTO}
        cd ${PYCRYPTO}
        curl -L -O "https://web.archive.org/web/20200627032153/http://www.voidspace.org.uk/python/pycrypto-2.6.1/pycrypto-2.6.1-cp34-none-win32.whl"
    - name: Install PyCrypto
      shell: bash
      run: |
        python -m pip install ./${PYCRYPTO}/${PYCRYPTO}.whl
    - name: Build EXE file
      run: python setup.py py2exe
    - name: Upload youtube-dl.exe Windows binary
      id: upload-release-windows
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.build_unix.outputs.upload_url }}
        asset_path: ./youtube-dl.exe
        asset_name: youtube-dl.exe
        asset_content_type: application/vnd.microsoft.portable-executable
    - name: Get SHA2-256SUMS for youtube-dl.exe
      id: sha256_win
      run: echo "sha256_win=$((Get-FileHash youtube-dl.exe -Algorithm SHA256).Hash.ToLower())" >> "$env:GITHUB_OUTPUT"
    - name: Get SHA2-512SUMS for youtube-dl.exe
      id: sha512_win
      run: echo "sha512_win=$((Get-FileHash youtube-dl.exe -Algorithm SHA512).Hash.ToLower())" >> "$env:GITHUB_OUTPUT"

  finish:
    runs-on: ubuntu-latest
    needs: [build_unix, build_windows]
    env:
      YTDL_VERSION: ${{ needs.build_unix.outputs.ytdl_version }}

    steps:
    - name: Make SHA2-256SUMS file
      env:
        SHA256_BIN: ${{ needs.build_unix.outputs.sha256_bin }}
        SHA256_TAR: ${{ needs.build_unix.outputs.sha256_tar }}
        SHA256_WIN: ${{ needs.build_windows.outputs.sha256_win }}
      run: |
        echo "${{ env.SHA256_BIN }}  youtube-dl" >> SHA2-256SUMS
        echo "${{ env.SHA256_TAR }}  youtube-dl-${YTDL_VERSION}.tar.gz" >> SHA2-256SUMS
        echo "${{ env.SHA256_WIN }}  youtube-dl.exe" >> SHA2-256SUMS
    - name: Upload 256SUMS file
      id: upload-sums
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.build_unix.outputs.upload_url }}
        asset_path: ./SHA2-256SUMS
        asset_name: SHA2-256SUMS
        asset_content_type: text/plain
    - name: Make SHA2-512SUMS file
      env:
        SHA512_BIN: ${{ needs.build_unix.outputs.sha512_bin }}
        SHA512_TAR: ${{ needs.build_unix.outputs.sha512_tar }}
        SHA512_WIN: ${{ needs.build_windows.outputs.sha512_win }}
      run: |
        echo "${{ env.SHA512_BIN }}  youtube-dl" >> SHA2-512SUMS
        echo "${{ env.SHA512_TAR }}  youtube-dl-${YTDL_VERSION}.tar.gz" >> SHA2-512SUMS
        echo "${{ env.SHA512_WIN }}  youtube-dl.exe" >> SHA2-512SUMS
    - name: Upload 512SUMS file
      id: upload-512sums
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.build_unix.outputs.upload_url }}
        asset_path: ./SHA2-512SUMS
        asset_name: SHA2-512SUMS
        asset_content_type: text/plain
