#!/bin/bash

# <xbar.title>Org Clock</xbar.title>
# <xbar.desc>Shows currently clocked org task (fixed open-clock detection)</xbar.desc>

# ←←← CHANGE THIS TO YOUR ACTUAL ORG FOLDER
ORG_DIR="$HOME/doc/org"          # e.g. ~/Documents/org or ~/Dropbox/org

if [[ ! -d "$ORG_DIR" ]]; then
    echo "⏱ Org folder not found"
    exit 0
fi

TASK=$(find "$ORG_DIR" -name "*.org" -type f -print0 2>/dev/null \
  | xargs -0 awk '
      /^\*+ / { headline = $0 }
      /CLOCK: \[/ && !/--/ {
          print headline
          exit
      }
    ' \
  | head -n 1 \
  | sed -E 's/^[ \t]*\*+[ \t]*//')

if [[ -n "$TASK" ]]; then
    echo "⏱ $TASK"
else
    echo "⏱ No clock"
fi
