name: Java CI

on:
  push:
    branches:
      - master

jobs:
  test:
    runs-on: arch-rolling


    steps:
      - name: Check Dotnet Version
        run: |
          dotnet --version

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Extract FileVersion from .csproj
        id: get_version
        run: |
          version=$(grep -oPm1 "(?<=<FileVersion>)[^<]+" PS2_Manager/PS2_Manager.csproj)
          echo "PS2Manager version: $version"
          echo "version=$version" >> $GITHUB_OUTPUT

      - name: Build the project (Linux x64)
        run: |
          dotnet publish ./PS2_Manager/PS2_Manager.csproj \
            -c Release \
            -r linux-x64 \
            --self-contained true \
            -p:PublishSingleFile=true \
            -p:PublishTrimmed=true \
            -o ./output_linux          
      - name: Build the project (Windows x64)
        run: |
          dotnet publish ./PS2_Manager/PS2_Manager.csproj \
            -c Release \
            -r win-x64 \
            --self-contained true \
            -p:PublishSingleFile=true \
            -p:PublishTrimmed=true \
            -o ./output_win


      - name: Download appimagetool
        run: |
          curl -L -o appimagetool-x86_64.AppImage https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage
          chmod +x appimagetool-x86_64.AppImage

      - name: Build AppImage
        run: |
          mkdir -p PS2_Manager.AppDir/usr/bin
          cp -r ./output_linux/* PS2_Manager.AppDir/usr/bin/

          cp PS2_Manager/Assets/AppRun PS2_Manager.AppDir/
          cp PS2_Manager/Assets/ps2manager.desktop PS2_Manager.AppDir/
          cp PS2_Manager/Assets/ps2manager.png PS2_Manager.AppDir/

          chmod +x PS2_Manager.AppDir/AppRun

          ./appimagetool-x86_64.AppImage PS2_Manager.AppDir

          mkdir -p release
          mv PS2_Manager-x86_64.AppImage release/PS2Manager_Linux_x64.AppImage

      - name: Pack Releases into Zips
        run: |
          mkdir -p ./release
          zip -r ./release/PS2Manager_Linux_x64.zip ./output_linux
          zip -r ./release/PS2Manager_Windows_x64.zip ./output_win
          rm -rf ./output_linux ./output_win



      - name: Generate tag name based on current date and time
        id: generate_tag
        run: |
          # Get current date and time, format as 'YYYY-MM-DD-HHMMSS'
          current_date=$(date +"%Y-%m-%d-%H%M%S")
          echo "Generated tag name: $current_date"
          echo "::set-output name=tag_name::$current_date"     


      - name: Create Git tag
        run: |
          git config user.name "WeeXnes"
          git config user.email "somtrigi@gmail.com"
          git tag v${{ steps.get_version.outputs.version }}
          git push origin v${{ steps.get_version.outputs.version }}

      - name: Upload to Forgejo
        uses: actions/forgejo-release@v2.6.0
        with:
          direction: upload
          url: https://git.weexnes.dev/
          repo: WeeXnes/ps2_manager
          token: ${{ secrets.RELEASE_TOKEN }}
          tag: v${{ steps.get_version.outputs.version }}
          release-dir: release
          release-notes: "Automated release for version ${{ steps.get_version.outputs.version }}"