84e06b7425ed799d3e17cb2289624a88fa108cca
[bash-notes.git] / notes.sh
1 #! /bin/bash
2
3 # set -ex
4
5 PID=$$
6 VERSION="0.1"
7
8 EDITOR=${EDITOR:-/usr/bin/vim}
9 TERMINAL=${TERMINAL:-/usr/bin/alacritty}
10 JQ=$(which jq)
11
12 BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
13 RCFILE=${RCFILE:-~/.bash-notes.rc}
14 DB=${BASEDIR}/db.json
15 NOTESDIR=${BASEDIR}/notes
16
17 TMPDB=/tmp/db.json
18 BASENAME=$( basename $0 )
19
20 if [ ! -x $JQ ]; then
21 echo "jq not found in your PATH"
22 echo "install jq to continue"
23 exit 1
24 fi
25
26 # IMPORT USER DEFINED OPTIONS IF ANY
27 if [[ -f $RCFILE ]]; then
28 source $RCFILE
29 fi
30
31 # We prevent the program from running more than one instance:
32 PIDFILE=/var/tmp/$(basename $0 .sh).pid
33
34 # Make sure the PID file is removed when we kill the process
35 trap 'rm -f $PIDFILE; exit 1' TERM INT
36
37 if [[ -r $PIDFILE ]]; then
38 # PIDFILE exists, so I guess there's already an instance running
39 # let's kill it and run again
40 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
41 # should already be deleted by trap, but just to be sure
42 rm $PIDFILE
43 fi
44
45 # create PIDFILE
46 echo $PID > $PIDFILE
47
48 function helptext() {
49 echo "Parameters are:"
50 echo " -h : This help text"
51 echo " -s <directory> : specify directory where to store all notes."
52 echo " -e <editor> : specify EDITOR for this session only."
53 echo " -l : List existing notes"
54 echo " -a : Add new note"
55 echo " -m <note> : Modify note"
56 echo " -d <note> : Modify date for note"
57 echo " -r <note> : Remove note"
58 echo " -v : Print version"
59 }
60
61 function addnote() {
62 NOTETITLE="$1"
63 echo "adding new note - \"$NOTETITLE\""
64 NOW=$(date +%s)
65 LASTID=$($JQ '.notes[-1].id | tonumber' $DB)
66 [ null == $LASTID ] && LASTID=0
67 NOTEID=$(( $LASTID + 1 ))
68 touch ${NOTESDIR}/${NOW}
69 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
70 mv $TMPDB $DB
71 $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW})
72 }
73
74 function listnotes() {
75 echo "list all notes"
76 }
77
78 function editnote() {
79 echo "edit note \"${1}\""
80 }
81
82 function datenote() {
83 echo "edit date for note \"${1}\""
84 # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
85
86 }
87
88 function rmnote() {
89 NOTE=$1
90 echo "removing note $NOTE"
91 }
92
93 # we should expand on this function to add a sample note and explain a little bit
94 # how the program works.
95 function firstrun() {
96 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
97
98 clear
99 echo "${BASENAME} configuration:
100
101 base directory: ${BASEDIR}/
102 notes archive: ${NOTESDIR}/
103 notes database: ${DB}
104 rc file: $RC
105 text editor: ${EDITOR}
106 terminal: ${TERMINAL}
107 jq executable: ${JQ}
108 "
109
110 read -r -p "Do you wish to continue? (y/N) " ANSWER
111 case $ANSWER in
112 y|Y )
113 mkdir -p $NOTESDIR
114 cat << "__EOL__" > ${DB}
115 {
116 "notes": []
117 }
118 __EOL__
119 ;;
120 n|N )
121 echo "No changes made. Exiting"
122 exit
123 ;;
124 * )
125 echo "No changes made. Exiting"
126 exit
127 ;;
128 esac
129 }
130
131 # check for notes dir existance and create it in case it doesn't exists
132 if [[ ! -d $NOTESDIR ]]; then
133 # we don't have a directory. FIRST RUN?
134 firstrun
135 fi
136
137 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
138 # separately; see below.
139 GOPT=`getopt -o hvla:m:d:r: --long help,version,list,add:,modify:,date:,remove:,editor:,storage: \
140 -n 'bash-notes' -- "$@"`
141
142 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
143
144 # Note the quotes around `$GOPT': they are essential!
145 eval set -- "$GOPT"
146
147 while true; do
148 case "$1" in
149 -h | --help )
150 helptext
151 exit
152 ;;
153 -v | --version )
154 echo $BASENAME v${VERSION}
155 exit
156 ;;
157 -l | --list )
158 listnotes
159 exit
160 ;;
161 -a | --add )
162 TITLE="$2"
163 shift 2
164 addnote "$TITLE"
165 ;;
166 -m | --modify )
167 NOTE="$2"
168 shift 2
169 editnote "$NOTE"
170 ;;
171 -d | --date )
172 NOTE="$2"
173 shift 2
174 datenote "$NOTE"
175 ;;
176 -r | --remove )
177 NOTE="$2"
178 shift 2
179 rmnote "$NOTE"
180 ;;
181 --editor )
182 EDITOR="$2"
183 shift 2
184 echo "changed EDITOR TO \"$EDITOR\""
185 ;;
186 --storage )
187 BASEDIR="$2"
188 shift 2
189 echo "changed BASEDIR TO \"$BASEDIR\""
190 # firstrun
191 ;;
192 -- )
193 shift; break
194 ;;
195 * )
196 break
197 ;;
198 esac
199 done
200