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