name: Build and deploy
on: [push]
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

      - name: Check out code into the Go module directory
        uses: actions/checkout@v6

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version-file: go.mod

      - name: Lint
        run: make lint

      - name: Test
        run: go test ./...

  deploy:
    name: Deploy
    needs: build
    runs-on: ubuntu-latest
    if: github.repository == 'df-mc/dragonfly'
    steps:

      - name: Check out code into the Go module directory
        uses: actions/checkout@v6

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version-file: go.mod

      - name: Build
        run: go build -o dragonfly_exe -v .

      - name: Set SSH info
        env:
          SSH_KNOWN_HOSTS: ${{ secrets.VPS_KNOWN_HOSTS }}
          SSH_PRIVATE_KEY: ${{ secrets.VPS_PRIVATE_KEY }}
        run: |
          mkdir -p ~/.ssh/
          echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
          echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa

      - name: Stop server
        env:
          HOST: ${{ secrets.VPS_HOST }}
        run: |
          ssh -i ~/.ssh/id_rsa $HOST screen -d -R -S dragonfly -X stuff '^C'
          ssh -i ~/.ssh/id_rsa $HOST rm -f dragonfly_exe

      - name: Transfer executable
        env:
          HOST: ${{ secrets.VPS_HOST }}
        run: |
          scp -i ~/.ssh/id_rsa dragonfly_exe $HOST:/home/dragonfly_exe

      - name: Restart server
        env:
          HOST: ${{ secrets.VPS_HOST }}
        run: |
          ssh -i ~/.ssh/id_rsa $HOST "screen -d -R -S dragonfly -X stuff '/home/dragonfly_exe\n'"

  update_contributors:
    name: Update Contributors
    runs-on: ubuntu-latest
    if: github.repository == 'df-mc/dragonfly' && github.ref == 'refs/heads/master'
    steps:
      - uses: actions/create-github-app-token@v3
        id: app-token
        with:
          client-id: ${{ vars.PUSH_CLIENT_ID }}
          private-key: ${{ secrets.PUSH_SECRET }}
      - name: Checkout Repository
        uses: actions/checkout@v6
        with:
          token: ${{ steps.app-token.outputs.token }}
      - name: Fetch Contributors
        run: |
          CONTRIBUTOR_DATA=$(curl -H "Accept: application/vnd.github+json" https://api.github.com/repos/df-mc/dragonfly/contributors?per_page=9999)
          echo $CONTRIBUTOR_DATA
          if [ $(echo $CONTRIBUTOR_DATA | jq type) == '"array"' ]; then echo -e "// Code generated by .github/workflows/push.yml; DO NOT EDIT\n\npackage session\n\n// enchantNames are names translated to the 'Standard Galactic Alphabet' client-side. The names generally have no meaning\n// on the vanilla server implementation, so we can sneak some easter eggs in here without anyone noticing.\nvar enchantNames = []string{"$(echo $CONTRIBUTOR_DATA | jq .[].login | sed -r -e "s/([^A-Z\"])([A-Z])/\1 \2/g" | sed "s/./\L&/g" | sort | sed -z "s/\n/,/g")"}" > server/session/enchantment_texts.go && gofmt -w server/session/enchantment_texts.go; fi
      - name: Push Changes
        uses: stefanzweifel/git-auto-commit-action@v7
        with:
          commit_message: "updated contributor list"
