name: Virtual Display Driver All Signpath Request
on:
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest
    strategy:
      matrix:
        configuration: [Release]
        platform: [x64, ARM64]
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup MSBuild
        uses: microsoft/setup-msbuild@v1

      - name: Check Chocolatey installation
        run: choco --version

      - name: Install Visual Studio 2022 dependencies
        run: |
          choco install visualstudio2022-workload-manageddesktop -y
          if ($LASTEXITCODE -ne 0) { exit 1 }
          
          choco install visualstudio2022-workload-nativedesktop -y
          if ($LASTEXITCODE -ne 0) { exit 1 }
          
          choco install visualstudio2022-workload-vctools -y
          if ($LASTEXITCODE -ne 0) { exit 1 }
          
          choco install windowsdriverkit11 -y
          if ($LASTEXITCODE -ne 0) { exit 1 }

      - name: Build the driver
        run: |
          msbuild "Virtual Display Driver (HDR)/MTTVDD.sln" /p:Configuration=${{ matrix.configuration }} /p:Platform=${{ matrix.platform }}

      - name: List build directory
        run: dir "Virtual Display Driver (HDR)\${{ matrix.platform }}\${{ matrix.configuration }}\MttVDD"

      - name: Upload built driver
        id: upload_artifact
        uses: actions/upload-artifact@v4
        with:
          name: Built-Driver-${{ matrix.configuration }}-${{ matrix.platform }}
          path: |
            Virtual Display Driver (HDR)\${{ matrix.platform }}\${{ matrix.configuration }}\MttVDD\MttVDD.dll
            Virtual Display Driver (HDR)\${{ matrix.platform }}\${{ matrix.configuration }}\MttVDD\MttVDD.inf
            Virtual Display Driver (HDR)\${{ matrix.platform }}\${{ matrix.configuration }}\MttVDD\mttvdd.cat
            Virtual Display Driver (HDR)\${{ matrix.platform }}\${{ matrix.configuration }}\MttVDD\vdd_settings.xml

      - name: Generate release tag
        id: generate_tag
        run: |
          $releaseTag = (Get-Date).ToString('yy.MM.dd')
          echo "RELEASE_TAG=$releaseTag" >> $env:GITHUB_ENV

      - name: Show generated release tag
        run: |
          echo "Generated Release Tag: ${{ env.RELEASE_TAG }}"
            
      - name: Verify Built Artifacts
        run: dir 'Virtual Display Driver (HDR)\${{ matrix.platform }}\${{ matrix.configuration }}\MttVDD'


      - name: Submit signing request to SignPath
        id: signpath_request
        uses: signpath/github-action-submit-signing-request@v1
        with:
          api-token: '${{ secrets.SIGNPATH_API_TOKEN }}'
          organization-id: '${{ vars.SIGNPATH_ORG_ID }}'
          project-slug: '${{ vars.SIGNPATH_PROJECT_SLUG }}'
          signing-policy-slug: '${{ vars.SIGNPATH_POLICY_SLUG }}'
          github-artifact-id: '${{ steps.upload_artifact.outputs.artifact-id }}'
          wait-for-completion: true
          output-artifact-directory: '${{ vars.SIGNPATH_OUTPUT_DIR }}'
          parameters: |
            Version: ${{ toJSON(matrix.configuration) }}
            Release_Tag: "${{ env.RELEASE_TAG }}"

      - name: Verify Signed Artifacts
        run: dir '${{ vars.SIGNPATH_OUTPUT_DIR }}'

      - name: Upload signed artifacts
        id: upload_signed_artifacts
        uses: actions/upload-artifact@v4
        with:
          name: Signed-Driver-${{ matrix.configuration }}-${{ matrix.platform }}
          path: |
            ${{ vars.SIGNPATH_OUTPUT_DIR }}\MttVDD.dll
            ${{ vars.SIGNPATH_OUTPUT_DIR }}\MttVDD.inf
            ${{ vars.SIGNPATH_OUTPUT_DIR }}\mttvdd.cat
            ${{ vars.SIGNPATH_OUTPUT_DIR }}\vdd_settings.xml
            
      - name: Prepare Setup Repository
        run: |
          git clone https://${{ secrets.READ_REPO }}@github.com/VirtualDisplay/Virtual-Driver-Installer.git inno-setup 

      - name: Prepare Setup 
        run: |
          copy "${{ vars.SIGNPATH_OUTPUT_DIR }}\*" inno-setup\input\
          $platform = "${{ matrix.platform }}"
          (Get-Content "inno-setup\Setup.iss") | 
            ForEach-Object { $_ -replace '1.0.0', '${{ env.RELEASE_TAG }}' } | 
            Set-Content "inno-setup\Setup.iss"
            
          if ($platform -eq 'ARM64') {
            (Get-Content "inno-setup\Setup.iss") | 
              ForEach-Object { $_ -replace 'x64compatible', 'arm64' } | 
              Set-Content "inno-setup\Setup.iss"
                
            (Get-Content "inno-setup\Setup.iss") | 
              ForEach-Object { $_ -replace '-x64', '-arm64' } | 
              Set-Content "inno-setup\Setup.iss"

            if (Test-Path "inno-setup\input\Companion\VDDSysTray.exe") {Remove-Item "inno-setup\input\Companion\VDDSysTray.exe"}
            copy "inno-setup\input\Companion\arm64\VDDSysTray.exe" "inno-setup\input\Companion\"
          }

      - name: Compile Installer
        uses: Minionguyjpro/Inno-Setup-Action@v1.2.2
        with:
          path: inno-setup\Setup.iss
          options: /O+

      - name: Upload Installer as artifact
        uses: actions/upload-artifact@v4
        with:
          name: Installer-${{ matrix.configuration }}-${{ matrix.platform }}
          path: inno-setup\output\*.exe

      - name: Submit Installer Signing Request to SignPath
        id: signpath_installer_request
        uses: signpath/github-action-submit-signing-request@v1
        with:
          api-token: '${{ secrets.SIGNPATH_API_TOKEN }}'
          organization-id: '${{ vars.SIGNPATH_ORG_ID }}'
          project-slug: '${{ vars.SIGNPATH_PROJECT_SLUG }}'
          signing-policy-slug: '${{ vars.SIGNPATH_POLICY_SLUG_INSTALLER }}'
          github-artifact-id: '${{ steps.upload_installer.outputs.artifact-id }}'
          wait-for-completion: true
          output-artifact-directory: '${{ vars.SIGNPATH_INSTALLER_OUTPUT_DIR }}'

      - name: Upload signed installer
        uses: actions/upload-artifact@v4
        with:
          name: Signed-Installer-${{ matrix.configuration }}-${{ matrix.platform }}
          path: '${{ vars.SIGNPATH_INSTALLER_OUTPUT_DIR }}/*'

  celebrate:
    runs-on: ubuntu-latest
    steps:
      - name: Celebrate
        run: |
          echo "Bud is a god, also hi if you're looking at this!"
