added showconf option. Fixed bug that prevented the rc file from being correctly...
[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.4git"
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 # Git binary only used if $USEGIT is true - See below
29 GIT=${GIT:-/usr/bin/git}
30 # add options for your terminal. Remember to add the last option to execute
31 # your editor program, otherwise the script will fail.
32 # see example in the addnote function
33 TERM_OPTS="--class notes --title notes -e "
34 # Setting PAGER here overrides whatever is set in your default shell
35 # comment this option to use your default pager if set in your shell.
36 PAGER=${PAGER:-/usr/bin/more}
37
38 # set this to true to have output in plain text
39 # or use the -p option on the command line before every other option
40 PLAIN=false
41 # base directory for program files
42 BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
43 # notes database in json format
44 DB=${BASEDIR}/db.json
45 # directory containing the actual notes
46 NOTESDIR=${BASEDIR}/notes
47
48 ### GIT SUPPORT
49
50 # If you want to store your notes in a git repository set this to true
51 USEGIT=true
52 # Address of your remote repository
53 GITREMOTE=${GITREMOTE:-""}
54 # How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour)
55 GITSYNCDELAY=${GITSYNCDELAY:-"3600"}
56 # The name of this client. Defaults to the output of hostname
57 GITCLIENT=${GITCLIENT:-""}
58
59 } # end set_defaults, do not change this line.
60
61 set_defaults
62
63 # Do not edit below this point
64 RCFILE=${RCFILE:-~/.config/bash-notes.rc}
65 TMPDB=/tmp/db.json
66
67 if [ ! -x "$JQ" ]; then
68 echo "jq not found in your PATH"
69 echo "install jq to continue"
70 exit 1
71 fi
72
73 # IMPORT USER DEFINED OPTIONS IF ANY
74 if [[ -f $RCFILE ]]; then
75 # shellcheck disable=SC1090
76 source "$RCFILE"
77 fi
78
79 # We prevent the program from running more than one instance:
80 PIDFILE=/var/tmp/$(basename "$0" .sh).pid
81
82 # Make sure the PID file is removed when we kill the process
83 trap 'rm -f $PIDFILE; exit 1' TERM INT
84
85 if [[ -r $PIDFILE ]]; then
86 # PIDFILE exists, so I guess there's already an instance running
87 # let's kill it and run again
88 # shellcheck disable=SC2046,SC2086
89 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
90 # should already be deleted by trap, but just to be sure
91 rm "$PIDFILE"
92 fi
93
94 # create PIDFILE
95 echo $PID > "$PIDFILE"
96
97 # Export config to file
98 export_config() {
99 if [ -r ${RCFILE} ]; then
100 echo "Backing up current '${RCFILE}'...."
101 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
102 fi
103 echo "Writing '${RCFILE}'...."
104 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
105 | grep -v set_defaults \
106 | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
107 > ${RCFILE}
108 if [ -r ${RCFILE} ]; then
109 echo "Taking no further action."
110 exit 0
111 else
112 echo "Could not write '${RCFILE}'...!"
113 exit 1
114 fi
115 }
116
117 # we should expand on this function to add a sample note and explain a little bit
118 # how the program works.
119 firstrun() {
120 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
121
122 clear
123 echo "${BASENAME} configuration:
124
125 base directory: ${BASEDIR}/
126 notes archive: ${NOTESDIR}/
127 notes database: ${DB}
128 rc file: $RC
129 text editor: ${EDITOR}
130 terminal: ${TERMINAL}
131 jq executable: ${JQ}
132 "
133
134 echo "Now I'll create the needed files and directories."
135 read -r -p "Do you wish to continue? (y/N) " ANSWER
136 case $ANSWER in
137 y|Y )
138 mkdir -p $NOTESDIR
139 cat << __EOL__ > ${DB}
140 {
141 "params": {
142 "version": "${VERSION}",
143 "dbversion": "${DBVERSION}"
144 },
145 "git": {
146 "lastpull": ""
147 },
148 "notes": []
149 }
150 __EOL__
151 echo; echo "All done, you can now write your first note."
152 ;;
153 * )
154 echo "No changes made. Exiting"
155 exit
156 ;;
157 esac
158 }
159
160 # check for notes dir existance and create it in case it doesn't exists
161 if [[ ! -d $NOTESDIR ]]; then
162 # we don't have a directory. FIRST RUN?
163 firstrun
164 fi
165 # check if input is a number, returns false or the number itself
166 check_noteID() {
167 IN=$1
168 case $IN in
169 ''|*[!0-9]*)
170 return 1
171 ;;
172 *)
173 echo "$IN"
174 ;;
175 esac
176 }
177
178 helptext() {
179 echo "Usage:"
180 echo " $0 [PARAMS] [note ID]..."
181 echo ""
182 echo "${BASENAME} parameters are:"
183 echo -e " -h | --help\t\t\t: This help text"
184 echo -e " -p | --plain\t\t\t: Output is in plain text"
185 echo -e "\t\t\t\t (without this option the output is formatted)"
186 echo -e "\t\t\t\t (this option must precede all others)"
187 echo -e " -l | --list\t\t\t: List existing notes"
188 echo -e " -a | --add=[\"<title>\"]\t: Add new note"
189 echo -e " -e | --edit=[<note>]\t\t: Edit note"
190 echo -e " -d | --delete=[<note> | all] : Delete single note or all notes at once"
191 echo -e " -s | --show=[<note>]\t\t: Display note using your favourite PAGER"
192 echo -e " -r | --restore=[<dir>]\t: Restore a previous backup from dir"
193 echo -e " -v | --version\t\t: Print version"
194 echo -e " --userconf\t\t\t: Export User config file"
195 echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder"
196 echo ""
197 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
198 }
199
200 configtext() {
201 [ $USEGIT ] && GITUSE="enabled" || GITUSE="disabled"
202 clear
203 echo -e "${BASENAME} configuration is:"
204
205 echo -e "base directory: ${BASEDIR}/"
206 echo -e "notes archive: ${NOTESDIR}/"
207 echo -e "notes database: ${DB}"
208 echo -e "rc file: $RCFILE"
209 echo -e "debug file: /tmp/debug_bash-note.log"
210
211 echo -e "text editor: ${EDITOR}"
212 echo -e "terminal: ${TERMINAL}"
213 echo -e "jq executable: ${JQ}"
214 echo -e "PAGER: ${PAGER}"
215
216 echo -e "GIT use: ${GITUSE}"
217 echo -e "GIT remote: ${GITREMOTE}"
218 echo -e "GIT sync delay: ${GITSYNCDELAY}"
219 }
220
221 # this function returns a random 2 words title
222 random_title() {
223 # Constants
224 X=0
225 DICT=/usr/share/dict/words
226 OUTPUT=""
227
228 # total number of non-random words available
229 COUNT=$(cat $DICT | wc -l)
230
231 # while loop to generate random words
232 while [ "$X" -lt 2 ]
233 do
234 RAND=$(od -N3 -An -i /dev/urandom | awk -v f=0 -v r="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}')
235 OUTPUT+="$(sed `echo $RAND`"q;d" $DICT)"
236 (("X = X + 1"))
237 [[ $X -eq 1 ]] && OUTPUT+=" "
238 done
239
240 echo $OUTPUT
241 }
242
243 # check if GITCLIENT has been set or set it to the output of hostname
244 if [ -z "$GITCLIENT" ]; then
245 GITCLIENT=$( hostname )
246 fi
247 # returns true if the argument provided directory is a git repository
248 is_git_repo() {
249 DIR=$1
250 if [[ -d $DIR ]]; then
251 cd $DIR
252 if git rev-parse 2>/dev/null; then
253 true
254 else
255 false
256 fi
257 fi
258 }
259
260 # sync local repository to remote
261 # accepts -f parameter to skip last sync check
262 gitsync() {
263 FORCE=$1
264 if [[ $USEGIT && -n $GITREMOTE ]]; then
265 [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\""
266 NOWSYNC=$(date +%s)
267 if [[ $FORCE == "-f" ]]; then
268 $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
269 mv $TMPDB $DB
270 cd $BASEDIR
271 $GIT pull
272 else
273 # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time.
274 LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB")
275 SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} ))
276 if (( $SYNCDIFF > $GITSYNCDELAY )); then
277 #more than our delay time has passed. We can sync again.
278 $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
279 mv $TMPDB $DB
280 cd $BASEDIR
281 $GIT pull
282 else
283 # Last synced less than $GITSYNCDELAY seconds ago. We shall wait
284 [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait"
285 fi
286 fi
287 else
288 # no git, so we just keep going
289 true
290 fi
291 }
292
293 # add note to git and push it to remote
294 gitadd() {
295 if [[ $USEGIT && -n $GITREMOTE ]]; then
296 [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\""
297 cd $BASEDIR
298 $GIT add .
299 $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}"
300 $GIT push origin master
301 else
302 # no git, so we just keep going
303 true
304 fi
305 }
306
307 # check for USEGIT and subsequent variables
308 if [[ $USEGIT && -n $GITREMOTE ]]; then
309 # GIT is a go.
310 if ! is_git_repo $BASEDIR; then
311 # initializing git repository
312 cd $BASEDIR
313 $GIT init
314 echo "adding all files to git"
315 $GIT add .
316 $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}"
317 $GIT remote add origin $GITREMOTE
318 $GIT push -u origin master
319 fi
320 elif [[ $USEGIT && -z $GITREMOTE ]]; then
321 echo "GITREMOTE variable not set. reverting USEGIT to false"
322 USEGIT=false
323 fi
324
325 addnote() {
326 # attempt syncing before adding a note
327 gitsync -f
328 # remove eventually existing temp DB file
329 if [[ -f $TMPDB ]]; then
330 rm $TMPDB
331 fi
332
333 RTITLE=$(random_title)
334 [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
335 echo "adding new note - \"$NOTETITLE\""
336 # shellcheck disable=SC2086
337 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
338 # [ "" == $LASTID ] && LASTID=0
339 NOTEID=$(( LASTID + 1 ))
340 # shellcheck disable=SC2086
341 touch ${NOTESDIR}/${NOW}
342 # shellcheck disable=SC2016
343 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
344 # shellcheck disable=SC2086
345 mv $TMPDB $DB
346 # example for alacritty:
347 # alacritty --class notes --title notes -e /usr/bin/vim ...
348 # shellcheck disable=SC2086,SC2091
349 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
350 # add note to git and push to remote
351 gitadd
352 }
353 backup_data() {
354 BACKUPDIR="$1"
355 echo "backing up data in $BACKUPDIR"
356
357
358 if [ -d $BACKUPDIR ]; then
359 if [ $(/bin/ls -A $BACKUPDIR) ]; then
360 echo "$BACKUPDIR is not empty. Cannot continue"
361 exit
362 else
363 echo "$BACKUPDIR is ok. Continuing!"
364 fi
365 else
366 # BACKUPDIR doesn't exists
367 echo "$BACKUPDIR doesn't exists"
368 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
369 case $ANSWER in
370 y|Y )
371 mkdir -p $BACKUPDIR
372 ;;
373 * )
374 echo "No changes made. Exiting"
375 exit
376 ;;
377 esac
378 fi
379 # ok, we have a backup directory
380 if [ -r $RCFILE ]; then
381 BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
382 else
383 BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR})
384 fi
385 # run the command
386 if [ "$BCKUP_COMM" ]; then
387 echo -e "All files backed up."
388 echo -e "BACKUP directory:\t$BACKUPDIR"
389 tree $BACKUPDIR | $PAGER
390 echo; echo "BACKUP COMPLETED"
391 fi
392 }
393
394 backup_restore() {
395 BACKUPDIR="$1"
396 echo "restoring backup from $BACKUPDIR"
397 echo "This will overwrite all your notes and configurations with the backup."
398 read -r -p "Do you want to continue? (y/N) " ANSWER
399 case $ANSWER in
400 y|Y )
401 # restoring rc file
402 BACKUPRC=$(basename $RCFILE)
403 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
404 if [ -r ${RCFILE} ]; then
405 echo "Backing up current '${RCFILE}'...."
406 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
407 fi
408 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
409 fi
410 # restoring notes directory
411 if [ -d $BACKUPDIR/notes ]; then
412 if [ $(/bin/ls -A $NOTESDIR) ]; then
413 rm --verbose $NOTESDIR/*
414 fi
415 cp -r --verbose $BACKUPDIR/notes $BASEDIR
416 fi
417 # restoring database
418 BACKUPDB=$(basename $DB)
419 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
420 if [ -r ${DB} ]; then
421 echo "Backing up current '${DB}'...."
422 mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M)
423 fi
424 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
425 fi
426 # restoring git repo subdirectory
427 if [ -d $BACKUPDIR/.git ]; then
428 if [ /bin/ls -A ${BASEDIR}/.git ]; then
429 rm -rf ${BASEDIR}/.git
430 fi
431 cp -r --verbose ${BACKUPDIR}/.git ${BASEDIR}/
432 fi
433 ;;
434 * )
435 echo "No changes made. Exiting"
436 exit
437 ;;
438 esac
439 }
440
441 editnote() {
442 NOTE=$1
443 # shellcheck disable=SC2155
444 local OK=$(check_noteID "$NOTE")
445 if [ ! "$OK" ]; then
446 echo "invalid note \"$NOTE\""
447 echo "Use the note ID that you can fetch after listing your notes"
448 exit 1
449 fi
450
451 # shellcheck disable=SC2016,SC2086
452 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
453 # shellcheck disable=SC2016,SC2086
454 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
455 if [ "$TITLE" ]; then
456 echo "editing note $TITLE"
457 # shellcheck disable=SC2086,SC2091
458 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
459 else
460 echo "note not found"
461 exit 1
462 fi
463 }
464 listnotes() {
465 # attempt syncing before listing all notes
466 gitsync
467 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
468 if [[ $(ls -A "$NOTESDIR") ]]; then
469 if [ $PLAIN == false ]; then
470 echo "listing all notes"
471 echo ""
472 fi
473 [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
474 for i in "${NOTESDIR}"/*; do
475 # shellcheck disable=SC2155
476 local fname=$(basename $i)
477 DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
478 # shellcheck disable=SC2016,SC2086
479 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
480 # shellcheck disable=SC2016,SC2086
481 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
482 [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
483 done
484 else
485 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
486 fi
487 }
488 rmnote() {
489 # remove eventually existing temp DB file
490 if [[ -f $TMPDB ]]; then
491 rm $TMPDB
492 fi
493
494 NOTE=$1
495 if [ "all" == "$NOTE" ]; then
496 echo "You're going to delete all notes."
497 read -r -p "Do you wish to continue? (y/N) " ANSWER
498 case $ANSWER in
499 y|Y )
500 # shellcheck disable=SC2086
501 $JQ 'del(.notes[])' $DB > $TMPDB
502 # shellcheck disable=SC2086
503 mv $TMPDB $DB
504 # shellcheck disable=SC2086
505 rm $NOTESDIR/*
506 echo "Deleted all notes"
507 ;;
508 * )
509 echo "Aborting, no notes were deleted."
510 exit 1
511 ;;
512 esac
513 else
514 # shellcheck disable=SC2155
515 local OK=$(check_noteID "$NOTE")
516 if [ ! "$OK" ]; then
517 echo "invalid note \"$NOTE\""
518 echo "Use the note ID that you can fetch after listing your notes"
519 sleep 1
520 exit 1
521 fi
522
523 # shellcheck disable=SC2016,SC2086
524 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
525 # shellcheck disable=SC2016,SC2086
526 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
527 if [ "$TITLE" ]; then
528 # shellcheck disable=SC2016,SC2086
529 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
530 # shellcheck disable=SC2086
531 mv $TMPDB $DB
532 rm $NOTESDIR/$FILE
533 echo "Deleted note $TITLE"
534 sleep 1
535 exit
536 else
537 echo "note not found"
538 sleep 1
539 exit 1
540 fi
541 fi
542 }
543 shownote() {
544 NOTE=$1
545
546 # shellcheck disable=SC2155
547 local OK=$(check_noteID "$NOTE")
548 if [ ! "$OK" ]; then
549 echo "invalid note \"$NOTE\""
550 echo "Use the note ID that you can fetch after listing your notes"
551 exit 1
552 fi
553
554 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
555
556 if [ "$FILE" ]; then
557 $PAGER ${NOTESDIR}/${FILE}
558 fi
559 }
560 # shellcheck disable=SC2006
561 GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,showconf,sync::,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
562
563 # shellcheck disable=SC2181
564 if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
565
566 # Note the quotes around `$GOPT': they are essential!
567 eval set -- "$GOPT"
568 unset GOPT
569
570 while true; do
571 case "$1" in
572 -h | --help )
573 helptext
574 exit
575 ;;
576 -v | --version )
577 echo $BASENAME v${VERSION}
578 exit
579 ;;
580 -p | --plain )
581 PLAIN=true
582 shift
583 ;;
584 -l | --list )
585 listnotes
586 exit
587 ;;
588 -a | --add )
589 case "$2" in
590 '' )
591 read -r -p "Title: " TITLE
592 ;;
593 * )
594 TITLE=$2
595 ;;
596 esac
597 shift 2
598 addnote "$TITLE"
599 exit
600 ;;
601 -e | --edit )
602 case "$2" in
603 '' )
604 read -r -p "Note ID: " NOTE
605 ;;
606 * )
607 NOTE=$2
608 ;;
609 esac
610 shift 2
611 editnote "$NOTE"
612 exit
613 ;;
614 -d | --delete )
615 case "$2" in
616 '' )
617 read -r -p "Note ID: " NOTE
618 ;;
619 * )
620 NOTE=$2
621 ;;
622 esac
623 shift 2
624 rmnote "$NOTE"
625 exit
626 ;;
627 -s | --show )
628 case "$2" in
629 '' )
630 read -r -p "Note ID: " NOTE
631 ;;
632 * )
633 NOTE=$2
634 ;;
635 esac
636 shift 2
637 shownote "$NOTE"
638 exit
639 ;;
640 -r | --restore )
641 case "$2" in
642 '' )
643 read -r -p "Backup Dir: " RDIR
644 ;;
645 * )
646 RDIR=$2
647 ;;
648 esac
649 shift 2
650 backup_restore $RDIR
651 exit
652 ;;
653 --sync )
654 case "$2" in
655 '' )
656 gitsync
657 ;;
658 '-f' )
659 gitsync -f
660 ;;
661 * )
662 helptext
663 exit
664 ;;
665 esac
666 shift 2
667 exit
668 ;;
669 --userconf )
670 export_config
671 # shellcheck disable=SC2317
672 echo "config exported to \"$RCFILE\""
673 # shellcheck disable=SC2317
674 exit
675 ;;
676 --showconf )
677 configtext
678 exit
679 ;;
680 --backup )
681 case "$2" in
682 '' )
683 read -r -p "Backup Dir: " BDIR
684 ;;
685 * )
686 BDIR=$2
687 ;;
688 esac
689 shift 2
690 backup_data $BDIR
691 exit
692 ;;
693 -- )
694 shift
695 break
696 ;;
697 * )
698 break
699 ;;
700 esac
701 done
702
703 for arg; do
704 if [ $(check_noteID $arg) ]; then
705 shownote $arg
706 else
707 helptext
708 exit
709 fi
710 done