name: Publish Installers

on:
  workflow_dispatch: ~
  push:
    branches: [master, beta, nightly]
    tags: [v*]

env:
  PYTHON_VERSION: '3.11'

jobs:
  build-installer:
    name: Build ${{ matrix.os_upper }} Installer
    runs-on: ${{ matrix.os }}-${{ matrix.os_version }}
    if: ${{ !contains(github.event.head_commit.message, '[skip ci]') }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: 'windows'
            os_upper: 'Windows'
            os_version: 'latest'
            arch: 'x64'
            ext: 'exe'
          - os: 'macos'
            os_upper: 'MacOS'
            os_version: '14'
            arch: 'universal'
            ext: 'pkg'

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set Release Version
        id: get_version
        shell: bash
        run: |
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
            VERSION_NSIS=${GITHUB_REF#refs/tags/v}.1
            echo "VERSION_NSIS=${VERSION_NSIS/%-beta.1/.0}" >> $GITHUB_OUTPUT
            echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
            echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
          else
            echo "VERSION=0.0.0" >> $GITHUB_ENV
            echo "VERSION_NSIS=0.0.0.0" >> $GITHUB_OUTPUT
            echo "VERSION=0.0.0" >> $GITHUB_OUTPUT
            echo "RELEASE_VERSION=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
          fi
          if [[ $GITHUB_REF == refs/tags/*-beta ]]; then
            echo "beta" > branch.txt
          elif [[ $GITHUB_REF == refs/tags/* ]]; then
            echo "master" > branch.txt
          else
            echo ${GITHUB_REF#refs/heads/} > branch.txt
          fi
          echo $GITHUB_SHA > version.txt

      - name: Set Up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          cache: pip
          cache-dependency-path: '**/requirements*.txt'

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r package/requirements-package.txt --no-binary cffi

      - name: Build Package
        run: |
          pyinstaller -y ./package/Tautulli-${{ matrix.os }}.spec

      - name: Create Windows Installer
        uses: joncloud/makensis-action@v4.1
        if: matrix.os == 'windows'
        with:
          script-file: ./package/Tautulli.nsi
          arguments: >
            /DVERSION=${{ steps.get_version.outputs.VERSION_NSIS }}
            /DINSTALLER_NAME=..\Tautulli-${{ matrix.os }}-${{ steps.get_version.outputs.RELEASE_VERSION }}-${{ matrix.arch }}.${{ matrix.ext }}
          additional-plugin-paths: package/nsis-plugins

      - name: Create MacOS Installer
        if: matrix.os == 'macos'
        run: |
          sudo pkgbuild \
            --install-location /Applications \
            --version ${{ steps.get_version.outputs.VERSION }} \
            --component ./dist/Tautulli.app \
            --scripts ./package/macos-scripts \
            Tautulli-${{ matrix.os }}-${{ steps.get_version.outputs.RELEASE_VERSION }}-${{ matrix.arch }}.${{ matrix.ext }}

      - name: Upload Installer
        uses: actions/upload-artifact@v4
        with:
          name: Tautulli-${{ matrix.os }}-installer
          path: Tautulli-${{ matrix.os }}-${{ steps.get_version.outputs.RELEASE_VERSION }}-${{ matrix.arch }}.${{ matrix.ext }}

  virus-total:
    name: VirusTotal Scan
    needs: build-installer
    if: needs.build-installer.result == 'success' && !contains(github.event.head_commit.message, '[skip ci]')
    runs-on: ubuntu-latest
    steps:
      - name: Download Installers
        if: needs.build-installer.result == 'success'
        uses: actions/download-artifact@v4

      - name: Upload to VirusTotal
        uses: crazy-max/ghaction-virustotal@v4
        with:
          vt_api_key: ${{ secrets.VT_API_KEY }}
          files: |
            Tautulli-windows-installer/Tautulli-windows-*-x64.exe
            Tautulli-macos-installer/Tautulli-macos-*-universal.pkg

  release:
    name: Release Installers
    needs: build-installer
    if: always() && startsWith(github.ref, 'refs/tags/') && !contains(github.event.head_commit.message, '[skip ci]')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set Release Version
        id: get_version
        run: |
          echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Download Installers
        if: needs.build-installer.result == 'success'
        uses: actions/download-artifact@v4

      - name: Get Changelog
        id: get_changelog
        run: |
          CHANGELOG="$( sed -n '/^## /{p; :loop n; p; /^## /q; b loop}' CHANGELOG.md \
            | sed '$d' | sed '$d' | sed '$d' )"
          EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
          echo "CHANGELOG<<$EOF" >> $GITHUB_OUTPUT
          echo "$CHANGELOG" >> $GITHUB_OUTPUT
          echo "$EOF" >> $GITHUB_OUTPUT

      - name: Create Release
        uses: softprops/action-gh-release@v2
        id: create_release
        env:
          GITHUB_TOKEN: ${{ secrets.GHACTIONS_TOKEN }}
        with:
          tag_name: ${{ steps.get_version.outputs.RELEASE_VERSION }}
          name: Tautulli ${{ steps.get_version.outputs.RELEASE_VERSION }}
          body: |
            ## Changelog
            
            ##${{ steps.get_changelog.outputs.CHANGELOG }}
          prerelease: ${{ endsWith(steps.get_version.outputs.RELEASE_VERSION, '-beta') }}
          files: |
            Tautulli-windows-installer/Tautulli-windows-${{ steps.get_version.outputs.RELEASE_VERSION }}-x64.exe
            Tautulli-macos-installer/Tautulli-macos-${{ steps.get_version.outputs.RELEASE_VERSION }}-universal.pkg

  discord:
    name: Discord Notification
    needs: [build-installer, release]
    if: always() && !contains(github.event.head_commit.message, '[skip ci]')
    runs-on: ubuntu-latest
    steps:
      - name: Post Status to Discord
        uses: sarisia/actions-status-discord@v1
        with:
          webhook: ${{ secrets.DISCORD_WEBHOOK }}
          status: ${{ needs.build-installer.result == 'success' && 'success' || contains(needs.*.result, 'failure') && 'failure' || 'cancelled' }}
          title: ${{ github.workflow }}
          nofail: true
