#!/usr/bin/zsh # Usage: logentry # This script relies on LOGBOOK_ROOT being set, if it is not, the script fails. if [ -z ${LOGBOOK_ROOT+x} ]; then echo "LOGBOOK_ROOT is not set, exiting" exit 1 fi # It also needs to already exist if [ ! -d "$LOGBOOK_ROOT" ]; then echo "LOGBOOK_ROOT is not a valid directory, exiting" exit 1 fi # A category is required as an argument if [ $# -le 0 ]; then echo "Usage: logentry [title]" exit fi # It also needs to exist - this is to prevent typos from creating a new category and messing everything up CAT_PATH="${LOGBOOK_ROOT%/}/$1" if [ ! -d "$CAT_PATH" ]; then echo "$1 is not a valid category, exiting" exit 1 fi # Ensure that the target directory exists MONTH=$(date +%B | tr '[:upper:]' '[:lower:]') YEAR=$(date +%Y) DIR="${CAT_PATH%/}/$YEAR/$MONTH" mkdir -p "$DIR" if [ $? -ne 0 ]; then exit 1 fi # Create full path ISO_DATE=$(date --iso-8601) if [ $# -gt 1 ]; then NAME_OPT="-$2" fi FULLNAME="$DIR/$ISO_DATE$NAME_OPT.md" # Create the file with the correct heading, but only if it doesn't already exist if [ ! -e "$FULLNAME" ]; then echo "# $ISO_DATE\n## " > "$FULLNAME" if [ $? -ne 0 ]; then exit 1 fi fi # Edit the file vim "$FULLNAME"