# Workflow for building Kiwix JS store packages for nightly and tagged releases.

name: Release

# Controls when the action will run.
on:
  release:
  # Triggers the workflow on publishing a release
    types:
      - published
  schedule:
  # Nightly run at 01:37 UTC (avoiding the usual midnight surge and rounded times)
    - cron: '37 1 * * *'
  # Allows us to run this workflow manually from the Actions tab
  workflow_dispatch:
    inputs:
      target:
        type: choice
        description: Do you wish to build release or nightly?
        required: false
        options:
          - release
          - nightly
        default: 'nightly'
      version:
        description: If building a release version, please set the version number here, like 9.9 (ignored if you selected nightly build)
        required: false

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-20.04

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Build the production version of the app
      - name: Build app for production
        shell: bash
        run: |
          npm install
          # Build the minified version of the app
          npm run build-min
          # Remove files we should not be packing
          rm -rf dist/www/js/*.map
      
      # Set up secret files from encrypted secrets
      - name: Set up secret files and copy needed scripts to dist
        env:
          SSH_KEY: ${{ secrets.SSH_KEY }}
          CHROME_EXTENSION_KEY: ${{ secrets.CHROME_EXTENSION_KEY }}
        shell: bash
        run: |
          mkdir -p ./dist/scripts
          echo "$SSH_KEY" > ./dist/scripts/ssh_key
          chmod 600 ./dist/scripts/ssh_key
          echo "$CHROME_EXTENSION_KEY" > ./dist/scripts/kiwix-html5.pem
          chmod 600 ./dist/scripts/kiwix-html5.pem
          cp ./scripts/create_all_packages.sh ./dist/scripts/create_all_packages.sh
          cp ./scripts/package_chrome_extension.sh ./dist/scripts/package_chrome_extension.sh
          cp ./scripts/package_firefox_extension.sh ./dist/scripts/package_firefox_extension.sh
          cp ./scripts/package_firefoxos_app.sh ./dist/scripts/package_firefoxos_app.sh
          cp ./scripts/package_ubuntu_touch_app.sh ./dist/scripts/package_ubuntu_touch_app.sh
          cp -rf ./ubuntu_touch dist/ubuntu_touch

      # Set up the environment
      - name: Setup environment
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get --yes install ./scripts/ubuntu_touch_packages/*.deb

      # Runs the build scripts
      - name: Run the build scripts to make extensions and packages
        env:
          MOZILLA_API_SECRET: ${{ secrets.MOZILLA_API_SECRET }}
          MOZILLA_API_KEY: ${{ secrets.MOZILLA_API_KEY }}
          TAG_NAME: ${{ github.event.release.tag_name }}
          CRON_LAUNCHED: ${{ github.event.schedule }}
          INPUT_TARGET: ${{ inputs.target }}
          RELEASE_VERSION: ${{ inputs.version }}
        shell: bash
        # Switch -t indicates a tag release (public release); add -d for dry run (for testing)
        # BEFORE the -v switch (because $TAG_NAME is empty for non-public builds)
        run: |
          # If user dispateched with "nightly", simulate a CRON run
          if [ "$INPUT_TARGET" = "nightly" ]; then
            CRON_LAUNCHED="nightly"
          # Else if use entered a release version, override any tag version
          elif [ -n "$RELEASE_VERSION" ]; then
            TAG_NAME="$RELEASE_VERSION"
          fi
          # Run xvfb to make Chrome/Chromium believe it has a display. Else it fails signing the package
          Xvfb -ac :99 -screen 0 1280x1024x16 &
          export DISPLAY=:99
          # Switch to the distribution directory
          cd ./dist
          if [ -z "$TAG_NAME" ]; then
            ./scripts/create_all_packages.sh
          else
            ./scripts/create_all_packages.sh -t -v "$TAG_NAME"
          fi
