name: "Deploy to Netlify"

on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: "Deploy Hugo site to Netlify"
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true
          fetch-depth: 0
        name: Checkout
      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: "0.124.1"
          extended: true
          github-token: ${{ secrets.GH_TOKEN }}
      - name: Build
        run: |
          hugo version
          hugo --minify
      - uses: https://github.com/nwtgck/actions-netlify@v2.0
        name: Deploy
        with:
          publish-dir: "./public"
          production-deploy: true
          github-token: ${{ secrets.GH_TOKEN }}
          deploy-message: "Deployed from Gitea Action"
          enable-commit-comment: false
          enable-pull-request-comment: false
          overwrites-pull-request-comment: true
          enable-github-deployment: false
        env:
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
        timeout-minutes: 1
      - name: Post to Social Media
        run: |
          COMMIT=$(git log --oneline -1 --format=%s | sed 's/^.*: //')
          PREFIX="Post "
          if [[ "$COMMIT" == "$PREFIX"* ]]; then
            SUBDIR=$(find content/posts -name "${COMMIT:5}*" -type d)
            POST=${SUBDIR:14}
            TITLE=$(awk -v pat="title" -F":" '$0 ~ pat {print $2}' $SUBDIR/index.md)
            DESC=$(awk -v pat="description" -F":" '$0 ~ pat {print $2}' $SUBDIR/index.md)
            echo "Posting the following to mastodon"
            echo "Check out my latest blog post $TITLE about $DESC read more at https://allisthewave.com/posts/$POST"
            curl https://fosstodon.org/api/v1/statuses -H 'Authorization: Bearer kdKxV9v4Cv-GwhAd5YHDqI6xJsKT4KkFX89FlhPvBzw' -F 'status=Check out my latest blog post $TITLE about $DESC read more at https://allisthewave.com/posts/$POST'
          else
            echo "The commit message is not in format 'Post YYYY-MM-DD' or failed to parse..."
          fi
