-
Notifications
You must be signed in to change notification settings - Fork 0
/
journaler.sh
executable file
·78 lines (70 loc) · 2.02 KB
/
journaler.sh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
cd "$(dirname "$0")"
config() {
#Grab value from config file for first arg of function ($1)
#Returns default for arg if not found
val=$((grep -E "^$1=" config.conf 2>/dev/null || echo "$1=__DEFAULT__") | head -n 1 | cut -d '=' -f 2-)
if [[ $val == __DEFAULT__ ]]
then
case $1 in
texteditor)
echo -n "vim"
;;
esac
else
echo -n $val
fi
}
DAY=$(date +%d)
MONTH=$(date +%b)
YEAR=$(date +%Y)
FULL_DATE=$(date +%d-%m-%Y)
ENTRY_FILE="./$YEAR/$MONTH/$DAY-$MONTH.txt"
#Check for directories and files
if [ ! -d "./$YEAR" ]; then
mkdir "./$YEAR"
fi
if [ ! -d "./$YEAR/$MONTH" ]; then
mkdir "./$YEAR/$MONTH"
fi
if [ ! -f "$ENTRY_FILE" ]; then
echo "- $DAY $MONTH -" > $ENTRY_FILE
echo "Entry for $FULL_DATE created"
fi
#Ask user for their preferred text editor
if [ ! -f "./config.conf" ] || [ "$1" = "--change-editor" ]; then
while :
do
read -p 'Choose a default text editor command: ' TEXTEDITOR
if hash $TEXTEDITOR 2>/dev/null;
then
break
fi
echo 'Unable to use provided command, please try again.'
done
echo "texteditor=$TEXTEDITOR" > config.conf
fi
#Get a specific journal entry
if [ "$1" = "-d" ]; then
#Read requested date from input arguments
#Split into DATE_ARR of length 3
IFS='-'
read -ra DATE_ARR <<< "$2"
MONTHS=(ZERO Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
MONTH_STRING=${MONTHS[${DATE_ARR[1]}]}
if [ -f "./${DATE_ARR[2]}/$MONTH_STRING/${DATE_ARR[0]}-$MONTH_STRING.txt" ];
then
DAY=${DATE_ARR[0]}
MONTH=${DATE_ARR[1]}
YEAR=${DATE_ARR[2]}
echo "Found requested journal entry $DAY-$MONTH-$YEAR"
FULL_DATE=$DAY-$MONTH-$YEAR
ENTRY_FILE="./$YEAR/$MONTH_STRING/$DAY-$MONTH_STRING.txt"
else
echo 'Unable to find requested journal entry'
fi
fi
echo "Opening entry for $FULL_DATE"
TIME=$(date +%H:%M)
printf "\n$TIME:" >> $ENTRY_FILE
$(config texteditor) "$ENTRY_FILE"