name: MSBuild

on:
  push:
    branches:
      - master
    paths-ignore:
      - '**/*.md'

env:
  SOLUTION_FILE_PATH: CP_Main_10.sln

jobs:
  build:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4
      
      - name: Generate build number
        id: buildnumber
        uses: onyxmueller/build-tag-number@v1
        with:
          token: ${{secrets.GITHUB_TOKEN}} 
          
      - name: Set the value
        run: |
            echo "VERSION_FILENAME=3_25_${env:BUILD_NUMBER}_0" | Out-File -FilePath $env:GITHUB_ENV -Append
            echo "VERSION_DOT_FILENAME=3.25.${env:BUILD_NUMBER}.0" | Out-File -FilePath $env:GITHUB_ENV -Append
            
      - name: Add MSBuild to PATH
        uses: microsoft/setup-msbuild@v2

      - name: Restore NuGet packages
        working-directory: ${{env.GITHUB_WORKSPACE}}
        run: nuget restore ${{env.SOLUTION_FILE_PATH}}
        
      - name: Build
        run: msbuild CP_Main_10.sln /p:Configuration=Release /p:Platform=x64
        
      - name: Set Versions
        run: |
            DittoSetup\rcedit-x64 Release64\Ditto.exe --set-file-version ${{env.VERSION_DOT_FILENAME}} --set-product-version ${{env.VERSION_DOT_FILENAME}}

      - name: upload-unsigned-exe
        id: upload-unsigned-exe
        uses: actions/upload-artifact@v4
        with: 
          name: unsigned-exe
          retention-days: 1
          path: |
            Release64\Ditto.exe
            Release64\ICU_Loader.dll
            Release64\Addins\DittoUtil.dll
            
      - id: sign-exe
        uses: signpath/github-action-submit-signing-request@v1.1
        with:
          api-token: '${{ secrets.SIGNPATH_API_TOKEN }}'
          organization-id: 'fcf33ea4-4343-46b3-be50-f8fe64e1a900'
          project-slug: 'Ditto'
          artifact-configuration-slug: 'exe'
          signing-policy-slug: 'release-signing'
          github-artifact-id: '${{ steps.upload-unsigned-exe.outputs.artifact-id }}'
          wait-for-completion: true
          output-artifact-directory: '/Release64'      
                   
      - name: Installer
        uses: Minionguyjpro/Inno-Setup-Action@v1.2.7
        with:
          path: DittoSetup\DittoSetup_10.iss
          
      - name: upload-unsigned-artifact
        id: upload-unsigned-artifact
        uses: actions/upload-artifact@v4
        with: 
          name: unsigned-intaller
          retention-days: 1
          path: DittoSetup\output\DittoSetup_${{env.VERSION_FILENAME}}.exe
          
      - id: sing-installer
        uses: signpath/github-action-submit-signing-request@v1.1
        with:
          api-token: '${{ secrets.SIGNPATH_API_TOKEN }}'
          organization-id: 'fcf33ea4-4343-46b3-be50-f8fe64e1a900'
          project-slug: 'Ditto'
          artifact-configuration-slug: 'Installer'
          signing-policy-slug: 'release-signing'
          github-artifact-id: '${{ steps.upload-unsigned-artifact.outputs.artifact-id }}'
          wait-for-completion: true
          output-artifact-directory: '/DittoSetup/output'          
          
      - name: Build Portable
        run: |
            DittoSetup\BuildPortableZIP.bat "DittoPortable_${{env.VERSION_FILENAME}}" bit64
            
      - name: Zip Debug Files
        run: |
            7z a DittoSetup\output\DittoDebug_${{env.VERSION_FILENAME}}.7z *.pdb -r
            7z a DittoSetup\output\DittoDebug_${{env.VERSION_FILENAME}}.7z *.map -r
            
      - name: Choco - Ditto
        run: |
            (Get-Content 'DittoSetup\Chocolatey\ditto.nuspec' -Raw).Replace("%version%", "${{env.VERSION_DOT_FILENAME}}") | Out-File 'DittoSetup\Chocolatey\ditto.nuspec'
            copy DittoSetup\Output\DittoSetup_${{env.VERSION_FILENAME}}.exe DittoSetup\Chocolatey\tools\DittoSetup_${{env.VERSION_FILENAME}}.exe
            cd DittoSetup\Chocolatey
            choco pack     
            choco push --source https://push.chocolatey.org/ --key ${{ secrets.CHOCO_API_KEY}}
            cd ..
            cd ..
            
      - name: Choco - Ditto.Install
        run: |
            (Get-Content 'DittoSetup\Chocolatey.install\ditto.nuspec' -Raw).Replace("%version%", "${{env.VERSION_DOT_FILENAME}}") | Out-File 'DittoSetup\Chocolatey.install\ditto.nuspec'
            copy DittoSetup\output\DittoSetup_${{env.VERSION_FILENAME}}.exe DittoSetup\Chocolatey.install\tools\DittoSetup_${{env.VERSION_FILENAME}}.exe
            cd DittoSetup\Chocolatey.install
            choco pack     
            choco push --source https://push.chocolatey.org/ --key ${{ secrets.CHOCO_API_KEY}}
            cd ..
            cd ..
    
      - name: Choco - Ditto.Portable
        run: |
            (Get-Content 'DittoSetup\Chocolatey.portable\ditto.nuspec' -Raw).Replace("%version%", "${{env.VERSION_DOT_FILENAME}}") | Out-File 'DittoSetup\Chocolatey.portable\ditto.nuspec'
            copy DittoSetup\output\DittoPortable_${{env.VERSION_FILENAME}}.zip DittoSetup\Chocolatey.portable\tools\DittoPortable_${{env.VERSION_FILENAME}}.zip
            cd DittoSetup\Chocolatey.portable
            choco pack     
            choco push --source https://push.chocolatey.org/ --key ${{ secrets.CHOCO_API_KEY}}
            cd ..
            cd ..
            
      - name: Build appx
        run: |
            7z x DittoSetup\output\DittoPortable_${{env.VERSION_FILENAME}}.zip -oDittoSetup\appx -r
            (Get-Content 'DittoSetup\appx\appxmanifest.xml' -Raw).Replace("%version%", "${{env.VERSION_DOT_FILENAME}}") | Out-File 'DittoSetup\appx\appxmanifest.xml'  
            cd DittoSetup\appx

            $env:Path += ";C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x86"
        
            makeappx.exe pack /f files.ini /p Ditto_${{env.VERSION_FILENAME}}.appx
            signtool.exe sign -f my.pfx -fd SHA256 -v Ditto_${{env.VERSION_FILENAME}}.appx
            cd ..
            cd ..
            copy DittoSetup\appx\Ditto_${{env.VERSION_FILENAME}}.appx DittoSetup\Output\Ditto_${{env.VERSION_FILENAME}}.appx
    
      - name: Publish to GitHub
        env:
          token: ${{secrets.GITHUB_TOKEN}}
          uploadPath: DittoSetup\output\
          tag: ${{env.VERSION_FILENAME}}
          previous_tag: 3.25.73.0
        run: |
             npm install ./DittoSetup/GitHubRelease
             node ./DittoSetup/GitHubRelease/index.js

