From 4eb8f12e25934e604ac096c357abcfdae87306ef Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sat, 28 Apr 2018 00:59:21 +0200 Subject: Initial commit, script is pretty much done --- logentry | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 logentry (limited to 'logentry') diff --git a/logentry b/logentry new file mode 100755 index 0000000..0c1bd41 --- /dev/null +++ b/logentry @@ -0,0 +1,54 @@ +#!/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" -- cgit v1.2.3