name: build-windows-exe

on:
  push:
    tags:
      - "v*"

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        shell: bash
        run: |
          git clone --depth 1 --branch "${GITHUB_REF_NAME}" https://git.vrcworldtour.com/every_holiday/VRCWT-OSC.git .

      - name: Install Go
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get install -y golang-go
          go version

      - name: Test
        shell: bash
        run: |
          cd VRWT_Tool/VRC_OSC
          GO111MODULE=on go test ./...

      - name: Build executables
        shell: bash
        run: |
          cd VRWT_Tool/VRC_OSC
          mkdir -p dist
          VERSION="${GITHUB_REF_NAME}"
          BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
          GO111MODULE=on GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-H windowsgui -X vrc_osc_go/internal/buildinfo.Version=${VERSION} -X vrc_osc_go/internal/buildinfo.BuildTime=${BUILD_TIME}" -o dist/vrc_osc.exe ./cmd/vrc_osc
          GO111MODULE=on GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-X vrc_osc_go/internal/buildinfo.Version=${VERSION} -X vrc_osc_go/internal/buildinfo.BuildTime=${BUILD_TIME}" -o dist/vrc_osc_launcher.exe ./cmd/vrc_osc_launcher
          GO111MODULE=on GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-X vrc_osc_go/internal/buildinfo.Version=${VERSION} -X vrc_osc_go/internal/buildinfo.BuildTime=${BUILD_TIME}" -o dist/vrwt_tool.exe ./cmd/vrwt_tool

      - name: Package executables
        shell: bash
        run: |
          cd VRWT_Tool/VRC_OSC
          cp config/config.example.toml dist/config.example.toml
          cp config/secrets.example.toml dist/secrets.example.toml
          cp config/guests.example.txt dist/guests.example.txt
          (cd dist && zip -r vrc_osc-windows.zip vrc_osc.exe vrc_osc_launcher.exe vrwt_tool.exe config.example.toml secrets.example.toml guests.example.txt)

      - name: Create Release
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          GITEA_BASE_URL: ${{ secrets.GITEA_BASE_URL }}
          TAG_NAME: ${{ github.ref_name }}
        shell: bash
        run: |
          set -euo pipefail
          api="${GITEA_BASE_URL:-https://git.vrcworldtour.com}/api/v1"
          owner_repo="every_holiday/VRCWT-OSC"
          release_json=$(curl -fsSL \
            -H "Authorization: token ${GITEA_TOKEN}" \
            -H "Content-Type: application/json" \
            "${api}/repos/${owner_repo}/releases/tags/${TAG_NAME}" || true)
          if [ -n "${release_json}" ]; then
            release_id=$(printf '%s' "${release_json}" | python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])')
          else
            release_json=$(curl -fsSL -X POST \
              -H "Authorization: token ${GITEA_TOKEN}" \
              -H "Content-Type: application/json" \
              -d "{\"tag_name\":\"${TAG_NAME}\",\"name\":\"${TAG_NAME}\",\"draft\":false,\"prerelease\":false}" \
              "${api}/repos/${owner_repo}/releases")
            release_id=$(printf '%s' "${release_json}" | python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])')
          fi
          asset_path="VRWT_Tool/VRC_OSC/dist/vrc_osc-windows.zip"
          curl -fsSL -X POST \
            -H "Authorization: token ${GITEA_TOKEN}" \
            -F "attachment=@${asset_path}" \
            "${api}/repos/${owner_repo}/releases/${release_id}/assets?name=vrc_osc-windows.zip"
