name: TVPN Actions Workflow
run-name: Deploy to ${{ inputs.deploy_target }} by ${{ gitea.actor }}
on: [push]

jobs:
  build:
    runs-on: windows-10
    steps:
      - uses: actions/checkout@v4
      #- name: Setup JDK 21
      #  uses: actions-setup-java@v4.4.0
      #  with:
      #    java-version: 21
      #    distribution: 'temurin'
      - name: Test Java
        run: java --version
      - name: Configure Git
        run: |
          git config --global user.email "git@nevets.tech"
          git config --global user.name "Gitea Actions"
          git clone https://git.nevets.tech/Steven/TVPN-jre.git jre
      - name: Build TVPN Jar
        run: .\gradlew.bat clean shadowJar createExe
      - name: Copy Header to C Dir
        run: |
          cp build/generated/sources/headers/java/main/tech_nevets_tvpn_wg_WireGuardJNI.h src/main/cpp/
      - name: Build DLL
        run: |
          cmake -DCMAKE_BUILD_TYPE=Release -G "CodeBlocks - MinGW Makefiles" -S ./src/main/cpp -B ./src/main/cpp/build
          cmake --build ./src/main/cpp/build --target wireguard_wrapper -- -j 14
      #  uses: actions/cmake-action@v2.0.0
      #  with:
      #   source-dir: src/main/cpp
      #    output-dir: src/main/cpp/build
      - name: Prepare artifacts
        run: |
          Get-ChildItem -Path artifacts -Recurse | Remove-Item -force -recurse
          if (Test-Path -Path .\artifacts) { Remove-Item -Path .\artifacts -Recurse -Force }
          $version = (Select-String -Path "build.gradle" -Pattern "^\s*version\s*=\s*'([^']+)'" | ForEach-Object { $_.Matches[0].Groups[1].Value })
          echo Version: $version
          New-Item -Path "artifacts" -ItemType Directory
          Copy-Item -Path "./build/libs/TVPN-$version-all.jar" -Destination "./artifacts/TVPN-$version.jar" -Verbose
          Copy-Item -Path "./build/launch4j/TVPN.exe" -Destination "./artifacts/TVPN.exe" -Verbose
          Copy-Item -Path "./src/main/cpp/build/libwireguard_wrapper.dll" -Destination "./artifacts/libwireguard_wrapper.dll" -Verbose
          Copy-Item -Path "./libs/tunnel.dll" -Destination "./artifacts/tunnel.dll" -Verbose
          Copy-Item -Path "./libs/wireguard.dll" -Destination "./artifacts/wireguard.dll" -Verbose
          Copy-Item -Path "./jre/jre" -Destination "./artifacts/jre" -Recurse

      - name: Create Installer
        run: |
          $version = (Select-String -Path "build.gradle" -Pattern "^\s*version\s*=\s*'([^']+)'" | ForEach-Object { $_.Matches[0].Groups[1].Value })
          ISCC /DMyAppVersion=$version "./installer-script.iss"
      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: artifacts
          path: artifacts\*
