aboutsummaryrefslogtreecommitdiffstats
path: root/logentry
blob: 0c1bd4114b6b0b79eadfd8cbbfd8c69b0809493e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/zsh
# Usage: logentry <category>
# 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 <category> [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"