#!/bin/bash
# bin/daily.sh — auto-create today's daily note from template
# Cron: 0 6 * * * /home/user/02-repos/shit_in_a_vault/bin/daily.sh

VAULT="/home/user/02-repos/shit_in_a_vault"
TPL="$VAULT/tpl/daily.md"
DEST_DIR="$VAULT/01-logs/daily"
DEST="$DEST_DIR/$(date +%Y-%m-%d).md"
DATE_STR=$(date +%Y-%m-%d)
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)

mkdir -p "$DEST_DIR"

if [ -f "$DEST" ]; then
  echo "Daily note $DEST already exists, skipping."
  exit 0
fi

if [ ! -f "$TPL" ]; then
  echo "Template not found: $TPL" >&2
  exit 1
fi

# Substitute {{date:YYYY-MM-DD}} and {{date:}} placeholders
sed -e "s|{{date:YYYY-MM-DD}}|$DATE_STR|g" \
    -e "s|{{date:}}|$DATE_STR|g" \
    -e "s|{{yesterday:YYYY-MM-DD}}|$YESTERDAY|g" \
    "$TPL" > "$DEST"

echo "Created: $DEST"

# Commit and push
cd "$VAULT" || exit 1
git add "$DEST"
if git diff --cached --quiet; then
  echo "Nothing to commit."
  exit 0
fi
git commit -m "daily: $DATE_STR" && git push
