Separated script into multiple files for easier management. Added Makefile
[bash-notes.git] / notes.sh
1 #! /bin/bash
2
3 # bash-notes © 2023 by danix is licensed under CC BY-NC 4.0.
4 # To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/
5
6 # to debug the script run it like:
7 # DEBUG=true notes.sh ...
8 # and check /tmp/debug_bash-notes.log
9 if [[ $DEBUG == true ]]; then
10 exec 5> /tmp/debug_bash-notes.log
11 BASH_XTRACEFD="5"
12 PS4='$LINENO: '
13 set -x
14 fi
15
16 PID=$$
17 BASENAME=$( basename "$0" )
18 NOW=$(date +%s)
19
20 VERSION="0.3"
21 DBVERSION=${VERSION}_${NOW}
22
23 set_defaults() {
24 # Binaries to use
25 JQ=${JQ:-/usr/bin/jq}
26 EDITOR=${EDITOR:-/usr/bin/vim}
27 TERMINAL=${TERMINAL:-/usr/bin/alacritty}
28 # add options for your terminal. Remember to add the last option to execute
29 # your editor program, otherwise the script will fail.
30 # see example in the addnote function
31 TERM_OPTS="--class notes --title notes -e "
32 # Setting PAGER here overrides whatever is set in your default shell
33 # comment this option to use your default pager if set in your shell.
34 PAGER=${PAGER:-/usr/bin/more}
35
36 # set this to true to have output in plain text
37 # or use the -p option on the command line before every other option
38 PLAIN=false
39 # base directory for program files
40 BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
41 # notes database in json format
42 DB=${BASEDIR}/db.json
43 # directory containing the actual notes
44 NOTESDIR=${BASEDIR}/notes
45
46 } # end set_defaults, do not change this line.
47
48 set_defaults
49
50 # Do not edit below this point
51 RCFILE=${RCFILE:-~/.config/bash-notes.rc}
52 TMPDB=/tmp/db.json
53
54 if [ ! -x "$JQ" ]; then
55 echo "jq not found in your PATH"
56 echo "install jq to continue"
57 exit 1
58 fi
59
60 # IMPORT USER DEFINED OPTIONS IF ANY
61 if [[ -f $RCFILE ]]; then
62 # shellcheck disable=SC1090
63 source "$RCFILE"
64 fi
65
66 # We prevent the program from running more than one instance:
67 PIDFILE=/var/tmp/$(basename "$0" .sh).pid
68
69 # Make sure the PID file is removed when we kill the process
70 trap 'rm -f $PIDFILE; exit 1' TERM INT
71
72 if [[ -r $PIDFILE ]]; then
73 # PIDFILE exists, so I guess there's already an instance running
74 # let's kill it and run again
75 # shellcheck disable=SC2046,SC2086
76 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
77 # should already be deleted by trap, but just to be sure
78 rm "$PIDFILE"
79 fi
80
81 # create PIDFILE
82 echo $PID > "$PIDFILE"
83
84 # Export config to file
85 function export_config() {
86 if [ -r ${RCFILE} ]; then
87 echo "Backing up current '${RCFILE}'...."
88 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
89 fi
90 echo "Writing '${RCFILE}'...."
91 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
92 | grep -v set_defaults \
93 | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
94 > ${RCFILE}
95 if [ -r ${RCFILE} ]; then
96 echo "Taking no further action."
97 exit 0
98 else
99 echo "Could not write '${RCFILE}'...!"
100 exit 1
101 fi
102 }
103
104 # we should expand on this function to add a sample note and explain a little bit
105 # how the program works.
106 function firstrun() {
107 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
108
109 clear
110 echo "${BASENAME} configuration:
111
112 base directory: ${BASEDIR}/
113 notes archive: ${NOTESDIR}/
114 notes database: ${DB}
115 rc file: $RC
116 text editor: ${EDITOR}
117 terminal: ${TERMINAL}
118 jq executable: ${JQ}
119 "
120
121 read -r -p "Do you wish to continue? (y/N) " ANSWER
122 case $ANSWER in
123 y|Y )
124 mkdir -p $NOTESDIR
125 cat << __EOL__ > ${DB}
126 {
127 "params": {
128 "version": "${VERSION}",
129 "dbversion": "${DBVERSION}"
130 },
131 "notes": []
132 }
133 __EOL__
134 echo; echo "All done, you can now write your first note."
135 ;;
136 * )
137 echo "No changes made. Exiting"
138 exit
139 ;;
140 esac
141 }
142
143 # check for notes dir existance and create it in case it doesn't exists
144 if [[ ! -d $NOTESDIR ]]; then
145 # we don't have a directory. FIRST RUN?
146 firstrun
147 fi
148 # check if input is a number, returns false or the number itself
149 function check_noteID() {
150 IN=$1
151 case $IN in
152 ''|*[!0-9]*)
153 return 1
154 ;;
155 *)
156 echo "$IN"
157 ;;
158 esac
159 }
160
161 function helptext() {
162 echo "Usage:"
163 echo " $0 [PARAMS] ..."
164 echo ""
165 cat << __NOWCONF__
166 ${BASENAME} configuration is:
167
168 base directory: ${BASEDIR}/
169 notes archive: ${NOTESDIR}/
170 notes database: ${DB}
171 rc file: $RCFILE
172 debug file: /tmp/debug_bash-note.log
173
174 text editor: ${EDITOR}
175 terminal: ${TERMINAL}
176 jq executable: ${JQ}
177 __NOWCONF__
178
179 echo ""
180 echo "${BASENAME} parameters are:"
181 echo " -h | --help : This help text"
182 echo " -p | --plain : Output is in plain text"
183 echo " (without this option the output is formatted)"
184 echo " (this option must precede all others)"
185 echo " -l | --list : List existing notes"
186 echo " -a | --add [\"<title>\"] : Add new note"
187 echo " -e | --edit [<note>] : Edit note"
188 echo " -d | --delete [<note> | all] : Delete single note or all notes at once"
189 echo " -s | --show [<note>] : Display note using your favourite PAGER"
190 echo " -v | --version : Print version"
191 echo " --userconf : Export User config file"
192 echo ""
193 }
194 function addnote() {
195 # remove eventually existing temp DB file
196 if [[ -f $TMPDB ]]; then
197 rm $TMPDB
198 fi
199
200 NOTETITLE="$1"
201 echo "adding new note - \"$NOTETITLE\""
202 # shellcheck disable=SC2086
203 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
204 # [ "" == $LASTID ] && LASTID=0
205 NOTEID=$(( LASTID + 1 ))
206 # shellcheck disable=SC2086
207 touch ${NOTESDIR}/${NOW}
208 # shellcheck disable=SC2016
209 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
210 # shellcheck disable=SC2086
211 mv $TMPDB $DB
212 # example for alacritty:
213 # alacritty --class notes --title notes -e /usr/bin/vim ...
214 # shellcheck disable=SC2086,SC2091
215 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
216 }
217 function editnote() {
218 NOTE=$1
219 # shellcheck disable=SC2155
220 local OK=$(check_noteID "$NOTE")
221 if [ ! "$OK" ]; then
222 echo "invalid note \"$NOTE\""
223 echo "Use the note ID that you can fetch after listing your notes"
224 exit 1
225 fi
226
227 # shellcheck disable=SC2016,SC2086
228 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
229 # shellcheck disable=SC2016,SC2086
230 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
231 if [ "$TITLE" ]; then
232 echo "editing note $TITLE"
233 # shellcheck disable=SC2086,SC2091
234 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
235 else
236 echo "note not found"
237 exit 1
238 fi
239 }
240 function listnotes() {
241 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
242 if [[ $(ls -A "$NOTESDIR") ]]; then
243 if [ $PLAIN == false ]; then
244 echo "listing all notes"
245 echo ""
246 fi
247 [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
248 for i in "${NOTESDIR}"/*; do
249 # shellcheck disable=SC2155
250 local fname=$(basename $i)
251 DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
252 # shellcheck disable=SC2016,SC2086
253 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
254 # shellcheck disable=SC2016,SC2086
255 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
256 [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
257 done
258 else
259 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
260 fi
261 }
262 function rmnote() {
263 # remove eventually existing temp DB file
264 if [[ -f $TMPDB ]]; then
265 rm $TMPDB
266 fi
267
268 NOTE=$1
269 if [ "all" == "$NOTE" ]; then
270 echo "You're going to delete all notes."
271 read -r -p "Do you wish to continue? (y/N) " ANSWER
272 case $ANSWER in
273 y|Y )
274 # shellcheck disable=SC2086
275 $JQ 'del(.notes[])' $DB > $TMPDB
276 # shellcheck disable=SC2086
277 mv $TMPDB $DB
278 # shellcheck disable=SC2086
279 rm $NOTESDIR/*
280 echo "Deleted all notes"
281 ;;
282 * )
283 echo "Aborting, no notes were deleted."
284 exit 1
285 ;;
286 esac
287 else
288 # shellcheck disable=SC2155
289 local OK=$(check_noteID "$NOTE")
290 if [ ! "$OK" ]; then
291 echo "invalid note \"$NOTE\""
292 echo "Use the note ID that you can fetch after listing your notes"
293 exit 1
294 fi
295
296 # shellcheck disable=SC2016,SC2086
297 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
298 # shellcheck disable=SC2016,SC2086
299 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
300 if [ "$TITLE" ]; then
301 # shellcheck disable=SC2016,SC2086
302 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
303 # shellcheck disable=SC2086
304 mv $TMPDB $DB
305 rm $NOTESDIR/$FILE
306 echo "Deleted note $TITLE"
307 else
308 echo "note not found"
309 exit 1
310 fi
311 fi
312 }
313 function shownote() {
314 NOTE=$1
315
316 # shellcheck disable=SC2155
317 local OK=$(check_noteID "$NOTE")
318 if [ ! "$OK" ]; then
319 echo "invalid note \"$NOTE\""
320 echo "Use the note ID that you can fetch after listing your notes"
321 exit 1
322 fi
323
324 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
325
326 if [ "$FILE" ]; then
327 $PAGER ${NOTESDIR}/${FILE}
328 fi
329 }
330 # shellcheck disable=SC2006
331 GOPT=$(getopt -o hvpla::e::d::s:: --long help,version,list,plain,userconf,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
332
333 # shellcheck disable=SC2181
334 if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
335
336 # Note the quotes around `$GOPT': they are essential!
337 eval set -- "$GOPT"
338 unset GOPT
339
340 while true; do
341 case "$1" in
342 -h | --help )
343 helptext
344 exit
345 ;;
346 -v | --version )
347 echo $BASENAME v${VERSION}
348 exit
349 ;;
350 -p | --plain )
351 PLAIN=true
352 shift
353 ;;
354 -l | --list )
355 listnotes
356 exit
357 ;;
358 -a | --add )
359 case "$2" in
360 '' )
361 read -r -p "Title: " TITLE
362 ;;
363 * )
364 TITLE=$2
365 ;;
366 esac
367 shift 2
368 addnote "$TITLE"
369 ;;
370 -e | --edit )
371 case "$2" in
372 '' )
373 read -r -p "Note ID: " NOTE
374 ;;
375 * )
376 NOTE=$2
377 ;;
378 esac
379 shift 2
380 editnote "$NOTE"
381 ;;
382 -d | --delete )
383 case "$2" in
384 '' )
385 read -r -p "Note ID: " NOTE
386 ;;
387 * )
388 NOTE=$2
389 ;;
390 esac
391 shift 2
392 rmnote "$NOTE"
393 ;;
394 -s | --show )
395 case "$2" in
396 '' )
397 read -r -p "Note ID: " NOTE
398 ;;
399 * )
400 NOTE=$2
401 ;;
402 esac
403 shift 2
404 shownote "$NOTE"
405 ;;
406 --userconf )
407 export_config
408 # shellcheck disable=SC2317
409 echo "config exported to \"$RCFILE\""
410 # shellcheck disable=SC2317
411 exit
412 ;;
413 -- )
414 shift
415 break
416 ;;
417 * )
418 break
419 ;;
420 esac
421 done
422
423 if [ -z $1 ]; then
424 helptext
425 fi