when creating a note, if the title is not specified, two random words will be assigne...
[bash-notes.git] / notes.sh
CommitLineData
a4aaf855 1#! /bin/bash
2
6c152f7e 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
cb8fcb2f 6# to debug the script run it like:
7# DEBUG=true notes.sh ...
8# and check /tmp/debug_bash-notes.log
9if [[ $DEBUG == true ]]; then
10 exec 5> /tmp/debug_bash-notes.log
11 BASH_XTRACEFD="5"
12 PS4='$LINENO: '
13 set -x
14fi
a4aaf855 15
16PID=$$
ad818a9d 17BASENAME=$( basename "$0" )
18NOW=$(date +%s)
19
20VERSION="0.3"
21DBVERSION=${VERSION}_${NOW}
53f2ed57 22
d80ac20a 23set_defaults() {
24# Binaries to use
cb8fcb2f 25JQ=${JQ:-/usr/bin/jq}
a4aaf855 26EDITOR=${EDITOR:-/usr/bin/vim}
53f2ed57 27TERMINAL=${TERMINAL:-/usr/bin/alacritty}
b648c006 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
e3670e83 31TERM_OPTS="--class notes --title notes -e "
ad818a9d 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.
34PAGER=${PAGER:-/usr/bin/more}
53f2ed57 35
cb8fcb2f 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
38PLAIN=false
d80ac20a 39# base directory for program files
53f2ed57 40BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
d80ac20a 41# notes database in json format
a4aaf855 42DB=${BASEDIR}/db.json
d80ac20a 43# directory containing the actual notes
a4aaf855 44NOTESDIR=${BASEDIR}/notes
53f2ed57 45
d80ac20a 46} # end set_defaults, do not change this line.
47
48set_defaults
49
50# Do not edit below this point
51RCFILE=${RCFILE:-~/.config/bash-notes.rc}
53f2ed57 52TMPDB=/tmp/db.json
a4aaf855 53
b9f21021 54if [ ! -x "$JQ" ]; then
a4aaf855 55 echo "jq not found in your PATH"
56 echo "install jq to continue"
57 exit 1
58fi
59
53f2ed57 60# IMPORT USER DEFINED OPTIONS IF ANY
61if [[ -f $RCFILE ]]; then
b9f21021 62 # shellcheck disable=SC1090
63 source "$RCFILE"
53f2ed57 64fi
65
a4aaf855 66# We prevent the program from running more than one instance:
b9f21021 67PIDFILE=/var/tmp/$(basename "$0" .sh).pid
a4aaf855 68
69# Make sure the PID file is removed when we kill the process
70trap 'rm -f $PIDFILE; exit 1' TERM INT
71
72if [[ -r $PIDFILE ]]; then
73 # PIDFILE exists, so I guess there's already an instance running
74 # let's kill it and run again
b9f21021 75 # shellcheck disable=SC2046,SC2086
a4aaf855 76 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
77 # should already be deleted by trap, but just to be sure
b9f21021 78 rm "$PIDFILE"
a4aaf855 79fi
80
81# create PIDFILE
b9f21021 82echo $PID > "$PIDFILE"
a4aaf855 83
fb711183 84# Export config to file
85function 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.
106function firstrun() {
107 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
108
109 clear
110 echo "${BASENAME} configuration:
111
112base directory: ${BASEDIR}/
113notes archive: ${NOTESDIR}/
114notes database: ${DB}
115rc file: $RC
116text editor: ${EDITOR}
117terminal: ${TERMINAL}
118jq executable: ${JQ}
119"
120
9eb02251 121 echo "Now I'll create the needed files and directories."
fb711183 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
145if [[ ! -d $NOTESDIR ]]; then
146 # we don't have a directory. FIRST RUN?
147 firstrun
148fi
61c91990 149# check if input is a number, returns false or the number itself
150function check_noteID() {
151 IN=$1
152 case $IN in
153 ''|*[!0-9]*)
154 return 1
155 ;;
156 *)
b9f21021 157 echo "$IN"
61c91990 158 ;;
159 esac
160}
161
a4aaf855 162function helptext() {
d80ac20a 163 echo "Usage:"
efa3e607 164 echo " $0 [PARAMS] [note ID]..."
d80ac20a 165 echo ""
e3670e83 166 echo "${BASENAME} parameters are:"
9eb02251 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"
3951cc3d 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"
9eb02251 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"
e3670e83 180 echo ""
efa3e607 181 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
a4aaf855 182}
9eb02251 183
3951cc3d 184function configtext() {
185 cat << __NOWCONF__
186${BASENAME} configuration is:
187
188base directory: ${BASEDIR}/
189notes archive: ${NOTESDIR}/
190notes database: ${DB}
191rc file: $RCFILE
192debug file: /tmp/debug_bash-note.log
193
194text editor: ${EDITOR}
195terminal: ${TERMINAL}
196jq executable: ${JQ}
197PAGER: ${PAGER}
198__NOWCONF__
199
200}
201
6a193095 202# this function returns a random 2 words title
203function random_title() {
204 # Constants
205 X=0
206 DICT=/usr/share/dict/words
207 OUTPUT=""
208
209 # total number of non-random words available
210 COUNT=$(cat $DICT | wc -l)
211
212 # while loop to generate random words
213 while [ "$X" -lt 2 ]
214 do
215 RAND=$(od -N3 -An -i /dev/urandom | awk -v f=0 -v r="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}')
216 OUTPUT+="$(sed `echo $RAND`"q;d" $DICT)"
217 (("X = X + 1"))
218 [[ $X -eq 1 ]] && OUTPUT+=" "
219 done
220
221 echo $OUTPUT
222}
223
a4aaf855 224function addnote() {
026502da 225 # remove eventually existing temp DB file
226 if [[ -f $TMPDB ]]; then
227 rm $TMPDB
228 fi
229
6a193095 230 RTITLE=$(random_title)
231 [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
53f2ed57 232 echo "adding new note - \"$NOTETITLE\""
b9f21021 233 # shellcheck disable=SC2086
e3670e83 234 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
235 # [ "" == $LASTID ] && LASTID=0
b9f21021 236 NOTEID=$(( LASTID + 1 ))
237 # shellcheck disable=SC2086
a4aaf855 238 touch ${NOTESDIR}/${NOW}
b9f21021 239 # shellcheck disable=SC2016
a4aaf855 240 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
b9f21021 241 # shellcheck disable=SC2086
a4aaf855 242 mv $TMPDB $DB
b648c006 243 # example for alacritty:
244 # alacritty --class notes --title notes -e /usr/bin/vim ...
b9f21021 245 # shellcheck disable=SC2086,SC2091
e3670e83 246 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
a4aaf855 247}
9eb02251 248function backup_data() {
249 BACKUPDIR="$1"
250 echo "backing up data in $BACKUPDIR"
251
252
253 if [ -d $BACKUPDIR ]; then
254 if [ $(/bin/ls -A $BACKUPDIR) ]; then
255 echo "$BACKUPDIR is not empty. Cannot continue"
256 exit
257 else
258 echo "$BACKUPDIR is ok. Continuing!"
259 fi
260 else
261 # BACKUPDIR doesn't exists
262 echo "$BACKUPDIR doesn't exists"
263 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
264 case $ANSWER in
265 y|Y )
266 mkdir -p $BACKUPDIR
267 ;;
268 * )
269 echo "No changes made. Exiting"
270 exit
271 ;;
272 esac
273 fi
274 # ok, we have a backup directory
275 if [ -r $RCFILE ]; then
3951cc3d 276 BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/* ${BACKUPDIR})
9eb02251 277 else
278 BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/* ${BACKUPDIR})
279 fi
280 # run the command
281 if [ "$BCKUP_COMM" ]; then
3951cc3d 282 echo -e "All files backed up."
9eb02251 283 echo -e "BACKUP directory:\t$BACKUPDIR"
3951cc3d 284 tree $BACKUPDIR | $PAGER
9eb02251 285 echo; echo "BACKUP COMPLETED"
286 fi
287}
288
3951cc3d 289function backup_restore() {
290 BACKUPDIR="$1"
291 echo "restoring backup from $BACKUPDIR"
292 echo "This will overwrite all your notes and configurations with the backup."
293 read -r -p "Do you want to continue? (y/N) " ANSWER
294 case $ANSWER in
295 y|Y )
296 # restoring rc file
297 BACKUPRC=$(basename $RCFILE)
298 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
299 if [ -r ${RCFILE} ]; then
300 echo "Backing up current '${RCFILE}'...."
301 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
302 fi
303 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
304 fi
305 # restoring notes directory
306 if [ -d $BACKUPDIR/notes ]; then
307 if [ $(/bin/ls -A $NOTESDIR) ]; then
308 rm --verbose $NOTESDIR/*
309 fi
310 cp -r --verbose $BACKUPDIR/notes $BASEDIR
311 fi
312 # restoring database
313 BACKUPDB=$(basename $DB)
314 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
315 if [ -r ${DB} ]; then
316 echo "Backing up current '${DB}'...."
317 mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M)
318 fi
319 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
320 fi
321 ;;
322 * )
323 echo "No changes made. Exiting"
324 exit
325 ;;
326 esac
327}
328
a4aaf855 329function editnote() {
61c91990 330 NOTE=$1
b9f21021 331 # shellcheck disable=SC2155
332 local OK=$(check_noteID "$NOTE")
333 if [ ! "$OK" ]; then
61c91990 334 echo "invalid note \"$NOTE\""
b9f21021 335 echo "Use the note ID that you can fetch after listing your notes"
61c91990 336 exit 1
337 fi
338
b9f21021 339 # shellcheck disable=SC2016,SC2086
61c91990 340 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 341 # shellcheck disable=SC2016,SC2086
61c91990 342 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
44abbfe7 343 if [ "$TITLE" ]; then
344 echo "editing note $TITLE"
b9f21021 345 # shellcheck disable=SC2086,SC2091
b648c006 346 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
44abbfe7 347 else
348 echo "note not found"
349 exit 1
350 fi
a4aaf855 351}
fb711183 352function listnotes() {
353 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
354 if [[ $(ls -A "$NOTESDIR") ]]; then
355 if [ $PLAIN == false ]; then
356 echo "listing all notes"
357 echo ""
358 fi
359 [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
360 for i in "${NOTESDIR}"/*; do
361 # shellcheck disable=SC2155
362 local fname=$(basename $i)
363 DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
364 # shellcheck disable=SC2016,SC2086
365 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
366 # shellcheck disable=SC2016,SC2086
367 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
368 [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
369 done
370 else
371 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
372 fi
373}
a4aaf855 374function rmnote() {
026502da 375 # remove eventually existing temp DB file
376 if [[ -f $TMPDB ]]; then
377 rm $TMPDB
b648c006 378 fi
379
026502da 380 NOTE=$1
b9f21021 381 if [ "all" == "$NOTE" ]; then
026502da 382 echo "You're going to delete all notes."
383 read -r -p "Do you wish to continue? (y/N) " ANSWER
384 case $ANSWER in
385 y|Y )
b9f21021 386 # shellcheck disable=SC2086
026502da 387 $JQ 'del(.notes[])' $DB > $TMPDB
b9f21021 388 # shellcheck disable=SC2086
026502da 389 mv $TMPDB $DB
b9f21021 390 # shellcheck disable=SC2086
026502da 391 rm $NOTESDIR/*
392 echo "Deleted all notes"
393 ;;
394 * )
395 echo "Aborting, no notes were deleted."
396 exit 1
397 ;;
398 esac
b648c006 399 else
b9f21021 400 # shellcheck disable=SC2155
401 local OK=$(check_noteID "$NOTE")
402 if [ ! "$OK" ]; then
026502da 403 echo "invalid note \"$NOTE\""
b9f21021 404 echo "Use the note ID that you can fetch after listing your notes"
f1343f20 405 sleep 1
026502da 406 exit 1
407 fi
408
b9f21021 409 # shellcheck disable=SC2016,SC2086
026502da 410 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 411 # shellcheck disable=SC2016,SC2086
026502da 412 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
413 if [ "$TITLE" ]; then
ad818a9d 414 # shellcheck disable=SC2016,SC2086
415 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
416 # shellcheck disable=SC2086
417 mv $TMPDB $DB
418 rm $NOTESDIR/$FILE
419 echo "Deleted note $TITLE"
f1343f20 420 sleep 1
421 exit
026502da 422 else
423 echo "note not found"
f1343f20 424 sleep 1
026502da 425 exit 1
426 fi
b648c006 427 fi
a4aaf855 428}
ad818a9d 429function shownote() {
430 NOTE=$1
431
432 # shellcheck disable=SC2155
433 local OK=$(check_noteID "$NOTE")
434 if [ ! "$OK" ]; then
435 echo "invalid note \"$NOTE\""
436 echo "Use the note ID that you can fetch after listing your notes"
437 exit 1
438 fi
439
440 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
441
442 if [ "$FILE" ]; then
443 $PAGER ${NOTESDIR}/${FILE}
444 fi
445}
b9f21021 446# shellcheck disable=SC2006
3951cc3d 447GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
53f2ed57 448
b9f21021 449# shellcheck disable=SC2181
cb8fcb2f 450if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
53f2ed57 451
452# Note the quotes around `$GOPT': they are essential!
453eval set -- "$GOPT"
ad818a9d 454unset GOPT
53f2ed57 455
456while true; do
457 case "$1" in
458 -h | --help )
459 helptext
460 exit
461 ;;
462 -v | --version )
463 echo $BASENAME v${VERSION}
464 exit
465 ;;
c018122c 466 -p | --plain )
467 PLAIN=true
468 shift
469 ;;
53f2ed57 470 -l | --list )
471 listnotes
472 exit
473 ;;
474 -a | --add )
ad818a9d 475 case "$2" in
476 '' )
477 read -r -p "Title: " TITLE
478 ;;
479 * )
480 TITLE=$2
481 ;;
482 esac
53f2ed57 483 shift 2
484 addnote "$TITLE"
07d42c7a 485 exit
53f2ed57 486 ;;
6c152f7e 487 -e | --edit )
ad818a9d 488 case "$2" in
489 '' )
490 read -r -p "Note ID: " NOTE
491 ;;
492 * )
493 NOTE=$2
494 ;;
495 esac
53f2ed57 496 shift 2
497 editnote "$NOTE"
07d42c7a 498 exit
53f2ed57 499 ;;
b648c006 500 -d | --delete )
ad818a9d 501 case "$2" in
502 '' )
503 read -r -p "Note ID: " NOTE
504 ;;
505 * )
506 NOTE=$2
507 ;;
508 esac
53f2ed57 509 shift 2
510 rmnote "$NOTE"
07d42c7a 511 exit
53f2ed57 512 ;;
ad818a9d 513 -s | --show )
514 case "$2" in
515 '' )
516 read -r -p "Note ID: " NOTE
517 ;;
518 * )
519 NOTE=$2
520 ;;
521 esac
522 shift 2
523 shownote "$NOTE"
07d42c7a 524 exit
ad818a9d 525 ;;
3951cc3d 526 -r | --restore )
527 case "$2" in
528 '' )
529 read -r -p "Backup Dir: " RDIR
530 ;;
531 * )
532 RDIR=$2
533 ;;
534 esac
535 shift 2
536 backup_restore $RDIR
537 exit
538 ;;
d80ac20a 539 --userconf )
540 export_config
b9f21021 541 # shellcheck disable=SC2317
d80ac20a 542 echo "config exported to \"$RCFILE\""
b9f21021 543 # shellcheck disable=SC2317
d80ac20a 544 exit
53f2ed57 545 ;;
9eb02251 546 --backup )
547 case "$2" in
548 '' )
549 read -r -p "Backup Dir: " BDIR
550 ;;
551 * )
552 BDIR=$2
553 ;;
554 esac
555 shift 2
556 backup_data $BDIR
557 exit
558 ;;
53f2ed57 559 -- )
026502da 560 shift
561 break
53f2ed57 562 ;;
563 * )
564 break
565 ;;
566 esac
a4aaf855 567done
efa3e607 568
569for arg; do
570 if [ $(check_noteID $arg) ]; then
571 shownote $arg
572 else
573 helptext
574 exit
575 fi
576done