60 lines
1.6 KiB
Bash
Executable File
60 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# On-demand rollover of a todo note.
|
|
# By default copies yesterday's note to today, pruning finished work and retitling.
|
|
# Usage: todo_rollover [source_date] [target_date]
|
|
# source_date defaults to yesterday (YYYY-MM-DD)
|
|
# target_date defaults to today
|
|
# Set TODO_NOTES to override notes directory.
|
|
# Set TODO_PRUNE to override prune script.
|
|
|
|
set -euo pipefail
|
|
|
|
NOTES=${TODO_NOTES:-$HOME/docs/notes/todo}
|
|
PRUNE=${TODO_PRUNE:-$HOME/.local/lib/todo/prune.py}
|
|
|
|
src_date=${1:-$(date -d yesterday +%Y-%m-%d)}
|
|
target_date=${2:-$(date +%Y-%m-%d)}
|
|
|
|
src="$NOTES/$src_date.md"
|
|
dst="$NOTES/$target_date.md"
|
|
|
|
if [[ ! -f $src ]]; then
|
|
echo "todo_rollover: source note $src not found" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ -f $dst ]]; then
|
|
echo "todo_rollover: target $dst already exists, skipping (use --force to overwrite)" >&2
|
|
echo " Existing target was created from a previous rollover. If you really want to replace it, remove it first." >&2
|
|
exit 2
|
|
fi
|
|
|
|
mkdir -p "$NOTES"
|
|
tmp=$(mktemp)
|
|
trap 'rm -f "$tmp"' EXIT
|
|
|
|
if [[ -x $PRUNE ]]; then
|
|
python3 "$PRUNE" "$src" > "$tmp"
|
|
else
|
|
# fallback: just copy
|
|
cp "$src" "$tmp"
|
|
fi
|
|
|
|
# Retitle first line to target date, matching rollover.py behavior
|
|
# Only replace line 1 if it looks like a date heading
|
|
first_line=$(head -n1 "$tmp")
|
|
if [[ $first_line =~ ^#\ [0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
|
|
sed -i "1s/.*/# $target_date/" "$tmp"
|
|
else
|
|
# prepend heading
|
|
printf "# %s\n\n" "$target_date" | cat - "$tmp" > "${tmp}.new"
|
|
mv "${tmp}.new" "$tmp"
|
|
fi
|
|
|
|
# Preserve mode of source
|
|
mode=$(stat -c %a "$src")
|
|
cp -p "$src" "$dst" # to get mode
|
|
cat "$tmp" > "$dst"
|
|
|
|
echo "todo_rollover: created $dst from $src_date.md (pruned)"
|