# docs https://github.com/marketplace/actions/create-release
# docs https://github.com/ncipollo/release-action

name: release

on:
  push:
    tags:
      - 'v*'

jobs:
  linux:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      # electron build
      - uses: actions/checkout@v2
        with:
          # needed for git commit history changelog
          fetch-depth: 0
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - run: yarn install --frozen-lockfile
      # make sure the ipfs executable is executable
      - run: node electron/downloadIpfs && sudo chmod +x bin/linux/ipfs
      - run: CI='' yarn build
      - run: yarn electron:build:linux
      - run: ls dist

      # publish version release
      - run: node scripts/release-body > release-body.txt
      - uses: ncipollo/release-action@v1
        with:
          artifacts: 'dist/plebchan*.AppImage,dist/plebchan-html*.zip'
          token: ${{ secrets.GITHUB_TOKEN }}
          replacesArtifacts: true
          bodyFile: "release-body.txt"
          allowUpdates: true

  mac:
    runs-on: macOS-latest
    permissions:
      contents: write
    steps:
      # electron build
      - uses: actions/checkout@v2
        with:
          # needed for git commit history changelog
          fetch-depth: 0
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - run: yarn install --frozen-lockfile
      # make sure the ipfs executable is executable
      - run: node electron/downloadIpfs && sudo chmod +x bin/mac/ipfs
      - run: CI='' yarn build
      - run: yarn electron:build:mac
      - run: ls dist

      # publish version release
      - run: node scripts/release-body > release-body.txt
      - uses: ncipollo/release-action@v1
        with:
          artifacts: 'dist/plebchan*.dmg'
          token: ${{ secrets.GITHUB_TOKEN }}
          replacesArtifacts: true
          bodyFile: "release-body.txt"
          allowUpdates: true

  windows:
    runs-on: windows-latest
    permissions:
      contents: write
    steps:
      # electron build
      - uses: actions/checkout@v2
        with:
          # needed for git commit history changelog
          fetch-depth: 0
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - run: yarn install --frozen-lockfile
      - run: yarn build
      - run: yarn electron:build:windows
      - run: dir dist

      # publish version release
      - run: node scripts/release-body > release-body.txt
      - uses: ncipollo/release-action@v1
        with:
          artifacts: 'dist/plebchan*.exe'
          token: ${{ secrets.GITHUB_TOKEN }}
          replacesArtifacts: true
          bodyFile: "release-body.txt"
          allowUpdates: true

  android:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v2
        with:
          # needed to use 'git tag' and get all tags and for git commit history changelog
          fetch-depth: 0
      - uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '11'
      - uses: gradle/gradle-build-action@v2
        with:
          gradle-version: 7.6
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - run: sudo apt install -y apksigner zipalign

      # build react app
      - run: yarn install --frozen-lockfile
      - run: CI='' yarn build
      # set android versionCode and versionName
      - run: sed -i "s/versionCode 1/versionCode $(git tag | wc -l)/" ./android/app/build.gradle
      - run: sed -i "s/versionName \"1.0\"/versionName \"$(node -e "console.log(require('./package.json').version)")\"/" ./android/app/build.gradle
      - run: cat ./android/app/build.gradle
      # build apk
      - run: npx cap update
      - run: npx cap copy
      - run: cd android && gradle bundle
      - run: cd android && ./gradlew assembleRelease
      # optimize apk
      - run: cd android/app/build/outputs/apk/release && zipalign 4 app-release-unsigned.apk app-release-unsigned-zip.apk
      # sign apk
      # to create keystore: keytool -genkey -v -keystore plebbit.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias release
      - run: cd android/app/build/outputs/apk/release && apksigner sign --ks ../../../../../plebbit.keystore --ks-pass pass:${{ secrets.PLEBBIT_REACT_KEYSTORE_PASSWORD }} --ks-key-alias release --out app-release-signed.apk app-release-unsigned-zip.apk
      # move apk to dist folder
      - run: mkdir -p dist && mv android/app/build/outputs/apk/release/app-release-signed.apk dist/plebchan-$(node -e "console.log(require('./package.json').version)").apk
      - run: ls dist

      # publish version release
      - run: node scripts/release-body > release-body.txt
      - uses: ncipollo/release-action@v1
        with:
          artifacts: 'dist/plebchan*.apk'
          token: ${{ secrets.GITHUB_TOKEN }}
          replacesArtifacts: true
          bodyFile: "release-body.txt"
          allowUpdates: true
