lackadaisical/agenda

60 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# This script is intended to be run via cron.
# It creates a folder structure in home for the current date in the following format:
# $HOME/<Tag>/<Year>/<Month>/<Day>
# It also sets up a symlink for the tagged folder.
# This symlink will always point to the folder for the current date.
# Finally, it removes any folders without data, to prevent clutter.
LD_INTERNAL=1
. $(dirname $(realpath $0))/daisy.source
if [[ $1 == '' ]]; then
echo "Usage: $LD_BIN <folder>"
echo "Creates a folder within '.daisy' in the current directory containing"
echo "a tree of dates associated with the folder name given by argument."
echo "A symlink is (re-)created at the same time with the same folder name."
echo "For example, if given the folder 'notes', this utility will:"
echo "- Create a folder '/.daisy/notes/<year>/<month>/<day>'."
echo "- Create a symlink named 'notes' that points to it."
echo "It is recommended to run this via cron."
exit 1
fi
dir=$(realpath -s "$1")
parent_dir=$(dirname "$dir")
name=$(basename "$dir")
root_dir="$parent_dir/.daisy/$name"
today_sym="$dir"
# Present day
read year month day < <(date "+%Y %m %d")
today="$root_dir/$year/$month/$day"
set -e
function errorFn
{
error=$?
if [[ $error -gt 0 ]];
then
echo "$LD_BIN error ($error): "
perl -E 'say $!=shift' $error
fi
exit $error
}
# Error handling
trap errorFn ERR
# First we clear out empty folders, and remove the symlink if it exists
test -e "$root_dir" && find "$root_dir" -maxdepth 3 -type d -empty -delete
# Now we can set up today's directory
mkdir -p "$today"
ln -snf "$today" "$today_sym"
ln -snf "$root_dir" "$dir/.tree"
exitcode=$?