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