name: Release

on:
  push:
    tags:
      - "v*.*.*"

jobs:
  android_release:
    name: Android Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Setup JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: gradle
      - name: Setup Rust build
        run: |
          cargo install cargo-ndk
          rustup target add aarch64-linux-android
          rustup target add armv7-linux-androideabi
          rustup target add x86_64-linux-android
      - name: Setup Java build
        run: |
          chmod +x android/gradlew
          echo "${{ secrets.ANDROID_RELEASE_KEYSTORE }}" > release.keystore.asc
          gpg -d --passphrase "${{ secrets.ANDROID_RELEASE_SECRET }}" --batch release.keystore.asc > android/keystore
          echo -e "storePassword=${{ secrets.ANDROID_PASS }}\nkeyPassword=${{ secrets.ANDROID_PASS }}\nkeyAlias=grim\nstoreFile=../keystore" > android/keystore.properties
      - name: Build lib ARMv8 1/2
        continue-on-error: true
        run: |
          sed -i -e 's/"rlib"/"cdylib","rlib"/g' Cargo.toml
          export CPPFLAGS="-DMDB_USE_ROBUST=0" && export CFLAGS="-DMDB_USE_ROBUST=0" && cargo ndk -t arm64-v8a build --profile release-apk
      - name: Build lib ARMv8 2/2
        run: |
          unset CPPFLAGS && unset CFLAGS && cargo ndk -t arm64-v8a -o android/app/src/main/jniLibs build --profile release-apk
          sed -i -e 's/"cdylib","rlib"/"rlib"/g' Cargo.toml
      - name: Build APK ARMv8
        working-directory: android
        run: |
          ./gradlew assembleRelease
          mv app/build/outputs/apk/release/app-release.apk grim-${{ github.ref_name }}-android-armv8.apk
      - name: Checksum APK ARMv8
        working-directory: android
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-android-armv8.apk | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-android-armv8-sha256sum.txt
      - name: Build lib ARMv7 1/2
        continue-on-error: true
        run: |
          sed -i -e 's/"rlib"/"cdylib","rlib"/g' Cargo.toml
          export CPPFLAGS="-DMDB_USE_ROBUST=0" && export CFLAGS="-DMDB_USE_ROBUST=0" && cargo ndk -t armeabi-v7a build --profile release-apk
      - name: Build lib ARMv7 2/2
        run: |
          unset CPPFLAGS && unset CFLAGS && cargo ndk -t armeabi-v7a -o android/app/src/main/jniLibs build --profile release-apk
          sed -i -e 's/"cdylib","rlib"/"rlib"/g' Cargo.toml
      - name: Build APK ARMv7
        working-directory: android
        run: |
          rm -rf app/build
          rm -rf app/src/main/jniLibs/*
          ./gradlew assembleRelease
          mv app/build/outputs/apk/release/app-release.apk grim-${{ github.ref_name }}-android-armv7.apk
      - name: Checksum APK ARMv7
        working-directory: android
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-android-armv7.apk | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-android-armv7-sha256sum.txt
      - name: Build lib x86 1/2
        continue-on-error: true
        run: |
          sed -i -e 's/"rlib"/"cdylib","rlib"/g' Cargo.toml
          export CPPFLAGS="-DMDB_USE_ROBUST=0" && export CFLAGS="-DMDB_USE_ROBUST=0" && cargo ndk -t x86_64 build --profile release-apk
      - name: Build lib x86 2/2
        run: |
          unset CPPFLAGS && unset CFLAGS && cargo ndk -t x86_64 -o android/app/src/main/jniLibs build --profile release-apk
          sed -i -e 's/"cdylib","rlib"/"rlib"/g' Cargo.toml
      - name: Build APK x86
        working-directory: android
        run: |
          rm -rf app/build
          rm -rf app/src/main/jniLibs/*
          ./gradlew assembleRelease
          mv app/build/outputs/apk/release/app-release.apk grim-${{ github.ref_name }}-android-x86_64.apk
      - name: Checksum APK x86
        working-directory: android
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-android-x86_64.apk | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-android-x86_64-sha256sum.txt
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            android/grim-${{ github.ref_name }}-android-armv8.apk
            android/grim-${{ github.ref_name }}-android-armv8-sha256sum.txt
            android/grim-${{ github.ref_name }}-android-armv7.apk
            android/grim-${{ github.ref_name }}-android-armv7-sha256sum.txt
            android/grim-${{ github.ref_name }}-android-x86_64.apk
            android/grim-${{ github.ref_name }}-android-x86_64-sha256sum.txt

  linux_release:
    name: Linux Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Download appimagetools
        run: |
          wget https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage
          chmod +x appimagetool-x86_64.AppImage
          sudo apt install libfuse2
      - name: Zig Setup
        uses: goto-bus-stop/setup-zig@v2
        with:
          version: 0.12.1
      - name: Install cargo-zigbuild
        run: cargo install cargo-zigbuild
      - name: Release x86
        run: cargo zigbuild --release --target x86_64-unknown-linux-gnu
      - name: Release ARM
        run: |
          rustup target add aarch64-unknown-linux-gnu
          cargo zigbuild --release --target aarch64-unknown-linux-gnu
      - name: AppImage x86
        run: |
          cp target/x86_64-unknown-linux-gnu/release/grim linux/Grim.AppDir/AppRun
          ./appimagetool-x86_64.AppImage linux/Grim.AppDir target/x86_64-unknown-linux-gnu/release/grim-${{ github.ref_name }}-linux-x86_64.AppImage
      - name: Checksum AppImage x86
        working-directory: target/x86_64-unknown-linux-gnu/release
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-linux-x86_64.AppImage | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-linux-x86_64-appimage-sha256sum.txt
      - name: AppImage ARM
        run: |
          cp target/aarch64-unknown-linux-gnu/release/grim linux/Grim.AppDir/AppRun
          ./appimagetool-x86_64.AppImage linux/Grim.AppDir target/aarch64-unknown-linux-gnu/release/grim-${{ github.ref_name }}-linux-arm.AppImage
      - name: Checksum AppImage ARM
        working-directory: target/aarch64-unknown-linux-gnu/release
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-linux-arm.AppImage | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-linux-arm-appimage-sha256sum.txt
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            target/x86_64-unknown-linux-gnu/release/grim-${{ github.ref_name }}-linux-x86_64.AppImage
            target/x86_64-unknown-linux-gnu/release/grim-${{ github.ref_name }}-linux-x86_64-appimage-sha256sum.txt
            target/aarch64-unknown-linux-gnu/release/grim-${{ github.ref_name }}-linux-arm.AppImage
            target/aarch64-unknown-linux-gnu/release/grim-${{ github.ref_name }}-linux-arm-appimage-sha256sum.txt

  windows_release:
    name: Windows Release
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Build release
        run: cargo build --release
      - name: Archive release
        uses: vimtor/action-zip@v1
        with:
          files: target/release/grim.exe
          dest: target/release/grim-${{ github.ref_name }}-win-x86_64.zip
      - name: Checksum release
        working-directory: target/release
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-win-x86_64.zip | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-win-x86_64-sha256sum.txt
      - name: Install cargo-wix
        run: cargo install cargo-wix
      - name: Run cargo-wix
        run: cargo wix -p grim -o ./target/wix/grim-${{ github.ref_name }}-win-x86_64.msi --nocapture
      - name: Checksum msi
        working-directory: target/wix
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-win-x86_64.msi | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-win-x86_64-msi-sha256sum.txt
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            target/release/grim-${{ github.ref_name }}-win-x86_64.zip
            target/release/grim-${{ github.ref_name }}-win-x86_64-sha256sum.txt
            target/wix/grim-${{ github.ref_name }}-win-x86_64.msi
            target/wix/grim-${{ github.ref_name }}-win-x86_64-msi-sha256sum.txt

  macos_release:
    name: MacOS Release
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Zig Setup
        uses: goto-bus-stop/setup-zig@v2
        with:
          version: 0.12.1
      - name: Install cargo-zigbuild
        run: cargo install cargo-zigbuild
      - name: Release x86
        run: |
          rustup target add x86_64-apple-darwin
          cargo zigbuild --release --target x86_64-apple-darwin
          mkdir macos/Grim.app/Contents/MacOS
          yes | cp -rf target/x86_64-apple-darwin/release/grim macos/Grim.app/Contents/MacOS
      - name: Archive x86
        run: |
          cd macos
          zip -r grim-${{ github.ref_name }}-macos-x86_64.zip Grim.app
          mv grim-${{ github.ref_name }}-macos-x86_64.zip ../target/x86_64-apple-darwin/release
          cd ..
      - name: Checksum Release x86
        working-directory: target/x86_64-apple-darwin/release
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-macos-x86_64.zip | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-macos-x86_64-sha256sum.txt
      - name: Release ARM
        run: |
          rustup target add aarch64-apple-darwin
          cargo zigbuild --release --target aarch64-apple-darwin
          yes | cp -rf target/aarch64-apple-darwin/release/grim macos/Grim.app/Contents/MacOS
      - name: Archive ARM
        run: |
          cd macos
          zip -r grim-${{ github.ref_name }}-macos-arm.zip Grim.app
          mv grim-${{ github.ref_name }}-macos-arm.zip ../target/aarch64-apple-darwin/release
          cd ..
      - name: Checksum Release ARM
        working-directory: target/aarch64-apple-darwin/release
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-macos-arm.zip | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-macos-arm-sha256sum.txt
      - name: Release Universal
        run: |
          rustup target add aarch64-apple-darwin
          rustup target add x86_64-apple-darwin
          cargo zigbuild --release --target universal2-apple-darwin
          yes | cp -rf target/universal2-apple-darwin/release/grim macos/Grim.app/Contents/MacOS
      - name: Archive Universal
        run: |
          cd macos
          zip -r grim-${{ github.ref_name }}-macos-universal.zip Grim.app
          mv grim-${{ github.ref_name }}-macos-universal.zip ../target/universal2-apple-darwin/release
          cd ..
      - name: Checksum Release Universal
        working-directory: target/universal2-apple-darwin/release
        shell: pwsh
        run: get-filehash -algorithm sha256 grim-${{ github.ref_name }}-macos-universal.zip | Format-List |  Out-String | ForEach-Object { $_.Trim() } > grim-${{ github.ref_name }}-macos-universal-sha256sum.txt
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            target/x86_64-apple-darwin/release/grim-${{ github.ref_name }}-macos-x86_64.zip
            target/x86_64-apple-darwin/release/grim-${{ github.ref_name }}-macos-x86_64-sha256sum.txt
            target/aarch64-apple-darwin/release/grim-${{ github.ref_name }}-macos-arm.zip
            target/aarch64-apple-darwin/release/grim-${{ github.ref_name }}-macos-arm-sha256sum.txt
            target/universal2-apple-darwin/release/grim-${{ github.ref_name }}-macos-universal.zip
            target/universal2-apple-darwin/release/grim-${{ github.ref_name }}-macos-universal-sha256sum.txt
