git hidden directory is now backed up and restored properly
[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
55 } # end set_defaults, do not change this line.
56
57 set_defaults
58
59 # Do not edit below this point
60 RCFILE=${RCFILE:-~/.config/bash-notes.rc}
61 TMPDB=/tmp/db.json
62
63 if [ ! -x "$JQ" ]; then
64 echo "jq not found in your PATH"
65 echo "install jq to continue"
66 exit 1
67 fi
68
69 # IMPORT USER DEFINED OPTIONS IF ANY
70 if [[ -f $RCFILE ]]; then
71 # shellcheck disable=SC1090
72 source "$RCFILE"
73 fi
74
75 # We prevent the program from running more than one instance:
76 PIDFILE=/var/tmp/$(basename "$0" .sh).pid
77
78 # Make sure the PID file is removed when we kill the process
79 trap 'rm -f $PIDFILE; exit 1' TERM INT
80
81 if [[ -r $PIDFILE ]]; then
82 # PIDFILE exists, so I guess there's already an instance running
83 # let's kill it and run again
84 # shellcheck disable=SC2046,SC2086
85 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
86 # should already be deleted by trap, but just to be sure
87 rm "$PIDFILE"
88 fi
89
90 # create PIDFILE
91 echo $PID > "$PIDFILE"
92
93 # Export config to file
94 function 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.
115 function firstrun() {
116 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
117
118 clear
119 echo "${BASENAME} configuration:
120
121 base directory: ${BASEDIR}/
122 notes archive: ${NOTESDIR}/
123 notes database: ${DB}
124 rc file: $RC
125 text editor: ${EDITOR}
126 terminal: ${TERMINAL}
127 jq executable: ${JQ}
128 "
129
130 echo "Now I'll create the needed files and directories."
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 },
141 "git": {
142 "lastpull": ""
143 },
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
157 if [[ ! -d $NOTESDIR ]]; then
158 # we don't have a directory. FIRST RUN?
159 firstrun
160 fi
161 # check if input is a number, returns false or the number itself
162 function check_noteID() {
163 IN=$1
164 case $IN in
165 ''|*[!0-9]*)
166 return 1
167 ;;
168 *)
169 echo "$IN"
170 ;;
171 esac
172 }
173
174 function helptext() {
175 echo "Usage:"
176 echo " $0 [PARAMS] [note ID]..."
177 echo ""
178 echo "${BASENAME} parameters are:"
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"
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"
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"
192 echo ""
193 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
194 }
195
196 function configtext() {
197 cat << __NOWCONF__
198 ${BASENAME} configuration is:
199
200 base directory: ${BASEDIR}/
201 notes archive: ${NOTESDIR}/
202 notes database: ${DB}
203 rc file: $RCFILE
204 debug file: /tmp/debug_bash-note.log
205
206 text editor: ${EDITOR}
207 terminal: ${TERMINAL}
208 jq executable: ${JQ}
209 PAGER: ${PAGER}
210 __NOWCONF__
211
212 }
213
214 # this function returns a random 2 words title
215 function 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
236 # returns true if the argument provided directory is a git repository
237 is_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
250 gitsync() {
251 echo "Syncing notes with git on remote \"$GITREMOTE\""
252 cd $BASEDIR
253 $GIT pull
254 }
255
256 # check for USEGIT and subsequent variables
257 if [[ $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
269 elif [[ $USEGIT && -z $GITREMOTE ]]; then
270 echo "GITREMOTE variable not set. reverting USEGIT to false"
271 USEGIT=false
272 fi
273
274 function addnote() {
275 # remove eventually existing temp DB file
276 if [[ -f $TMPDB ]]; then
277 rm $TMPDB
278 fi
279
280 RTITLE=$(random_title)
281 [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
282 echo "adding new note - \"$NOTETITLE\""
283 # shellcheck disable=SC2086
284 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
285 # [ "" == $LASTID ] && LASTID=0
286 NOTEID=$(( LASTID + 1 ))
287 # shellcheck disable=SC2086
288 touch ${NOTESDIR}/${NOW}
289 # shellcheck disable=SC2016
290 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
291 # shellcheck disable=SC2086
292 mv $TMPDB $DB
293 # example for alacritty:
294 # alacritty --class notes --title notes -e /usr/bin/vim ...
295 # shellcheck disable=SC2086,SC2091
296 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
297 }
298 function 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
326 BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
327 else
328 BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR})
329 fi
330 # run the command
331 if [ "$BCKUP_COMM" ]; then
332 echo -e "All files backed up."
333 echo -e "BACKUP directory:\t$BACKUPDIR"
334 tree $BACKUPDIR | $PAGER
335 echo; echo "BACKUP COMPLETED"
336 fi
337 }
338
339 function 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
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
378 ;;
379 * )
380 echo "No changes made. Exiting"
381 exit
382 ;;
383 esac
384 }
385
386 function editnote() {
387 NOTE=$1
388 # shellcheck disable=SC2155
389 local OK=$(check_noteID "$NOTE")
390 if [ ! "$OK" ]; then
391 echo "invalid note \"$NOTE\""
392 echo "Use the note ID that you can fetch after listing your notes"
393 exit 1
394 fi
395
396 # shellcheck disable=SC2016,SC2086
397 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
398 # shellcheck disable=SC2016,SC2086
399 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
400 if [ "$TITLE" ]; then
401 echo "editing note $TITLE"
402 # shellcheck disable=SC2086,SC2091
403 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
404 else
405 echo "note not found"
406 exit 1
407 fi
408 }
409 function 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 }
431 function rmnote() {
432 # remove eventually existing temp DB file
433 if [[ -f $TMPDB ]]; then
434 rm $TMPDB
435 fi
436
437 NOTE=$1
438 if [ "all" == "$NOTE" ]; then
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 )
443 # shellcheck disable=SC2086
444 $JQ 'del(.notes[])' $DB > $TMPDB
445 # shellcheck disable=SC2086
446 mv $TMPDB $DB
447 # shellcheck disable=SC2086
448 rm $NOTESDIR/*
449 echo "Deleted all notes"
450 ;;
451 * )
452 echo "Aborting, no notes were deleted."
453 exit 1
454 ;;
455 esac
456 else
457 # shellcheck disable=SC2155
458 local OK=$(check_noteID "$NOTE")
459 if [ ! "$OK" ]; then
460 echo "invalid note \"$NOTE\""
461 echo "Use the note ID that you can fetch after listing your notes"
462 sleep 1
463 exit 1
464 fi
465
466 # shellcheck disable=SC2016,SC2086
467 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
468 # shellcheck disable=SC2016,SC2086
469 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
470 if [ "$TITLE" ]; then
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"
477 sleep 1
478 exit
479 else
480 echo "note not found"
481 sleep 1
482 exit 1
483 fi
484 fi
485 }
486 function 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 }
503 # shellcheck disable=SC2006
504 GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,sync,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
505
506 # shellcheck disable=SC2181
507 if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
508
509 # Note the quotes around `$GOPT': they are essential!
510 eval set -- "$GOPT"
511 unset GOPT
512
513 while 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 ;;
523 -p | --plain )
524 PLAIN=true
525 shift
526 ;;
527 -l | --list )
528 listnotes
529 exit
530 ;;
531 -a | --add )
532 case "$2" in
533 '' )
534 read -r -p "Title: " TITLE
535 ;;
536 * )
537 TITLE=$2
538 ;;
539 esac
540 shift 2
541 addnote "$TITLE"
542 exit
543 ;;
544 -e | --edit )
545 case "$2" in
546 '' )
547 read -r -p "Note ID: " NOTE
548 ;;
549 * )
550 NOTE=$2
551 ;;
552 esac
553 shift 2
554 editnote "$NOTE"
555 exit
556 ;;
557 -d | --delete )
558 case "$2" in
559 '' )
560 read -r -p "Note ID: " NOTE
561 ;;
562 * )
563 NOTE=$2
564 ;;
565 esac
566 shift 2
567 rmnote "$NOTE"
568 exit
569 ;;
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"
581 exit
582 ;;
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 ;;
596 --sync )
597 gitsync
598 exit
599 ;;
600 --userconf )
601 export_config
602 # shellcheck disable=SC2317
603 echo "config exported to \"$RCFILE\""
604 # shellcheck disable=SC2317
605 exit
606 ;;
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 ;;
620 -- )
621 shift
622 break
623 ;;
624 * )
625 break
626 ;;
627 esac
628 done
629
630 for arg; do
631 if [ $(check_noteID $arg) ]; then
632 shownote $arg
633 else
634 helptext
635 exit
636 fi
637 done