8a9c082237a31b84c5ec43f454811224d0610da1
[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 echo "Now I'll create the needed files and directories."
122 read -r -p "Do you wish to continue? (y/N) " ANSWER
123 case $ANSWER in
124 y|Y )
125 mkdir -p $NOTESDIR
126 cat << __EOL__ > ${DB}
127 {
128 "params": {
129 "version": "${VERSION}",
130 "dbversion": "${DBVERSION}"
131 },
132 "notes": []
133 }
134 __EOL__
135 echo; echo "All done, you can now write your first note."
136 ;;
137 * )
138 echo "No changes made. Exiting"
139 exit
140 ;;
141 esac
142 }
143
144 # check for notes dir existance and create it in case it doesn't exists
145 if [[ ! -d $NOTESDIR ]]; then
146 # we don't have a directory. FIRST RUN?
147 firstrun
148 fi
149 # check if input is a number, returns false or the number itself
150 function check_noteID() {
151 IN=$1
152 case $IN in
153 ''|*[!0-9]*)
154 return 1
155 ;;
156 *)
157 echo "$IN"
158 ;;
159 esac
160 }
161
162 function helptext() {
163 echo "Usage:"
164 echo " $0 [PARAMS] [note ID]..."
165 echo ""
166 echo "${BASENAME} parameters are:"
167 echo -e " -h | --help\t\t\t: This help text"
168 echo -e " -p | --plain\t\t\t: Output is in plain text"
169 echo -e "\t\t\t\t (without this option the output is formatted)"
170 echo -e "\t\t\t\t (this option must precede all others)"
171 echo -e " -l | --list\t\t\t: List existing notes"
172 echo -e " -a | --add=[\"<title>\"]\t: Add new note"
173 echo -e " -e | --edit=[<note>]\t\t: Edit note"
174 echo -e " -d | --delete=[<note> | all] : Delete single note or all notes at once"
175 echo -e " -s | --show=[<note>]\t\t: Display note using your favourite PAGER"
176 echo -e " -r | --restore=[<dir>]\t: Restore a previous backup from dir"
177 echo -e " -v | --version\t\t: Print version"
178 echo -e " --userconf\t\t\t: Export User config file"
179 echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder"
180 echo ""
181 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
182 }
183
184 function configtext() {
185 cat << __NOWCONF__
186 ${BASENAME} configuration is:
187
188 base directory: ${BASEDIR}/
189 notes archive: ${NOTESDIR}/
190 notes database: ${DB}
191 rc file: $RCFILE
192 debug file: /tmp/debug_bash-note.log
193
194 text editor: ${EDITOR}
195 terminal: ${TERMINAL}
196 jq executable: ${JQ}
197 PAGER: ${PAGER}
198 __NOWCONF__
199
200 }
201
202 function addnote() {
203 # remove eventually existing temp DB file
204 if [[ -f $TMPDB ]]; then
205 rm $TMPDB
206 fi
207
208 NOTETITLE="$1"
209 echo "adding new note - \"$NOTETITLE\""
210 # shellcheck disable=SC2086
211 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
212 # [ "" == $LASTID ] && LASTID=0
213 NOTEID=$(( LASTID + 1 ))
214 # shellcheck disable=SC2086
215 touch ${NOTESDIR}/${NOW}
216 # shellcheck disable=SC2016
217 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
218 # shellcheck disable=SC2086
219 mv $TMPDB $DB
220 # example for alacritty:
221 # alacritty --class notes --title notes -e /usr/bin/vim ...
222 # shellcheck disable=SC2086,SC2091
223 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
224 }
225 function backup_data() {
226 BACKUPDIR="$1"
227 echo "backing up data in $BACKUPDIR"
228
229
230 if [ -d $BACKUPDIR ]; then
231 if [ $(/bin/ls -A $BACKUPDIR) ]; then
232 echo "$BACKUPDIR is not empty. Cannot continue"
233 exit
234 else
235 echo "$BACKUPDIR is ok. Continuing!"
236 fi
237 else
238 # BACKUPDIR doesn't exists
239 echo "$BACKUPDIR doesn't exists"
240 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
241 case $ANSWER in
242 y|Y )
243 mkdir -p $BACKUPDIR
244 ;;
245 * )
246 echo "No changes made. Exiting"
247 exit
248 ;;
249 esac
250 fi
251 # ok, we have a backup directory
252 if [ -r $RCFILE ]; then
253 BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/* ${BACKUPDIR})
254 else
255 BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/* ${BACKUPDIR})
256 fi
257 # run the command
258 if [ "$BCKUP_COMM" ]; then
259 echo -e "All files backed up."
260 echo -e "BACKUP directory:\t$BACKUPDIR"
261 tree $BACKUPDIR | $PAGER
262 echo; echo "BACKUP COMPLETED"
263 fi
264 }
265
266 function backup_restore() {
267 BACKUPDIR="$1"
268 echo "restoring backup from $BACKUPDIR"
269 echo "This will overwrite all your notes and configurations with the backup."
270 read -r -p "Do you want to continue? (y/N) " ANSWER
271 case $ANSWER in
272 y|Y )
273 # restoring rc file
274 BACKUPRC=$(basename $RCFILE)
275 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
276 if [ -r ${RCFILE} ]; then
277 echo "Backing up current '${RCFILE}'...."
278 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
279 fi
280 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
281 fi
282 # restoring notes directory
283 if [ -d $BACKUPDIR/notes ]; then
284 if [ $(/bin/ls -A $NOTESDIR) ]; then
285 rm --verbose $NOTESDIR/*
286 fi
287 cp -r --verbose $BACKUPDIR/notes $BASEDIR
288 fi
289 # restoring database
290 BACKUPDB=$(basename $DB)
291 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
292 if [ -r ${DB} ]; then
293 echo "Backing up current '${DB}'...."
294 mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M)
295 fi
296 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
297 fi
298 ;;
299 * )
300 echo "No changes made. Exiting"
301 exit
302 ;;
303 esac
304 }
305
306 function editnote() {
307 NOTE=$1
308 # shellcheck disable=SC2155
309 local OK=$(check_noteID "$NOTE")
310 if [ ! "$OK" ]; then
311 echo "invalid note \"$NOTE\""
312 echo "Use the note ID that you can fetch after listing your notes"
313 exit 1
314 fi
315
316 # shellcheck disable=SC2016,SC2086
317 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
318 # shellcheck disable=SC2016,SC2086
319 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
320 if [ "$TITLE" ]; then
321 echo "editing note $TITLE"
322 # shellcheck disable=SC2086,SC2091
323 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
324 else
325 echo "note not found"
326 exit 1
327 fi
328 }
329 function listnotes() {
330 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
331 if [[ $(ls -A "$NOTESDIR") ]]; then
332 if [ $PLAIN == false ]; then
333 echo "listing all notes"
334 echo ""
335 fi
336 [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
337 for i in "${NOTESDIR}"/*; do
338 # shellcheck disable=SC2155
339 local fname=$(basename $i)
340 DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
341 # shellcheck disable=SC2016,SC2086
342 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
343 # shellcheck disable=SC2016,SC2086
344 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
345 [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
346 done
347 else
348 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
349 fi
350 }
351 function rmnote() {
352 # remove eventually existing temp DB file
353 if [[ -f $TMPDB ]]; then
354 rm $TMPDB
355 fi
356
357 NOTE=$1
358 if [ "all" == "$NOTE" ]; then
359 echo "You're going to delete all notes."
360 read -r -p "Do you wish to continue? (y/N) " ANSWER
361 case $ANSWER in
362 y|Y )
363 # shellcheck disable=SC2086
364 $JQ 'del(.notes[])' $DB > $TMPDB
365 # shellcheck disable=SC2086
366 mv $TMPDB $DB
367 # shellcheck disable=SC2086
368 rm $NOTESDIR/*
369 echo "Deleted all notes"
370 ;;
371 * )
372 echo "Aborting, no notes were deleted."
373 exit 1
374 ;;
375 esac
376 else
377 # shellcheck disable=SC2155
378 local OK=$(check_noteID "$NOTE")
379 if [ ! "$OK" ]; then
380 echo "invalid note \"$NOTE\""
381 echo "Use the note ID that you can fetch after listing your notes"
382 exit 1
383 fi
384
385 # shellcheck disable=SC2016,SC2086
386 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
387 # shellcheck disable=SC2016,SC2086
388 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
389 if [ "$TITLE" ]; then
390 # shellcheck disable=SC2016,SC2086
391 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
392 # shellcheck disable=SC2086
393 mv $TMPDB $DB
394 rm $NOTESDIR/$FILE
395 echo "Deleted note $TITLE"
396 else
397 echo "note not found"
398 exit 1
399 fi
400 fi
401 }
402 function shownote() {
403 NOTE=$1
404
405 # shellcheck disable=SC2155
406 local OK=$(check_noteID "$NOTE")
407 if [ ! "$OK" ]; then
408 echo "invalid note \"$NOTE\""
409 echo "Use the note ID that you can fetch after listing your notes"
410 exit 1
411 fi
412
413 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
414
415 if [ "$FILE" ]; then
416 $PAGER ${NOTESDIR}/${FILE}
417 fi
418 }
419 # shellcheck disable=SC2006
420 GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
421
422 # shellcheck disable=SC2181
423 if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
424
425 # Note the quotes around `$GOPT': they are essential!
426 eval set -- "$GOPT"
427 unset GOPT
428
429 while true; do
430 case "$1" in
431 -h | --help )
432 helptext
433 exit
434 ;;
435 -v | --version )
436 echo $BASENAME v${VERSION}
437 exit
438 ;;
439 -p | --plain )
440 PLAIN=true
441 shift
442 ;;
443 -l | --list )
444 listnotes
445 exit
446 ;;
447 -a | --add )
448 case "$2" in
449 '' )
450 read -r -p "Title: " TITLE
451 ;;
452 * )
453 TITLE=$2
454 ;;
455 esac
456 shift 2
457 addnote "$TITLE"
458 exit
459 ;;
460 -e | --edit )
461 case "$2" in
462 '' )
463 read -r -p "Note ID: " NOTE
464 ;;
465 * )
466 NOTE=$2
467 ;;
468 esac
469 shift 2
470 editnote "$NOTE"
471 exit
472 ;;
473 -d | --delete )
474 case "$2" in
475 '' )
476 read -r -p "Note ID: " NOTE
477 ;;
478 * )
479 NOTE=$2
480 ;;
481 esac
482 shift 2
483 rmnote "$NOTE"
484 exit
485 ;;
486 -s | --show )
487 case "$2" in
488 '' )
489 read -r -p "Note ID: " NOTE
490 ;;
491 * )
492 NOTE=$2
493 ;;
494 esac
495 shift 2
496 shownote "$NOTE"
497 exit
498 ;;
499 -r | --restore )
500 case "$2" in
501 '' )
502 read -r -p "Backup Dir: " RDIR
503 ;;
504 * )
505 RDIR=$2
506 ;;
507 esac
508 shift 2
509 backup_restore $RDIR
510 exit
511 ;;
512 --userconf )
513 export_config
514 # shellcheck disable=SC2317
515 echo "config exported to \"$RCFILE\""
516 # shellcheck disable=SC2317
517 exit
518 ;;
519 --backup )
520 case "$2" in
521 '' )
522 read -r -p "Backup Dir: " BDIR
523 ;;
524 * )
525 BDIR=$2
526 ;;
527 esac
528 shift 2
529 backup_data $BDIR
530 exit
531 ;;
532 -- )
533 shift
534 break
535 ;;
536 * )
537 break
538 ;;
539 esac
540 done
541
542 for arg; do
543 if [ $(check_noteID $arg) ]; then
544 shownote $arg
545 else
546 helptext
547 exit
548 fi
549 done