name: Theos CI

on:
  push:
    branches: ['*']
    paths-ignore: ['.gitignore']
  pull_request:
    branches: ['*']
    paths-ignore: ['.gitignore']
  workflow_dispatch:
  schedule:
    - cron: '0 0 1 1 *'
    - cron: '0 0 1 4 *'
    - cron: '0 0 30 6 *'
    - cron: '0 0 28 9 *'
    - cron: '0 0 27 12 *'

jobs:
  build:
    runs-on: macos-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          submodules: recursive

      - name: Theos Setup
        uses: NyaMisty/theos-action@master

      - name: Determine Tag or Default
        id: determine_tag
        run: |
          # Check if the current ref is a tag
          if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
            TAG_NAME="${GITHUB_REF#refs/tags/}"
          else
            TAG_NAME="0.0.5" # Fallback tag version
          fi
          echo "TAG_NAME=${TAG_NAME}" >> $GITHUB_ENV

      - name: Create Tag
        run: |
          git config --local user.name "github-actions[bot]"
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git tag ${{ env.TAG_NAME }} || echo "Tag already exists."
          git push origin ${{ env.TAG_NAME }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Build Release package - Rootless
        run: |
          cd screendumpLowFrame
          make clean
          make package THEOS_PACKAGE_SCHEME=rootless FINALPACKAGE=1 PACKAGE_VERSION=${{ env.TAG_NAME }}-rootless

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: screendumpLowFrame/packages/*.deb
          tag_name: ${{ env.TAG_NAME }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
