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