initial setup of git functionality. The program can now initalize a local repo and...
[bash-notes.git] / notes.sh
... / ...
CommitLineData
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
9if [[ $DEBUG == true ]]; then
10 exec 5> /tmp/debug_bash-notes.log
11 BASH_XTRACEFD="5"
12 PS4='$LINENO: '
13 set -x
14fi
15
16PID=$$
17BASENAME=$( basename "$0" )
18NOW=$(date +%s)
19
20VERSION="0.4git"
21DBVERSION=${VERSION}_${NOW}
22
23set_defaults() {
24# Binaries to use
25JQ=${JQ:-/usr/bin/jq}
26EDITOR=${EDITOR:-/usr/bin/vim}
27TERMINAL=${TERMINAL:-/usr/bin/alacritty}
28# Git binary only used if $USEGIT is true - See below
29GIT=${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
33TERM_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.
36PAGER=${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
40PLAIN=false
41# base directory for program files
42BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
43# notes database in json format
44DB=${BASEDIR}/db.json
45# directory containing the actual notes
46NOTESDIR=${BASEDIR}/notes
47
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
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}
61TMPDB=/tmp/db.json
62
63if [ ! -x "$JQ" ]; then
64 echo "jq not found in your PATH"
65 echo "install jq to continue"
66 exit 1
67fi
68
69# IMPORT USER DEFINED OPTIONS IF ANY
70if [[ -f $RCFILE ]]; then
71 # shellcheck disable=SC1090
72 source "$RCFILE"
73fi
74
75# We prevent the program from running more than one instance:
76PIDFILE=/var/tmp/$(basename "$0" .sh).pid
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
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"
88fi
89
90# create PIDFILE
91echo $PID > "$PIDFILE"
92
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
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 "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
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 *)
166 echo "$IN"
167 ;;
168 esac
169}
170
171function helptext() {
172 echo "Usage:"
173 echo " $0 [PARAMS] [note ID]..."
174 echo ""
175 echo "${BASENAME} parameters are:"
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"
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"
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"
189 echo ""
190 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
191}
192
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
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
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
271function addnote() {
272 # remove eventually existing temp DB file
273 if [[ -f $TMPDB ]]; then
274 rm $TMPDB
275 fi
276
277 RTITLE=$(random_title)
278 [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
279 echo "adding new note - \"$NOTETITLE\""
280 # shellcheck disable=SC2086
281 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
282 # [ "" == $LASTID ] && LASTID=0
283 NOTEID=$(( LASTID + 1 ))
284 # shellcheck disable=SC2086
285 touch ${NOTESDIR}/${NOW}
286 # shellcheck disable=SC2016
287 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
288 # shellcheck disable=SC2086
289 mv $TMPDB $DB
290 # example for alacritty:
291 # alacritty --class notes --title notes -e /usr/bin/vim ...
292 # shellcheck disable=SC2086,SC2091
293 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
294}
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
323 BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/* ${BACKUPDIR})
324 else
325 BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/* ${BACKUPDIR})
326 fi
327 # run the command
328 if [ "$BCKUP_COMM" ]; then
329 echo -e "All files backed up."
330 echo -e "BACKUP directory:\t$BACKUPDIR"
331 tree $BACKUPDIR | $PAGER
332 echo; echo "BACKUP COMPLETED"
333 fi
334}
335
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
376function editnote() {
377 NOTE=$1
378 # shellcheck disable=SC2155
379 local OK=$(check_noteID "$NOTE")
380 if [ ! "$OK" ]; then
381 echo "invalid note \"$NOTE\""
382 echo "Use the note ID that you can fetch after listing your notes"
383 exit 1
384 fi
385
386 # shellcheck disable=SC2016,SC2086
387 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
388 # shellcheck disable=SC2016,SC2086
389 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
390 if [ "$TITLE" ]; then
391 echo "editing note $TITLE"
392 # shellcheck disable=SC2086,SC2091
393 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
394 else
395 echo "note not found"
396 exit 1
397 fi
398}
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}
421function rmnote() {
422 # remove eventually existing temp DB file
423 if [[ -f $TMPDB ]]; then
424 rm $TMPDB
425 fi
426
427 NOTE=$1
428 if [ "all" == "$NOTE" ]; then
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 )
433 # shellcheck disable=SC2086
434 $JQ 'del(.notes[])' $DB > $TMPDB
435 # shellcheck disable=SC2086
436 mv $TMPDB $DB
437 # shellcheck disable=SC2086
438 rm $NOTESDIR/*
439 echo "Deleted all notes"
440 ;;
441 * )
442 echo "Aborting, no notes were deleted."
443 exit 1
444 ;;
445 esac
446 else
447 # shellcheck disable=SC2155
448 local OK=$(check_noteID "$NOTE")
449 if [ ! "$OK" ]; then
450 echo "invalid note \"$NOTE\""
451 echo "Use the note ID that you can fetch after listing your notes"
452 sleep 1
453 exit 1
454 fi
455
456 # shellcheck disable=SC2016,SC2086
457 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
458 # shellcheck disable=SC2016,SC2086
459 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
460 if [ "$TITLE" ]; then
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"
467 sleep 1
468 exit
469 else
470 echo "note not found"
471 sleep 1
472 exit 1
473 fi
474 fi
475}
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}
493# shellcheck disable=SC2006
494GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,sync,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
495
496# shellcheck disable=SC2181
497if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
498
499# Note the quotes around `$GOPT': they are essential!
500eval set -- "$GOPT"
501unset GOPT
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 ;;
513 -p | --plain )
514 PLAIN=true
515 shift
516 ;;
517 -l | --list )
518 listnotes
519 exit
520 ;;
521 -a | --add )
522 case "$2" in
523 '' )
524 read -r -p "Title: " TITLE
525 ;;
526 * )
527 TITLE=$2
528 ;;
529 esac
530 shift 2
531 addnote "$TITLE"
532 exit
533 ;;
534 -e | --edit )
535 case "$2" in
536 '' )
537 read -r -p "Note ID: " NOTE
538 ;;
539 * )
540 NOTE=$2
541 ;;
542 esac
543 shift 2
544 editnote "$NOTE"
545 exit
546 ;;
547 -d | --delete )
548 case "$2" in
549 '' )
550 read -r -p "Note ID: " NOTE
551 ;;
552 * )
553 NOTE=$2
554 ;;
555 esac
556 shift 2
557 rmnote "$NOTE"
558 exit
559 ;;
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"
571 exit
572 ;;
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 ;;
586 --sync )
587 gitsync
588 exit
589 ;;
590 --userconf )
591 export_config
592 # shellcheck disable=SC2317
593 echo "config exported to \"$RCFILE\""
594 # shellcheck disable=SC2317
595 exit
596 ;;
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 ;;
610 -- )
611 shift
612 break
613 ;;
614 * )
615 break
616 ;;
617 esac
618done
619
620for arg; do
621 if [ $(check_noteID $arg) ]; then
622 shownote $arg
623 else
624 helptext
625 exit
626 fi
627done