name: Compile and release production build

on:
  workflow_dispatch:
    branches:
      - master

jobs:
  build-android:
    name: Build Android .apk and .aab
    runs-on: ubuntu-latest
    env:
      ANDROID_AAB_RELEASE_PATH: build/app/outputs/bundle/release
      ANDROID_APK_RELEASE_PATH: build/app/outputs/apk/release
    outputs:
      VERSION_NAME: ${{ steps.save_version.outputs.version_name }}
      VERSION_NUMBER: ${{ steps.save_version.outputs.version_number }}
    steps:
      - uses: actions/checkout@v3
      - name: Decode android/app/keystore.jks
        run: echo "${{ secrets.KEYSTORE_JKS }}" | base64 --decode > android/app/keystore.jks
      - name: Decode android/key.properties
        run: echo "${{ secrets.KEY_PROPERTIES }}" | base64 --decode > android/key.properties
      - name: Decode .env
        run: echo "${{ secrets.ENV }}" | base64 --decode > .env
      - name: Read pubspec.yaml
        uses: jbutcher5/read-yaml@1.6
        id: read_pubspec
        with:
          file: './pubspec.yaml'          # File to read from
          key-path: '["version"]'
      - name: Save version on env variable
        id: save_version
        run: |
          version=${{ steps.read_pubspec.outputs.data }}
          IFS='+' 
          read -r -a split <<< "$version"
          echo "VERSION_NAME=$(echo ${split[0]})" >> $GITHUB_ENV
          echo "version_name=${split[0]}" >> $GITHUB_OUTPUT
          echo "version_number=${split[1]}" >> $GITHUB_OUTPUT
      - name: Update KeyStore password in gradle properties
        run: sed -i 's/#{KEYSTORE_PASS}#/${{ secrets.KEYSTORE_PASS }}/g' android/key.properties
      - name: Update KeyStore key password in gradle properties
        run: sed -i 's/#{KEYSTORE_KEY_PASS}#/${{ secrets.KEYSTORE_KEY_PASS }}/g' android/key.properties
      - uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '18.x'
      - uses: subosito/flutter-action@v2
        with:
          channel: "stable"
      - run: flutter clean
      - run: flutter pub get
      - run: flutter build apk --release
      - run: flutter build appbundle --release
      - name: Rename apk 
        run: mv $ANDROID_APK_RELEASE_PATH/app-release.apk $ANDROID_APK_RELEASE_PATH/AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.apk
      - name: Rename aab 
        run: mv $ANDROID_AAB_RELEASE_PATH/app-release.aab $ANDROID_AAB_RELEASE_PATH/AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.aab
      - name: Copy apk to project root
        run: cp $ANDROID_APK_RELEASE_PATH/AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.apk AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.apk
      - name: Copy aab to project root
        run: cp $ANDROID_AAB_RELEASE_PATH/AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.aab AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.aab
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: android
          path: |
            AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.aab
            AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.apk
  build-macos:
    name: Build macOS .dmg
    runs-on: macos-latest
    env:
      MACOS_APP_RELEASE_PATH: build/macos/Build/Products/Release
    outputs:
      VERSION_NAME: ${{ steps.save_version.outputs.version_name }}
      VERSION_NUMBER: ${{ steps.save_version.outputs.version_number }}
    steps:
      - uses: actions/checkout@v3
      - name: Decode .env
        run: echo "${{ secrets.ENV }}" | base64 --decode > .env
      - name: Read pubspec.yaml
        uses: jbutcher5/read-yaml@1.6
        id: read_pubspec
        with:
          file: './pubspec.yaml'
          key-path: '["version"]'
      - name: Save version on env variable
        id: save_version
        run: |
          version=${{ steps.read_pubspec.outputs.data }}
          IFS='+' 
          read -r -a split <<< "$version"
          echo "VERSION_NAME=$(echo ${split[0]})" >> $GITHUB_ENV
          echo "version_name=${split[0]}" >> $GITHUB_OUTPUT
          echo "version_number=${split[1]}" >> $GITHUB_OUTPUT
      - uses: subosito/flutter-action@v2
        with:
          channel: "stable"
      - run: flutter clean
      - run: flutter pub get
      - run: flutter build macos --release
      - name: Install the Apple certificate and sign the application
        env:
          APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
          APPLE_CERTIFICATE_PWD: ${{ secrets.APPLE_CERTIFICATE_PWD }}
          APPLE_KEYCHAIN_PWD: ${{ secrets.APPLE_KEYCHAIN_PWD }}
          APPLE_IDENTITY_ID: ${{ secrets.APPLE_IDENTITY_ID }}
        run: |
          echo "$APPLE_CERTIFICATE" | base64 --decode > certificate.p12
          security create-keychain -p $APPLE_KEYCHAIN_PWD build.keychain
          security default-keychain -s build.keychain
          security unlock-keychain -p $APPLE_KEYCHAIN_PWD build.keychain
          security import certificate.p12 -k build.keychain -P $APPLE_CERTIFICATE_PWD -T /usr/bin/codesign
          security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k $APPLE_KEYCHAIN_PWD build.keychain
          /usr/bin/codesign --force -s "$APPLE_IDENTITY_ID" $MACOS_APP_RELEASE_PATH/AdGuard\ Home\ Manager.app -v
      - name: Create folder to build dmg
        run: mkdir $MACOS_APP_RELEASE_PATH/AdGuard\ Home\ Manager
      - name: Copy app into folder
        run: cp -r $MACOS_APP_RELEASE_PATH/AdGuard\ Home\ Manager.app $MACOS_APP_RELEASE_PATH/AdGuard\ Home\ Manager/AdGuard\ Home\ Manager.app
      - name: Generate symbolic link to Applications dir
        run: ln -s /Applications $MACOS_APP_RELEASE_PATH/AdGuard\ Home\ Manager
      - name: Generate dmg
        run: hdiutil create -srcfolder $MACOS_APP_RELEASE_PATH/AdGuard\ Home\ Manager $MACOS_APP_RELEASE_PATH/AdGuardHomeManager_${{ env.VERSION_NAME }}_macOS_Universal.dmg
      - name: Copy dmg to project root
        run: cp $MACOS_APP_RELEASE_PATH/AdGuardHomeManager_${{ env.VERSION_NAME }}_macOS_Universal.dmg AdGuardHomeManager_${{ env.VERSION_NAME }}_macOS_Universal.dmg
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: macos
          path: AdGuardHomeManager_${{ env.VERSION_NAME }}_macOS_Universal.dmg
  build-linux:
    name: Build Linux .tar.gz and .deb
    runs-on: ubuntu-latest
    outputs:
      VERSION_NAME: ${{ steps.save_version.outputs.version_name }}
      VERSION_NUMBER: ${{ steps.save_version.outputs.version_number }}
    steps:
      - uses: actions/checkout@v3
      - name: Decode .env
        run: echo "${{ secrets.ENV }}" | base64 --decode > .env
      - name: Read pubspec.yaml
        uses: jbutcher5/read-yaml@1.6
        id: read_pubspec
        with:
          file: './pubspec.yaml'
          key-path: '["version"]'
      - name: Save version on env variable
        id: save_version
        run: |
          version=${{ steps.read_pubspec.outputs.data }}
          IFS='+' 
          read -r -a split <<< "$version"
          echo "VERSION_NAME=$(echo ${split[0]})" >> $GITHUB_ENV
          echo "version_name=${split[0]}" >> $GITHUB_OUTPUT
          echo "version_number=${split[1]}" >> $GITHUB_OUTPUT
      - name: Update version in debian.yaml
        run: sed -i 's/<REPLACE_VERSION_NUMBER_ACTIONS>/${{ env.VERSION_NAME }}/g' debian/debian.yaml
      - name: Update dependencies list
        run: sudo apt-get update
      - name: Install dependencies
        run: sudo apt-get install -y clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev
      - uses: subosito/flutter-action@v2
        with:
          channel: "stable"
      - run: flutter clean
      - run: flutter pub get
      - run: flutter build linux --release
      - name: Install flutter_to_debian
        run: dart pub global activate flutter_to_debian
      - name: Generate .deb package
        run: flutter_to_debian
      - name: Move .deb package to project root
        run: mv build/linux/x64/release/debian/AdGuardHomeManager_${{ env.VERSION_NAME }}_amd64.deb AdGuardHomeManager_${{ env.VERSION_NAME }}_Linux_amd64.deb 
      - name: Generate .tar.gz package
        uses: a7ul/tar-action@v1.1.3
        id: compress
        with:
          command: c
          cwd: build/linux/x64/release/bundle
          files: |
            ./data
            ./lib
            ./AdGuardHomeManager
          outPath: AdGuardHomeManager_${{ env.VERSION_NAME }}_Linux.tar.gz
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: linux
          path: |
            AdGuardHomeManager_${{ env.VERSION_NAME }}_Linux_amd64.deb 
            AdGuardHomeManager_${{ env.VERSION_NAME }}_Linux.tar.gz
  build-windows:
    name: Build Windows installer
    runs-on: windows-latest
    outputs:
      VERSION_NAME: ${{ steps.save_version.outputs.version_name }}
      VERSION_NUMBER: ${{ steps.save_version.outputs.version_number }}
    steps:
      - uses: actions/checkout@v3
      - name: Decode .env
        shell: pwsh
        run: |
          [IO.File]::WriteAllBytes('.env', [Convert]::FromBase64String('${{ secrets.ENV }}'))
      - name: Read pubspec.yaml
        uses: jbutcher5/read-yaml@1.6
        id: read_pubspec
        with:
          file: './pubspec.yaml'
          key-path: '["version"]'
      - name: Save version on env variable
        id: save_version
        run: |
          version=${{ steps.read_pubspec.outputs.data }}
          IFS='+' 
          read -r -a split <<< "$version"
          echo "VERSION_NAME=$(echo ${split[0]})" >> $GITHUB_ENV
          echo "version_name=${split[0]}" >> $GITHUB_OUTPUT
          echo "version_number=${split[1]}" >> $GITHUB_OUTPUT
      - name: Update version in innosetup config file
        shell: pwsh
        run: |
          (Get-Content windows/innosetup_installer_builder.iss) -replace '<REPLACE_VERSION_ACTIONS>', '${{ env.VERSION_NAME }}' | Out-File -encoding ASCII windows/innosetup_installer_builder.iss
      - uses: subosito/flutter-action@v2
        with:
          channel: "stable"
      - run: flutter clean
      - run: flutter pub get
      - run: flutter build windows --release
      - name: Build installer witn innosetup
        run: iscc /Q windows/innosetup_installer_builder.iss
      - name: Move installer file to root directory
        run: move build/windows/aghm_installer.exe AdGuardHomeManager_${{ env.VERSION_NAME }}_Windows_x64.exe
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: windows
          path: AdGuardHomeManager_${{ env.VERSION_NAME }}_Windows_x64.exe
  release-builds-github:
    name: Release builds to GitHub
    runs-on: ubuntu-latest
    needs: [build-android, build-macos, build-linux, build-windows]
    env: 
      VERSION_NAME: ${{ needs.build-android.outputs.VERSION_NAME }}
      VERSION_NUMBER: ${{ needs.build-android.outputs.VERSION_NUMBER }}
    steps:
      - uses: actions/checkout@v3
      - name: Create builds directory
        run: mkdir releases
      - name: Download Android artifacts
        uses: actions/download-artifact@v3
        with:
          name: android
          path: releases/
      - name: Download macOS artifacts
        uses: actions/download-artifact@v3
        with:
          name: macos
          path: releases/
      - name: Download Linux artifacts
        uses: actions/download-artifact@v3
        with:
          name: linux
          path: releases/
      - name: Download Windows artifacts
        uses: actions/download-artifact@v3
        with:
          name: windows
          path: releases/
      - name: Release to GitHub
        uses: ncipollo/release-action@v1
        with:
          artifacts: "releases/*"
          token: ${{ secrets.GH_TOKEN }}
          tag: '${{ env.VERSION_NAME }}_(${{ env.VERSION_NUMBER }})'
          name: v${{ env.VERSION_NAME }}
          draft: true
          prerelease: false
          commit: ${{ github.sha }}
  release-build-google-play:
    name: Release Android build to the Google Play Store
    runs-on: ubuntu-latest
    needs: [build-android, build-macos, build-linux, build-windows]
    env: 
      VERSION_NAME: ${{ needs.build-android.outputs.VERSION_NAME }}
    steps:
      - uses: actions/checkout@v3
      - name: Download Android artifacts
        uses: actions/download-artifact@v3
        with:
          name: android
      - name: Release app to Google Play
        uses: r0adkll/upload-google-play@v1
        with:
          serviceAccountJsonPlainText: ${{ secrets.PLAYSTORE_ACCOUNT_KEY }}
          packageName: com.jgeek00.adguard_home_manager
          releaseFiles: AdGuardHomeManager_${{ env.VERSION_NAME }}_Android.aab
          track: production
          status: draft
          releaseName: ${{ env.VERSION_NAME }}