name: 'Build Electron and NWJS packages'
on:
  schedule:
  # Nightly run at 03:39 UTC
    - cron: '39 03 * * *'
  workflow_dispatch:
    inputs:
      version:
        description: Specific version to build like v9.9.9 (if empty, builds version in package.json)
        required: false
        default: ''
      target:
        description: Do you wish to build release or nightly?
        required: false
        default: 'nightly'
env:
  INPUT_VERSION: ${{ github.event.inputs.version }}
  INPUT_TARGET: ${{ github.event.inputs.target }}
  CRON_LAUNCHED: ${{ github.event.schedule }}
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  CSC_LINK: ${{ secrets.CSC_LINK }}
  CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }}
  SSH_KEY: ${{ secrets.SSH_KEY }}
      
jobs:
  Release_Linux:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - name: install dependencies
      run: npm install
    - name: rewrite app version number
      run: |
        chmod +x ./scripts/rewrite_app_version_number.sh
        ./scripts/rewrite_app_version_number.sh
    - name: publish
      run: |
        npm run publish
        echo "$SSH_KEY" > ./scripts/ssh_key
        chmod 600 ./scripts/ssh_key
        chmod +x ./scripts/publish_linux_packages_to_kiwix.sh
        ./scripts/publish_linux_packages_to_kiwix.sh

  Release_Windows:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v3      
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - name: install dependencies
      run: npm install
    - name: rewrite app version number
      run: |
        $INPUT_VERSION = $Env:INPUT_VERSION
        $INPUT_TARGET = $Env:INPUT_TARGET
        $CRON_LAUNCHED = $Env:CRON_LAUNCHED
        ./scripts/Rewrite-AppVersion.ps1
    - name: run electron builder
      run: | 
        $GITHUB_TOKEN = $Env:GITHUB_TOKEN
        $INPUT_VERSION = $Env:INPUT_VERSION
        $INPUT_TARGET = $Env:INPUT_TARGET
        $CRON_LAUNCHED = $Env:CRON_LAUNCHED
        ./scripts/Rewrite-DraftReleaseTag.ps1
        npm run publish
        ./scripts/Rewrite-DraftReleaseTag.ps1
    - name: build portable Electron app
      run: |
        if (-not ($Env:CRON_LAUNCHED -or ($Env:INPUT_TARGET -eq 'nightly'))) {
          $GITHUB_TOKEN = $Env:GITHUB_TOKEN
          $INPUT_VERSION_E = $Env:INPUT_VERSION -replace '^(v[0-9.]+).*', '$1E'
          if ($Env:INPUT_VERSION -match '-Wiki[\w]+') {
            $INPUT_VERSION_E += $matches[0]
          }
          ./scripts/Create-DraftRelease -buildonly -tag_name $INPUT_VERSION_E -portableonly -wingetprompt N
        }
    - name: publish packages
      run: |
        $SSH_KEY = $Env:SSH_KEY
        echo "$SSH_KEY" > .\scripts\ssh_key
        $GITHUB_TOKEN = $Env:GITHUB_TOKEN
        $INPUT_VERSION = $Env:INPUT_VERSION
        $INPUT_TARGET = $Env:INPUT_TARGET
        $CRON_LAUNCHED = $Env:CRON_LAUNCHED
        ./scripts/Publish-ElectronPackages.ps1 -portableonly

  Release_NWJS:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v3       
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - name: select NWJS app
      run: |
        del package.json
        ren package.json.nwjs package.json
    - name: install dependencies
      run: npm install
    - name: rewrite app version number
      run: |
        $INPUT_VERSION = $Env:INPUT_VERSION
        $INPUT_TARGET = $Env:INPUT_TARGET
        $CRON_LAUNCHED = $Env:CRON_LAUNCHED
        ./scripts/Rewrite-AppVersion.ps1
    - name: build NWJS app
      run: ./scripts/Build-NWJS.ps1 -only32bit
    - name: publish
      run: |
        $SSH_KEY = $Env:SSH_KEY
        echo "$SSH_KEY" > .\scripts\ssh_key
        $GITHUB_TOKEN = $Env:GITHUB_TOKEN
        $INPUT_VERSION = $Env:INPUT_VERSION
        $INPUT_TARGET = $Env:INPUT_TARGET
        $CRON_LAUNCHED = $Env:CRON_LAUNCHED
        ./scripts/Publish-ElectronPackages.ps1