on:
  pull_request:
    types: [opened, synchronize, reopened, closed]
  push:
jobs:
  web:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write # allow surge-preview to create/update PR comments
    env:
      BUNDLE_GEMFILE: ${{ github.workspace }}/web/Gemfile
    steps:
      - name: Clone repository
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.2.2
          bundler-cache: true
      - name: Build
        run: cd web && bundle exec jekyll build
        
      - name: Preview
        if: github.event_name == 'pull_request'
        uses: afc163/surge-preview@v1
        with:
          surge_token: ${{ secrets.SURGE_TOKEN }}
          dist: web/_site
          teardown: true
          build: echo # see https://github.com/afc163/surge-preview/issues/231
      
      # this is a beautiful way to deploy a website and i will not take any criticism
      - name: Deploy
        if: github.event_name == 'push' && github.ref_name == 'main'
        run: |
          curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb && sudo dpkg -i cloudflared.deb
          mkdir ~/.ssh && echo "${{ secrets.ELLBOT_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
          rsync -rv --delete -e 'ssh -o "ProxyCommand cloudflared access ssh --hostname %h" -o "StrictHostKeyChecking=no"' web/_site/. ellbot@ssh.ellpeck.de:/var/www/tinylifegame
      - name: Purge Cloudflare cache
        uses: NathanVaughn/actions-cloudflare-purge@v3.1.0
        with:
          cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
          cf_auth: ${{ secrets.CLOUDFLARE_TOKEN }}
      
  docs:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write # allow surge-preview to create/update PR comments
    steps:
      - name: Clone repository
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.305
      - name: Build
        run: cd docs && dotnet tool restore && dotnet docfx
        
      - name: Preview
        if: github.event_name == 'pull_request'
        uses: afc163/surge-preview@v1
        with:
          surge_token: ${{ secrets.SURGE_TOKEN }}
          dist: docs/_site
          teardown: true
          build: echo # see https://github.com/afc163/surge-preview/issues/231
      
      # this is a beautiful way to deploy a website and i will not take any criticism
      - name: Deploy
        if: github.event_name == 'push' && github.ref_name == 'main'
        run: |
          curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb && sudo dpkg -i cloudflared.deb
          mkdir ~/.ssh && echo "${{ secrets.ELLBOT_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
          rsync -rv --delete -e 'ssh -o "ProxyCommand cloudflared access ssh --hostname %h" -o "StrictHostKeyChecking=no"' docs/_site/. ellbot@ssh.ellpeck.de:/var/www/tinylifedocs
      - name: Purge Cloudflare cache
        uses: NathanVaughn/actions-cloudflare-purge@v3.1.0
        with:
          cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
          cf_auth: ${{ secrets.CLOUDFLARE_TOKEN }}