cleanup of the output of the program. Updated README.
[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
3bd93e7f 52# Address of your remote repository. Without this GIT will refuse to work
4cbcb39e 53GITREMOTE=${GITREMOTE:-""}
cf6d89bc 54# How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour)
1f4d7742 55GITSYNCDELAY=${GITSYNCDELAY:-"3600"}
3bd93e7f 56# The name of this client. If left empty, defaults to the output of hostname
bba0734f 57GITCLIENT=${GITCLIENT:-""}
4cbcb39e 58
d80ac20a 59} # end set_defaults, do not change this line.
60
61set_defaults
62
63# Do not edit below this point
64RCFILE=${RCFILE:-~/.config/bash-notes.rc}
53f2ed57 65TMPDB=/tmp/db.json
a4aaf855 66
b9f21021 67if [ ! -x "$JQ" ]; then
a4aaf855 68 echo "jq not found in your PATH"
69 echo "install jq to continue"
70 exit 1
71fi
72
53f2ed57 73# IMPORT USER DEFINED OPTIONS IF ANY
74if [[ -f $RCFILE ]]; then
b9f21021 75 # shellcheck disable=SC1090
76 source "$RCFILE"
53f2ed57 77fi
78
a4aaf855 79# We prevent the program from running more than one instance:
b9f21021 80PIDFILE=/var/tmp/$(basename "$0" .sh).pid
a4aaf855 81
82# Make sure the PID file is removed when we kill the process
83trap 'rm -f $PIDFILE; exit 1' TERM INT
84
85if [[ -r $PIDFILE ]]; then
86 # PIDFILE exists, so I guess there's already an instance running
87 # let's kill it and run again
b9f21021 88 # shellcheck disable=SC2046,SC2086
a4aaf855 89 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
90 # should already be deleted by trap, but just to be sure
b9f21021 91 rm "$PIDFILE"
a4aaf855 92fi
93
94# create PIDFILE
b9f21021 95echo $PID > "$PIDFILE"
a4aaf855 96
fb711183 97# Export config to file
cf6d89bc 98export_config() {
fb711183 99 if [ -r ${RCFILE} ]; then
100 echo "Backing up current '${RCFILE}'...."
101 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
102 fi
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/' \
107 > ${RCFILE}
108 if [ -r ${RCFILE} ]; then
109 echo "Taking no further action."
110 exit 0
111 else
112 echo "Could not write '${RCFILE}'...!"
113 exit 1
114 fi
115}
116
117# we should expand on this function to add a sample note and explain a little bit
118# how the program works.
cf6d89bc 119firstrun() {
fb711183 120 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
121
122 clear
123 echo "${BASENAME} configuration:
124
125base directory: ${BASEDIR}/
126notes archive: ${NOTESDIR}/
127notes database: ${DB}
128rc file: $RC
129text editor: ${EDITOR}
130terminal: ${TERMINAL}
131jq executable: ${JQ}
132"
133
9eb02251 134 echo "Now I'll create the needed files and directories."
fb711183 135 read -r -p "Do you wish to continue? (y/N) " ANSWER
136 case $ANSWER in
137 y|Y )
138 mkdir -p $NOTESDIR
139 cat << __EOL__ > ${DB}
140{
141 "params": {
142 "version": "${VERSION}",
143 "dbversion": "${DBVERSION}"
144 },
87a368fe 145 "git": {
146 "lastpull": ""
147 },
fb711183 148 "notes": []
149}
150__EOL__
151 echo; echo "All done, you can now write your first note."
152 ;;
153 * )
154 echo "No changes made. Exiting"
155 exit
156 ;;
157 esac
158}
159
160# check for notes dir existance and create it in case it doesn't exists
161if [[ ! -d $NOTESDIR ]]; then
162 # we don't have a directory. FIRST RUN?
163 firstrun
164fi
61c91990 165# check if input is a number, returns false or the number itself
cf6d89bc 166check_noteID() {
61c91990 167 IN=$1
168 case $IN in
169 ''|*[!0-9]*)
3bd93e7f 170 false
61c91990 171 ;;
172 *)
b9f21021 173 echo "$IN"
61c91990 174 ;;
175 esac
176}
177
cf6d89bc 178helptext() {
d80ac20a 179 echo "Usage:"
efa3e607 180 echo " $0 [PARAMS] [note ID]..."
d80ac20a 181 echo ""
e3670e83 182 echo "${BASENAME} parameters are:"
9eb02251 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"
3951cc3d 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"
9eb02251 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"
3bd93e7f 196 echo -e " --showconf\t\t\t: Display running options"
197 echo -e " --sync\t\t\t: Sync notes to git repository"
e3670e83 198 echo ""
efa3e607 199 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
a4aaf855 200}
9eb02251 201
cf6d89bc 202configtext() {
1f4d7742 203 [ $USEGIT ] && GITUSE="enabled" || GITUSE="disabled"
3bd93e7f 204 if [ -n $GITCLIENT ]; then
205 CLIENTGIT="$( hostname )"
206 else
207 CLIENTGIT="$GITCLIENT"
208 fi
1f4d7742 209 clear
210 echo -e "${BASENAME} configuration is:"
211
3bd93e7f 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"
217 echo
218 echo -e "\ttext editor: ${EDITOR}"
219 echo -e "\tterminal: ${TERMINAL}"
220 echo -e "\tjq executable: ${JQ}"
221 echo -e "\tPAGER: ${PAGER}"
222 echo
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}"
3951cc3d 227}
228
6a193095 229# this function returns a random 2 words title
cf6d89bc 230random_title() {
6a193095 231 # Constants
232 X=0
233 DICT=/usr/share/dict/words
234 OUTPUT=""
235
236 # total number of non-random words available
237 COUNT=$(cat $DICT | wc -l)
238
239 # while loop to generate random words
240 while [ "$X" -lt 2 ]
241 do
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)"
244 (("X = X + 1"))
245 [[ $X -eq 1 ]] && OUTPUT+=" "
246 done
247
248 echo $OUTPUT
249}
250
bba0734f 251# check if GITCLIENT has been set or set it to the output of hostname
1f4d7742 252if [ -z "$GITCLIENT" ]; then
bba0734f 253 GITCLIENT=$( hostname )
254fi
4cbcb39e 255# returns true if the argument provided directory is a git repository
256is_git_repo() {
257 DIR=$1
258 if [[ -d $DIR ]]; then
259 cd $DIR
260 if git rev-parse 2>/dev/null; then
261 true
262 else
263 false
264 fi
265 fi
266}
267
268# sync local repository to remote
1f4d7742 269# accepts -f parameter to skip last sync check
4cbcb39e 270gitsync() {
1f4d7742 271 FORCE=$1
bba0734f 272 if [[ $USEGIT && -n $GITREMOTE ]]; then
bba0734f 273 [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\""
1f4d7742 274 NOWSYNC=$(date +%s)
275 if [[ $FORCE == "-f" ]]; then
bba0734f 276 $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
277 mv $TMPDB $DB
278 cd $BASEDIR
cf9f797b 279 [ $PLAIN == false ] && $GIT pull || $GIT pull -q
bba0734f 280 else
1f4d7742 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
287 mv $TMPDB $DB
288 cd $BASEDIR
cf9f797b 289 [ $PLAIN == false ] && $GIT pull || $GIT pull -q
1f4d7742 290 else
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"
293 fi
bba0734f 294 fi
295 else
296 # no git, so we just keep going
297 true
298 fi
299}
300
301# add note to git and push it to remote
302gitadd() {
303 if [[ $USEGIT && -n $GITREMOTE ]]; then
304 [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\""
cf6d89bc 305 cd $BASEDIR
bba0734f 306 $GIT add .
307 $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}"
308 $GIT push origin master
cf6d89bc 309 else
bba0734f 310 # no git, so we just keep going
311 true
cf6d89bc 312 fi
4cbcb39e 313}
314
d1f115c1 315# edited note added to git and pushed it to remote
316gitedit() {
317 if [[ $USEGIT && -n $GITREMOTE ]]; then
318 [ $PLAIN == false ] && echo "Editing note on remote \"$GITREMOTE\""
319 cd $BASEDIR
320 $GIT add .
321 $GIT commit -m "$(basename $0) - ${GITCLIENT} note edited."
322 $GIT push origin master
323 else
324 # no git, so we just keep going
325 true
326 fi
327}
328
329# add note to git and push it to remote
330gitremove() {
331 NOTE=$1
332 FILE=$2
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"
337 cd $BASEDIR
338 $GIT rm notes/*
339 $GIT commit -m "$(basename $0) - ${GITCLIENT} removing all notes."
340 $GIT push origin master
341 else
d1f115c1 342 local OK=$(check_noteID "$NOTE")
3bd93e7f 343 if [[ "$OK" ]]; then
344 echo "Deleting note ID ${NOTE}"
345 cd $BASEDIR
346 $GIT rm notes/${FILE}
347 $GIT add .
348 $GIT commit -m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}."
349 $GIT push origin master
350 fi
d1f115c1 351 fi
352 else
353 # no git, so we just keep going
354 true
355 fi
356}
357
4cbcb39e 358# check for USEGIT and subsequent variables
359if [[ $USEGIT && -n $GITREMOTE ]]; then
360 # GIT is a go.
361 if ! is_git_repo $BASEDIR; then
362 # initializing git repository
363 cd $BASEDIR
364 $GIT init
365 echo "adding all files to git"
366 $GIT add .
bba0734f 367 $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}"
4cbcb39e 368 $GIT remote add origin $GITREMOTE
369 $GIT push -u origin master
370 fi
371elif [[ $USEGIT && -z $GITREMOTE ]]; then
372 echo "GITREMOTE variable not set. reverting USEGIT to false"
373 USEGIT=false
374fi
375
cf6d89bc 376addnote() {
bba0734f 377 # attempt syncing before adding a note
1f4d7742 378 gitsync -f
026502da 379 # remove eventually existing temp DB file
380 if [[ -f $TMPDB ]]; then
381 rm $TMPDB
382 fi
383
6a193095 384 RTITLE=$(random_title)
385 [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
53f2ed57 386 echo "adding new note - \"$NOTETITLE\""
b9f21021 387 # shellcheck disable=SC2086
e3670e83 388 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
389 # [ "" == $LASTID ] && LASTID=0
b9f21021 390 NOTEID=$(( LASTID + 1 ))
391 # shellcheck disable=SC2086
a4aaf855 392 touch ${NOTESDIR}/${NOW}
b9f21021 393 # shellcheck disable=SC2016
a4aaf855 394 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
b9f21021 395 # shellcheck disable=SC2086
a4aaf855 396 mv $TMPDB $DB
b648c006 397 # example for alacritty:
398 # alacritty --class notes --title notes -e /usr/bin/vim ...
b9f21021 399 # shellcheck disable=SC2086,SC2091
e3670e83 400 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
bba0734f 401 # add note to git and push to remote
402 gitadd
a4aaf855 403}
cf6d89bc 404backup_data() {
9eb02251 405 BACKUPDIR="$1"
406 echo "backing up data in $BACKUPDIR"
407
408
409 if [ -d $BACKUPDIR ]; then
410 if [ $(/bin/ls -A $BACKUPDIR) ]; then
411 echo "$BACKUPDIR is not empty. Cannot continue"
412 exit
413 else
414 echo "$BACKUPDIR is ok. Continuing!"
415 fi
416 else
417 # BACKUPDIR doesn't exists
418 echo "$BACKUPDIR doesn't exists"
419 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
420 case $ANSWER in
421 y|Y )
422 mkdir -p $BACKUPDIR
423 ;;
424 * )
425 echo "No changes made. Exiting"
426 exit
427 ;;
428 esac
429 fi
430 # ok, we have a backup directory
431 if [ -r $RCFILE ]; then
87a368fe 432 BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
9eb02251 433 else
87a368fe 434 BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR})
9eb02251 435 fi
436 # run the command
437 if [ "$BCKUP_COMM" ]; then
3951cc3d 438 echo -e "All files backed up."
9eb02251 439 echo -e "BACKUP directory:\t$BACKUPDIR"
3951cc3d 440 tree $BACKUPDIR | $PAGER
9eb02251 441 echo; echo "BACKUP COMPLETED"
442 fi
443}
444
cf6d89bc 445backup_restore() {
3951cc3d 446 BACKUPDIR="$1"
447 echo "restoring backup from $BACKUPDIR"
448 echo "This will overwrite all your notes and configurations with the backup."
449 read -r -p "Do you want to continue? (y/N) " ANSWER
450 case $ANSWER in
451 y|Y )
452 # restoring rc file
453 BACKUPRC=$(basename $RCFILE)
454 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
455 if [ -r ${RCFILE} ]; then
456 echo "Backing up current '${RCFILE}'...."
457 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
458 fi
459 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
460 fi
461 # restoring notes directory
462 if [ -d $BACKUPDIR/notes ]; then
463 if [ $(/bin/ls -A $NOTESDIR) ]; then
464 rm --verbose $NOTESDIR/*
465 fi
466 cp -r --verbose $BACKUPDIR/notes $BASEDIR
467 fi
468 # restoring database
469 BACKUPDB=$(basename $DB)
470 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
471 if [ -r ${DB} ]; then
472 echo "Backing up current '${DB}'...."
473 mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M)
474 fi
475 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
476 fi
87a368fe 477 # restoring git repo subdirectory
478 if [ -d $BACKUPDIR/.git ]; then
479 if [ /bin/ls -A ${BASEDIR}/.git ]; then
480 rm -rf ${BASEDIR}/.git
481 fi
482 cp -r --verbose ${BACKUPDIR}/.git ${BASEDIR}/
483 fi
3951cc3d 484 ;;
485 * )
486 echo "No changes made. Exiting"
487 exit
488 ;;
489 esac
490}
491
cf6d89bc 492editnote() {
61c91990 493 NOTE=$1
b9f21021 494 # shellcheck disable=SC2155
495 local OK=$(check_noteID "$NOTE")
496 if [ ! "$OK" ]; then
61c91990 497 echo "invalid note \"$NOTE\""
b9f21021 498 echo "Use the note ID that you can fetch after listing your notes"
61c91990 499 exit 1
500 fi
501
b9f21021 502 # shellcheck disable=SC2016,SC2086
61c91990 503 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 504 # shellcheck disable=SC2016,SC2086
61c91990 505 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
44abbfe7 506 if [ "$TITLE" ]; then
507 echo "editing note $TITLE"
b9f21021 508 # shellcheck disable=SC2086,SC2091
b648c006 509 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
d1f115c1 510 gitedit
44abbfe7 511 else
512 echo "note not found"
513 exit 1
514 fi
a4aaf855 515}
cf6d89bc 516listnotes() {
517 # attempt syncing before listing all notes
518 gitsync
fb711183 519 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
520 if [[ $(ls -A "$NOTESDIR") ]]; then
521 if [ $PLAIN == false ]; then
522 echo "listing all notes"
523 echo ""
524 fi
525 [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
526 for i in "${NOTESDIR}"/*; do
527 # shellcheck disable=SC2155
528 local fname=$(basename $i)
529 DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
530 # shellcheck disable=SC2016,SC2086
531 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
532 # shellcheck disable=SC2016,SC2086
533 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
534 [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
535 done
536 else
537 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
538 fi
539}
cf6d89bc 540rmnote() {
026502da 541 # remove eventually existing temp DB file
542 if [[ -f $TMPDB ]]; then
543 rm $TMPDB
b648c006 544 fi
545
026502da 546 NOTE=$1
b9f21021 547 if [ "all" == "$NOTE" ]; then
026502da 548 echo "You're going to delete all notes."
549 read -r -p "Do you wish to continue? (y/N) " ANSWER
550 case $ANSWER in
551 y|Y )
b9f21021 552 # shellcheck disable=SC2086
026502da 553 $JQ 'del(.notes[])' $DB > $TMPDB
b9f21021 554 # shellcheck disable=SC2086
026502da 555 mv $TMPDB $DB
b9f21021 556 # shellcheck disable=SC2086
026502da 557 rm $NOTESDIR/*
d1f115c1 558 gitremove "all"
026502da 559 echo "Deleted all notes"
560 ;;
561 * )
562 echo "Aborting, no notes were deleted."
563 exit 1
564 ;;
565 esac
b648c006 566 else
b9f21021 567 # shellcheck disable=SC2155
568 local OK=$(check_noteID "$NOTE")
569 if [ ! "$OK" ]; then
026502da 570 echo "invalid note \"$NOTE\""
b9f21021 571 echo "Use the note ID that you can fetch after listing your notes"
f1343f20 572 sleep 1
026502da 573 exit 1
574 fi
575
b9f21021 576 # shellcheck disable=SC2016,SC2086
026502da 577 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 578 # shellcheck disable=SC2016,SC2086
026502da 579 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
580 if [ "$TITLE" ]; then
ad818a9d 581 # shellcheck disable=SC2016,SC2086
582 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
583 # shellcheck disable=SC2086
584 mv $TMPDB $DB
585 rm $NOTESDIR/$FILE
d1f115c1 586 gitremove $OK $FILE
ad818a9d 587 echo "Deleted note $TITLE"
f1343f20 588 sleep 1
589 exit
026502da 590 else
591 echo "note not found"
f1343f20 592 sleep 1
026502da 593 exit 1
594 fi
b648c006 595 fi
a4aaf855 596}
cf6d89bc 597shownote() {
ad818a9d 598 NOTE=$1
599
600 # shellcheck disable=SC2155
601 local OK=$(check_noteID "$NOTE")
602 if [ ! "$OK" ]; then
603 echo "invalid note \"$NOTE\""
604 echo "Use the note ID that you can fetch after listing your notes"
605 exit 1
606 fi
607
608 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
609
610 if [ "$FILE" ]; then
611 $PAGER ${NOTESDIR}/${FILE}
612 fi
613}
b9f21021 614# shellcheck disable=SC2006
1f4d7742 615GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,showconf,sync::,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
53f2ed57 616
b9f21021 617# shellcheck disable=SC2181
cb8fcb2f 618if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
53f2ed57 619
620# Note the quotes around `$GOPT': they are essential!
621eval set -- "$GOPT"
ad818a9d 622unset GOPT
53f2ed57 623
624while true; do
625 case "$1" in
626 -h | --help )
627 helptext
628 exit
629 ;;
630 -v | --version )
631 echo $BASENAME v${VERSION}
632 exit
633 ;;
c018122c 634 -p | --plain )
635 PLAIN=true
636 shift
637 ;;
53f2ed57 638 -l | --list )
639 listnotes
640 exit
641 ;;
642 -a | --add )
ad818a9d 643 case "$2" in
644 '' )
645 read -r -p "Title: " TITLE
646 ;;
647 * )
648 TITLE=$2
649 ;;
650 esac
53f2ed57 651 shift 2
652 addnote "$TITLE"
07d42c7a 653 exit
53f2ed57 654 ;;
6c152f7e 655 -e | --edit )
ad818a9d 656 case "$2" in
657 '' )
658 read -r -p "Note ID: " NOTE
659 ;;
660 * )
661 NOTE=$2
662 ;;
663 esac
53f2ed57 664 shift 2
665 editnote "$NOTE"
07d42c7a 666 exit
53f2ed57 667 ;;
b648c006 668 -d | --delete )
ad818a9d 669 case "$2" in
670 '' )
671 read -r -p "Note ID: " NOTE
672 ;;
673 * )
674 NOTE=$2
675 ;;
676 esac
53f2ed57 677 shift 2
678 rmnote "$NOTE"
07d42c7a 679 exit
53f2ed57 680 ;;
ad818a9d 681 -s | --show )
682 case "$2" in
683 '' )
684 read -r -p "Note ID: " NOTE
685 ;;
686 * )
687 NOTE=$2
688 ;;
689 esac
690 shift 2
691 shownote "$NOTE"
07d42c7a 692 exit
ad818a9d 693 ;;
3951cc3d 694 -r | --restore )
695 case "$2" in
696 '' )
697 read -r -p "Backup Dir: " RDIR
698 ;;
699 * )
700 RDIR=$2
701 ;;
702 esac
703 shift 2
704 backup_restore $RDIR
705 exit
706 ;;
4cbcb39e 707 --sync )
1f4d7742 708 case "$2" in
709 '' )
710 gitsync
711 ;;
712 '-f' )
713 gitsync -f
714 ;;
715 * )
716 helptext
717 exit
718 ;;
719 esac
720 shift 2
4cbcb39e 721 exit
722 ;;
d80ac20a 723 --userconf )
724 export_config
b9f21021 725 # shellcheck disable=SC2317
d80ac20a 726 echo "config exported to \"$RCFILE\""
b9f21021 727 # shellcheck disable=SC2317
d80ac20a 728 exit
53f2ed57 729 ;;
1f4d7742 730 --showconf )
731 configtext
732 exit
733 ;;
9eb02251 734 --backup )
735 case "$2" in
736 '' )
737 read -r -p "Backup Dir: " BDIR
738 ;;
739 * )
740 BDIR=$2
741 ;;
742 esac
743 shift 2
744 backup_data $BDIR
745 exit
746 ;;
53f2ed57 747 -- )
026502da 748 shift
749 break
53f2ed57 750 ;;
751 * )
752 break
753 ;;
754 esac
a4aaf855 755done
efa3e607 756
757for arg; do
758 if [ $(check_noteID $arg) ]; then
759 shownote $arg
760 else
761 helptext
762 exit
763 fi
764done