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/
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
17 BASENAME
=$
( basename "$0" )
21 DBVERSION
=${VERSION}_
${NOW}
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}
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
41 # base directory for program files
42 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
43 # notes database in json format
45 # directory containing the actual notes
46 NOTESDIR
=${BASEDIR}/notes
50 # If you want to store your notes in a git repository set this to true
52 # Address of your remote repository. Without this GIT will refuse to work
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 # The name of this client. If left empty, defaults to the output of hostname
57 GITCLIENT
=${GITCLIENT:-""}
59 } # end set_defaults, do not change this line.
63 # Do not edit below this point
64 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
67 if [ ! -x "$JQ" ]; then
68 echo "jq not found in your PATH"
69 echo "install jq to continue"
73 # IMPORT USER DEFINED OPTIONS IF ANY
74 if [[ -f $RCFILE ]]; then
75 # shellcheck disable=SC1090
79 # We prevent the program from running more than one instance:
80 PIDFILE
=/var
/tmp
/$
(basename "$0" .sh
).pid
82 # Make sure the PID file is removed when we kill the process
83 trap 'rm -f $PIDFILE; exit 1' TERM INT
85 if [[ -r $PIDFILE ]]; then
86 # PIDFILE exists, so I guess there's already an instance running
87 # let's kill it and run again
88 # shellcheck disable=SC2046,SC2086
89 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
90 # should already be deleted by trap, but just to be sure
95 echo $PID > "$PIDFILE"
97 # Export config to file
99 if [ -r ${RCFILE} ]; then
100 echo "Backing up current '${RCFILE}'...."
101 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
103 echo "Writing '${RCFILE}'...."
104 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
105 |
grep -v set_defaults \
106 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
108 if [ -r ${RCFILE} ]; then
109 echo "Taking no further action."
112 echo "Could not write '${RCFILE}'...!"
117 # we should expand on this function to add a sample note and explain a little bit
118 # how the program works.
120 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
123 echo "${BASENAME} configuration:
125 base directory: ${BASEDIR}/
126 notes archive: ${NOTESDIR}/
127 notes database: ${DB}
129 text editor: ${EDITOR}
130 terminal: ${TERMINAL}
134 echo "Now I'll create the needed files and directories."
135 read -r -p "Do you wish to continue? (y/N) " ANSWER
139 cat << __EOL__ > ${DB}
142 "version": "${VERSION}",
143 "dbversion": "${DBVERSION}"
151 echo; echo "All done, you can now write your first note."
154 echo "No changes made. Exiting"
160 # check for notes dir existance and create it in case it doesn't exists
161 if [[ ! -d $NOTESDIR ]]; then
162 # we don't have a directory. FIRST RUN?
165 # check if input is a number, returns false or the number itself
180 echo " $0 [PARAMS] [note ID]..."
182 echo "${BASENAME} parameters are:"
183 echo -e " -h | --help\t\t\t: This help text"
184 echo -e " -p | --plain\t\t\t: Output is in plain text"
185 echo -e "\t\t\t\t (without this option the output is formatted)"
186 echo -e "\t\t\t\t (this option must precede all others)"
187 echo -e " -l | --list\t\t\t: List existing notes"
188 echo -e " -a | --add=[\"<title>\"]\t: Add new note"
189 echo -e " -e | --edit=[<note>]\t\t: Edit note"
190 echo -e " -d | --delete=[<note> | all] : Delete single note or all notes at once"
191 echo -e " -s | --show=[<note>]\t\t: Display note using your favourite PAGER"
192 echo -e " -r | --restore=[<dir>]\t: Restore a previous backup from dir"
193 echo -e " -v | --version\t\t: Print version"
194 echo -e " --userconf\t\t\t: Export User config file"
195 echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder"
196 echo -e " --showconf\t\t\t: Display running options"
197 echo -e " --sync\t\t\t: Sync notes to git repository"
199 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
203 [ $USEGIT ] && GITUSE
="enabled" || GITUSE
="disabled"
204 if [ -n $GITCLIENT ]; then
205 CLIENTGIT
="$( hostname )"
207 CLIENTGIT
="$GITCLIENT"
210 echo -e "${BASENAME} configuration is:"
212 echo -e "\tbase directory: ${BASEDIR}/"
213 echo -e "\tnotes archive: ${NOTESDIR}/"
214 echo -e "\tnotes database: ${DB}"
215 echo -e "\trc file: $RCFILE"
216 echo -e "\tdebug file: /tmp/debug_bash-note.log"
218 echo -e "\ttext editor: ${EDITOR}"
219 echo -e "\tterminal: ${TERMINAL}"
220 echo -e "\tjq executable: ${JQ}"
221 echo -e "\tPAGER: ${PAGER}"
223 echo -e "\tGIT: ${GITUSE} - ${GIT}"
224 echo -e "\tGIT remote: ${GITREMOTE}"
225 echo -e "\tGIT sync delay: ${GITSYNCDELAY}"
226 echo -e "\tGIT client name: ${CLIENTGIT}"
229 # this function returns a random 2 words title
233 DICT
=/usr
/share
/dict
/words
236 # total number of non-random words available
237 COUNT
=$
(cat $DICT |
wc -l)
239 # while loop to generate random words
242 RAND
=$
(od -N3 -An -i /dev
/urandom |
awk -v f
=0 -v r
="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}')
243 OUTPUT
+="$(sed `echo $RAND`"q
;d
" $DICT)"
245 [[ $X -eq 1 ]] && OUTPUT
+=" "
251 # check if GITCLIENT has been set or set it to the output of hostname
252 if [ -z "$GITCLIENT" ]; then
253 GITCLIENT
=$
( hostname
)
255 # returns true if the argument provided directory is a git repository
258 if [[ -d $DIR ]]; then
260 if git rev-parse
2>/dev
/null
; then
268 # sync local repository to remote
269 # accepts -f parameter to skip last sync check
272 if [[ $USEGIT && -n $GITREMOTE ]]; then
273 [ $PLAIN == false
] && echo "Syncing notes with git on remote \"$GITREMOTE\""
275 if [[ $FORCE == "-f" ]]; then
276 $JQ --arg n
"$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
279 [ $PLAIN == false
] && $GIT pull ||
$GIT pull
-q
281 # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time.
282 LASTSYNC
=$
($JQ -r '.git["lastpull"] // 0' "$DB")
283 SYNCDIFF
=$
(( ${NOWSYNC} - ${LASTSYNC} ))
284 if (( $SYNCDIFF > $GITSYNCDELAY )); then
285 #more than our delay time has passed. We can sync again.
286 $JQ --arg n
"$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
289 [ $PLAIN == false
] && $GIT pull ||
$GIT pull
-q
291 # Last synced less than $GITSYNCDELAY seconds ago. We shall wait
292 [ $PLAIN == false
] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait"
296 # no git, so we just keep going
301 # add note to git and push it to remote
303 if [[ $USEGIT && -n $GITREMOTE ]]; then
304 [ $PLAIN == false
] && echo "Adding note to remote \"$GITREMOTE\""
307 $GIT commit
-m "$(basename $0) - adding note from ${GITCLIENT}"
308 $GIT push origin master
310 # no git, so we just keep going
315 # edited note added to git and pushed it to remote
317 if [[ $USEGIT && -n $GITREMOTE ]]; then
318 [ $PLAIN == false
] && echo "Editing note on remote \"$GITREMOTE\""
321 $GIT commit
-m "$(basename $0) - ${GITCLIENT} note edited."
322 $GIT push origin master
324 # no git, so we just keep going
329 # add note to git and push it to remote
333 if [[ $USEGIT && -n $GITREMOTE ]]; then
334 [ $PLAIN == false
] && echo "Deleting notes from remote \"$GITREMOTE\""
335 if [ "all" == $NOTE ];then
336 echo "Deleting all notes"
339 $GIT commit
-m "$(basename $0) - ${GITCLIENT} removing all notes."
340 $GIT push origin master
342 local OK
=$
(check_noteID
"$NOTE")
344 echo "Deleting note ID ${NOTE}"
346 $GIT rm notes
/${FILE}
348 $GIT commit
-m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}."
349 $GIT push origin master
353 # no git, so we just keep going
358 # check for USEGIT and subsequent variables
359 if [[ $USEGIT && -n $GITREMOTE ]]; then
361 if ! is_git_repo
$BASEDIR; then
362 # initializing git repository
365 echo "adding all files to git"
367 $GIT commit
-m "$(basename $0) - initial commit from ${GITCLIENT}"
368 $GIT remote add origin
$GITREMOTE
369 $GIT push
-u origin master
371 elif [[ $USEGIT && -z $GITREMOTE ]]; then
372 echo "GITREMOTE variable not set. reverting USEGIT to false"
377 # attempt syncing before adding a note
379 # remove eventually existing temp DB file
380 if [[ -f $TMPDB ]]; then
385 RTITLE
=$
(random_title
)
388 read -r -p "Title: " TITLE
399 # [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
400 echo "adding new note - \"$NOTETITLE\""
401 # shellcheck disable=SC2086
402 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
403 # [ "" == $LASTID ] && LASTID=0
404 NOTEID
=$
(( LASTID
+ 1 ))
405 # shellcheck disable=SC2086
406 touch ${NOTESDIR}/${NOW}
407 # shellcheck disable=SC2016
408 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
409 # shellcheck disable=SC2086
411 # example for alacritty:
412 # alacritty --class notes --title notes -e /usr/bin/vim ...
413 # shellcheck disable=SC2086,SC2091
414 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
415 # add note to git and push to remote
420 echo "backing up data in $BACKUPDIR"
423 if [ -d $BACKUPDIR ]; then
424 if [ $
(/bin
/ls -A $BACKUPDIR) ]; then
425 echo "$BACKUPDIR is not empty. Cannot continue"
428 echo "$BACKUPDIR is ok. Continuing!"
431 # BACKUPDIR doesn't exists
432 echo "$BACKUPDIR doesn't exists"
433 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
439 echo "No changes made. Exiting"
444 # ok, we have a backup directory
445 if [ -r $RCFILE ]; then
446 BCKUP_COMM
=$
(rsync
-avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
448 BCKUP_COMM
=$
(rsync
-avz --progress ${BASEDIR}/ ${BACKUPDIR})
451 if [ "$BCKUP_COMM" ]; then
452 echo -e "All files backed up."
453 echo -e "BACKUP directory:\t$BACKUPDIR"
454 tree
$BACKUPDIR |
$PAGER
455 echo; echo "BACKUP COMPLETED"
461 echo "restoring backup from $BACKUPDIR"
462 echo "This will overwrite all your notes and configurations with the backup."
463 read -r -p "Do you want to continue? (y/N) " ANSWER
467 BACKUPRC
=$
(basename $RCFILE)
468 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
469 if [ -r ${RCFILE} ]; then
470 echo "Backing up current '${RCFILE}'...."
471 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
473 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
475 # restoring notes directory
476 if [ -d $BACKUPDIR/notes
]; then
477 if [ $
(/bin
/ls -A $NOTESDIR) ]; then
478 rm --verbose $NOTESDIR/*
480 cp -r --verbose $BACKUPDIR/notes
$BASEDIR
483 BACKUPDB
=$
(basename $DB)
484 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
485 if [ -r ${DB} ]; then
486 echo "Backing up current '${DB}'...."
487 mv -f ${DB} ${DB}.$
(date +%Y
%m
%d_
%H
%M
)
489 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
491 # restoring git repo subdirectory
492 if [ -d $BACKUPDIR/.git
]; then
493 if [ /bin
/ls -A ${BASEDIR}/.git
]; then
494 rm -rf ${BASEDIR}/.git
496 cp -r --verbose ${BACKUPDIR}/.git
${BASEDIR}/
500 echo "No changes made. Exiting"
508 # shellcheck disable=SC2155
509 local OK
=$
(check_noteID
"$NOTE")
511 echo "invalid note \"$NOTE\""
512 echo "Use the note ID that you can fetch after listing your notes"
516 # shellcheck disable=SC2016,SC2086
517 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
518 # shellcheck disable=SC2016,SC2086
519 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
520 if [ "$TITLE" ]; then
521 echo "editing note $TITLE"
522 # shellcheck disable=SC2086,SC2091
523 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
526 echo "note not found"
531 # attempt syncing before listing all notes
533 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
534 if [[ $
(ls -A "$NOTESDIR") ]]; then
535 if [ $PLAIN == false
]; then
536 echo "listing all notes"
539 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
540 for i
in "${NOTESDIR}"/*; do
541 # shellcheck disable=SC2155
542 local fname
=$
(basename $i)
543 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
544 # shellcheck disable=SC2016,SC2086
545 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
546 # shellcheck disable=SC2016,SC2086
547 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
548 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
551 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
555 # remove eventually existing temp DB file
556 if [[ -f $TMPDB ]]; then
561 if [ "all" == "$NOTE" ]; then
562 echo "You're going to delete all notes."
563 read -r -p "Do you wish to continue? (y/N) " ANSWER
566 # shellcheck disable=SC2086
567 $JQ 'del(.notes[])' $DB > $TMPDB
568 # shellcheck disable=SC2086
570 # shellcheck disable=SC2086
573 echo "Deleted all notes"
576 echo "Aborting, no notes were deleted."
581 # shellcheck disable=SC2155
582 local OK
=$
(check_noteID
"$NOTE")
584 echo "invalid note \"$NOTE\""
585 echo "Use the note ID that you can fetch after listing your notes"
590 # shellcheck disable=SC2016,SC2086
591 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
592 # shellcheck disable=SC2016,SC2086
593 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
594 if [ "$TITLE" ]; then
595 # shellcheck disable=SC2016,SC2086
596 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
597 # shellcheck disable=SC2086
601 echo "Deleted note $TITLE"
605 echo "note not found"
614 # shellcheck disable=SC2155
615 local OK
=$
(check_noteID
"$NOTE")
617 echo "invalid note \"$NOTE\""
618 echo "Use the note ID that you can fetch after listing your notes"
622 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
625 $PAGER ${NOTESDIR}/${FILE}
628 # shellcheck disable=SC2006
629 GOPT
=$
(getopt
-o hvplr
:a
::e
:d
:s
: --long help,version
,list
,plain
,userconf
,showconf
,sync
,restore
:,backup
:,add
::,edit
:,delete
:,show
: -n 'bash-notes' -- "$@")
631 # shellcheck disable=SC2181
632 if [ $?
!= 0 ] ; then helptext
>&2 ; exit 1 ; fi
634 # Note the quotes around `$GOPT': they are essential!
645 echo $BASENAME v
${VERSION}
687 # I'm forcing it because if you run it manually, chances are that you need to.
694 # shellcheck disable=SC2317
695 echo "config exported to \"$RCFILE\""
696 # shellcheck disable=SC2317
720 if [ $
(check_noteID
$arg) ]; then