added TODO list
[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
1e68b305 384 # RANDOM TITLE
6a193095 385 RTITLE=$(random_title)
1e68b305 386
387 if [[ -z $1 ]]; then
388 read -r -p "Title: " TITLE
389 case "$TITLE" in
390 '' )
391 NOTETITLE="$RTITLE"
392 ;;
393 * )
394 NOTETITLE=$TITLE
395 ;;
396 esac
397 fi
398
399 # [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
53f2ed57 400 echo "adding new note - \"$NOTETITLE\""
b9f21021 401 # shellcheck disable=SC2086
e3670e83 402 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
403 # [ "" == $LASTID ] && LASTID=0
b9f21021 404 NOTEID=$(( LASTID + 1 ))
405 # shellcheck disable=SC2086
a4aaf855 406 touch ${NOTESDIR}/${NOW}
b9f21021 407 # shellcheck disable=SC2016
a4aaf855 408 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
b9f21021 409 # shellcheck disable=SC2086
a4aaf855 410 mv $TMPDB $DB
b648c006 411 # example for alacritty:
412 # alacritty --class notes --title notes -e /usr/bin/vim ...
b9f21021 413 # shellcheck disable=SC2086,SC2091
e3670e83 414 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
bba0734f 415 # add note to git and push to remote
416 gitadd
a4aaf855 417}
cf6d89bc 418backup_data() {
9eb02251 419 BACKUPDIR="$1"
420 echo "backing up data in $BACKUPDIR"
421
422
423 if [ -d $BACKUPDIR ]; then
424 if [ $(/bin/ls -A $BACKUPDIR) ]; then
425 echo "$BACKUPDIR is not empty. Cannot continue"
426 exit
427 else
428 echo "$BACKUPDIR is ok. Continuing!"
429 fi
430 else
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
434 case $ANSWER in
435 y|Y )
436 mkdir -p $BACKUPDIR
437 ;;
438 * )
439 echo "No changes made. Exiting"
440 exit
441 ;;
442 esac
443 fi
444 # ok, we have a backup directory
445 if [ -r $RCFILE ]; then
87a368fe 446 BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
9eb02251 447 else
87a368fe 448 BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR})
9eb02251 449 fi
450 # run the command
451 if [ "$BCKUP_COMM" ]; then
3951cc3d 452 echo -e "All files backed up."
9eb02251 453 echo -e "BACKUP directory:\t$BACKUPDIR"
3951cc3d 454 tree $BACKUPDIR | $PAGER
9eb02251 455 echo; echo "BACKUP COMPLETED"
456 fi
457}
458
cf6d89bc 459backup_restore() {
3951cc3d 460 BACKUPDIR="$1"
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
464 case $ANSWER in
465 y|Y )
466 # restoring rc file
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)
472 fi
473 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
474 fi
475 # restoring notes directory
476 if [ -d $BACKUPDIR/notes ]; then
477 if [ $(/bin/ls -A $NOTESDIR) ]; then
478 rm --verbose $NOTESDIR/*
479 fi
480 cp -r --verbose $BACKUPDIR/notes $BASEDIR
481 fi
482 # restoring database
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)
488 fi
489 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
490 fi
87a368fe 491 # restoring git repo subdirectory
492 if [ -d $BACKUPDIR/.git ]; then
493 if [ /bin/ls -A ${BASEDIR}/.git ]; then
494 rm -rf ${BASEDIR}/.git
495 fi
496 cp -r --verbose ${BACKUPDIR}/.git ${BASEDIR}/
497 fi
3951cc3d 498 ;;
499 * )
500 echo "No changes made. Exiting"
501 exit
502 ;;
503 esac
504}
505
cf6d89bc 506editnote() {
61c91990 507 NOTE=$1
b9f21021 508 # shellcheck disable=SC2155
509 local OK=$(check_noteID "$NOTE")
510 if [ ! "$OK" ]; then
61c91990 511 echo "invalid note \"$NOTE\""
b9f21021 512 echo "Use the note ID that you can fetch after listing your notes"
61c91990 513 exit 1
514 fi
515
b9f21021 516 # shellcheck disable=SC2016,SC2086
61c91990 517 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 518 # shellcheck disable=SC2016,SC2086
61c91990 519 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
44abbfe7 520 if [ "$TITLE" ]; then
521 echo "editing note $TITLE"
b9f21021 522 # shellcheck disable=SC2086,SC2091
b648c006 523 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
d1f115c1 524 gitedit
44abbfe7 525 else
526 echo "note not found"
527 exit 1
528 fi
a4aaf855 529}
cf6d89bc 530listnotes() {
531 # attempt syncing before listing all notes
532 gitsync
fb711183 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"
537 echo ""
538 fi
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}"
549 done
550 else
551 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
552 fi
553}
cf6d89bc 554rmnote() {
026502da 555 # remove eventually existing temp DB file
556 if [[ -f $TMPDB ]]; then
557 rm $TMPDB
b648c006 558 fi
559
026502da 560 NOTE=$1
b9f21021 561 if [ "all" == "$NOTE" ]; then
026502da 562 echo "You're going to delete all notes."
563 read -r -p "Do you wish to continue? (y/N) " ANSWER
564 case $ANSWER in
565 y|Y )
b9f21021 566 # shellcheck disable=SC2086
026502da 567 $JQ 'del(.notes[])' $DB > $TMPDB
b9f21021 568 # shellcheck disable=SC2086
026502da 569 mv $TMPDB $DB
b9f21021 570 # shellcheck disable=SC2086
026502da 571 rm $NOTESDIR/*
d1f115c1 572 gitremove "all"
026502da 573 echo "Deleted all notes"
574 ;;
575 * )
576 echo "Aborting, no notes were deleted."
577 exit 1
578 ;;
579 esac
b648c006 580 else
b9f21021 581 # shellcheck disable=SC2155
582 local OK=$(check_noteID "$NOTE")
583 if [ ! "$OK" ]; then
026502da 584 echo "invalid note \"$NOTE\""
b9f21021 585 echo "Use the note ID that you can fetch after listing your notes"
f1343f20 586 sleep 1
026502da 587 exit 1
588 fi
589
b9f21021 590 # shellcheck disable=SC2016,SC2086
026502da 591 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 592 # shellcheck disable=SC2016,SC2086
026502da 593 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
594 if [ "$TITLE" ]; then
ad818a9d 595 # shellcheck disable=SC2016,SC2086
596 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
597 # shellcheck disable=SC2086
598 mv $TMPDB $DB
599 rm $NOTESDIR/$FILE
d1f115c1 600 gitremove $OK $FILE
ad818a9d 601 echo "Deleted note $TITLE"
f1343f20 602 sleep 1
603 exit
026502da 604 else
605 echo "note not found"
f1343f20 606 sleep 1
026502da 607 exit 1
608 fi
b648c006 609 fi
a4aaf855 610}
cf6d89bc 611shownote() {
ad818a9d 612 NOTE=$1
613
614 # shellcheck disable=SC2155
615 local OK=$(check_noteID "$NOTE")
616 if [ ! "$OK" ]; then
617 echo "invalid note \"$NOTE\""
618 echo "Use the note ID that you can fetch after listing your notes"
619 exit 1
620 fi
621
622 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
623
624 if [ "$FILE" ]; then
625 $PAGER ${NOTESDIR}/${FILE}
626 fi
627}
b9f21021 628# shellcheck disable=SC2006
1e68b305 629GOPT=$(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 630
b9f21021 631# shellcheck disable=SC2181
cb8fcb2f 632if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
53f2ed57 633
634# Note the quotes around `$GOPT': they are essential!
635eval set -- "$GOPT"
ad818a9d 636unset GOPT
53f2ed57 637
638while true; do
639 case "$1" in
640 -h | --help )
641 helptext
642 exit
643 ;;
644 -v | --version )
645 echo $BASENAME v${VERSION}
646 exit
647 ;;
c018122c 648 -p | --plain )
649 PLAIN=true
650 shift
651 ;;
53f2ed57 652 -l | --list )
653 listnotes
654 exit
655 ;;
656 -a | --add )
79f10f4e 657 TITLE=$2
53f2ed57 658 shift 2
659 addnote "$TITLE"
07d42c7a 660 exit
53f2ed57 661 ;;
6c152f7e 662 -e | --edit )
79f10f4e 663 NOTE=$2
53f2ed57 664 shift 2
665 editnote "$NOTE"
07d42c7a 666 exit
53f2ed57 667 ;;
b648c006 668 -d | --delete )
79f10f4e 669 NOTE=$2
53f2ed57 670 shift 2
671 rmnote "$NOTE"
07d42c7a 672 exit
53f2ed57 673 ;;
ad818a9d 674 -s | --show )
79f10f4e 675 NOTE=$2
ad818a9d 676 shift 2
677 shownote "$NOTE"
07d42c7a 678 exit
ad818a9d 679 ;;
3951cc3d 680 -r | --restore )
79f10f4e 681 RDIR=$2
3951cc3d 682 shift 2
683 backup_restore $RDIR
684 exit
685 ;;
4cbcb39e 686 --sync )
da52e73a 687 # I'm forcing it because if you run it manually, chances are that you need to.
688 gitsync -f
689 shift
4cbcb39e 690 exit
691 ;;
d80ac20a 692 --userconf )
693 export_config
b9f21021 694 # shellcheck disable=SC2317
d80ac20a 695 echo "config exported to \"$RCFILE\""
b9f21021 696 # shellcheck disable=SC2317
d80ac20a 697 exit
53f2ed57 698 ;;
1f4d7742 699 --showconf )
700 configtext
701 exit
702 ;;
9eb02251 703 --backup )
79f10f4e 704 BDIR=$2
9eb02251 705 shift 2
706 backup_data $BDIR
707 exit
708 ;;
53f2ed57 709 -- )
026502da 710 shift
711 break
53f2ed57 712 ;;
713 * )
714 break
715 ;;
716 esac
a4aaf855 717done
efa3e607 718
719for arg; do
720 if [ $(check_noteID $arg) ]; then
721 shownote $arg
722 else
723 helptext
724 exit
725 fi
726done