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 |
9 | if [[ $DEBUG == true ]]; then |
10 | exec 5> /tmp/debug_bash-notes.log |
11 | BASH_XTRACEFD="5" |
12 | PS4='$LINENO: ' |
13 | set -x |
14 | fi |
a4aaf855 |
15 | |
16 | PID=$$ |
ad818a9d |
17 | BASENAME=$( basename "$0" ) |
18 | NOW=$(date +%s) |
19 | |
20 | VERSION="0.3" |
21 | DBVERSION=${VERSION}_${NOW} |
53f2ed57 |
22 | |
d80ac20a |
23 | set_defaults() { |
24 | # Binaries to use |
cb8fcb2f |
25 | JQ=${JQ:-/usr/bin/jq} |
a4aaf855 |
26 | EDITOR=${EDITOR:-/usr/bin/vim} |
53f2ed57 |
27 | TERMINAL=${TERMINAL:-/usr/bin/alacritty} |
b648c006 |
28 | # add options for your terminal. Remember to add the last option to execute |
29 | # your editor program, otherwise the script will fail. |
30 | # see example in the addnote function |
e3670e83 |
31 | TERM_OPTS="--class notes --title notes -e " |
ad818a9d |
32 | # Setting PAGER here overrides whatever is set in your default shell |
33 | # comment this option to use your default pager if set in your shell. |
34 | PAGER=${PAGER:-/usr/bin/more} |
53f2ed57 |
35 | |
cb8fcb2f |
36 | # set this to true to have output in plain text |
37 | # or use the -p option on the command line before every other option |
38 | PLAIN=false |
d80ac20a |
39 | # base directory for program files |
53f2ed57 |
40 | BASEDIR=${BASEDIR:-~/.local/share/bash-notes} |
d80ac20a |
41 | # notes database in json format |
a4aaf855 |
42 | DB=${BASEDIR}/db.json |
d80ac20a |
43 | # directory containing the actual notes |
a4aaf855 |
44 | NOTESDIR=${BASEDIR}/notes |
53f2ed57 |
45 | |
d80ac20a |
46 | } # end set_defaults, do not change this line. |
47 | |
48 | set_defaults |
49 | |
50 | # Do not edit below this point |
51 | RCFILE=${RCFILE:-~/.config/bash-notes.rc} |
53f2ed57 |
52 | TMPDB=/tmp/db.json |
a4aaf855 |
53 | |
b9f21021 |
54 | if [ ! -x "$JQ" ]; then |
a4aaf855 |
55 | echo "jq not found in your PATH" |
56 | echo "install jq to continue" |
57 | exit 1 |
58 | fi |
59 | |
53f2ed57 |
60 | # IMPORT USER DEFINED OPTIONS IF ANY |
61 | if [[ -f $RCFILE ]]; then |
b9f21021 |
62 | # shellcheck disable=SC1090 |
63 | source "$RCFILE" |
53f2ed57 |
64 | fi |
65 | |
a4aaf855 |
66 | # We prevent the program from running more than one instance: |
b9f21021 |
67 | PIDFILE=/var/tmp/$(basename "$0" .sh).pid |
a4aaf855 |
68 | |
69 | # Make sure the PID file is removed when we kill the process |
70 | trap 'rm -f $PIDFILE; exit 1' TERM INT |
71 | |
72 | if [[ -r $PIDFILE ]]; then |
73 | # PIDFILE exists, so I guess there's already an instance running |
74 | # let's kill it and run again |
b9f21021 |
75 | # shellcheck disable=SC2046,SC2086 |
a4aaf855 |
76 | kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1 |
77 | # should already be deleted by trap, but just to be sure |
b9f21021 |
78 | rm "$PIDFILE" |
a4aaf855 |
79 | fi |
80 | |
81 | # create PIDFILE |
b9f21021 |
82 | echo $PID > "$PIDFILE" |
a4aaf855 |
83 | |
fb711183 |
84 | # Export config to file |
85 | function export_config() { |
86 | if [ -r ${RCFILE} ]; then |
87 | echo "Backing up current '${RCFILE}'...." |
88 | mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) |
89 | fi |
90 | echo "Writing '${RCFILE}'...." |
91 | sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \ |
92 | | grep -v set_defaults \ |
93 | | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \ |
94 | > ${RCFILE} |
95 | if [ -r ${RCFILE} ]; then |
96 | echo "Taking no further action." |
97 | exit 0 |
98 | else |
99 | echo "Could not write '${RCFILE}'...!" |
100 | exit 1 |
101 | fi |
102 | } |
103 | |
104 | # we should expand on this function to add a sample note and explain a little bit |
105 | # how the program works. |
106 | function firstrun() { |
107 | [ -f $RCFILE ] && RC=$RCFILE || RC="none" |
108 | |
109 | clear |
110 | echo "${BASENAME} configuration: |
111 | |
112 | base directory: ${BASEDIR}/ |
113 | notes archive: ${NOTESDIR}/ |
114 | notes database: ${DB} |
115 | rc file: $RC |
116 | text editor: ${EDITOR} |
117 | terminal: ${TERMINAL} |
118 | jq executable: ${JQ} |
119 | " |
120 | |
9eb02251 |
121 | echo "Now I'll create the needed files and directories." |
fb711183 |
122 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
123 | case $ANSWER in |
124 | y|Y ) |
125 | mkdir -p $NOTESDIR |
126 | cat << __EOL__ > ${DB} |
127 | { |
128 | "params": { |
129 | "version": "${VERSION}", |
130 | "dbversion": "${DBVERSION}" |
131 | }, |
132 | "notes": [] |
133 | } |
134 | __EOL__ |
135 | echo; echo "All done, you can now write your first note." |
136 | ;; |
137 | * ) |
138 | echo "No changes made. Exiting" |
139 | exit |
140 | ;; |
141 | esac |
142 | } |
143 | |
144 | # check for notes dir existance and create it in case it doesn't exists |
145 | if [[ ! -d $NOTESDIR ]]; then |
146 | # we don't have a directory. FIRST RUN? |
147 | firstrun |
148 | fi |
61c91990 |
149 | # check if input is a number, returns false or the number itself |
150 | function check_noteID() { |
151 | IN=$1 |
152 | case $IN in |
153 | ''|*[!0-9]*) |
154 | return 1 |
155 | ;; |
156 | *) |
b9f21021 |
157 | echo "$IN" |
61c91990 |
158 | ;; |
159 | esac |
160 | } |
161 | |
a4aaf855 |
162 | function helptext() { |
d80ac20a |
163 | echo "Usage:" |
c018122c |
164 | echo " $0 [PARAMS] ..." |
d80ac20a |
165 | echo "" |
e3670e83 |
166 | echo "${BASENAME} parameters are:" |
9eb02251 |
167 | echo -e " -h | --help\t\t\t: This help text" |
168 | echo -e " -p | --plain\t\t\t: Output is in plain text" |
169 | echo -e "\t\t\t\t (without this option the output is formatted)" |
170 | echo -e "\t\t\t\t (this option must precede all others)" |
171 | echo -e " -l | --list\t\t\t: List existing notes" |
3951cc3d |
172 | echo -e " -a | --add=[\"<title>\"]\t: Add new note" |
173 | echo -e " -e | --edit=[<note>]\t\t: Edit note" |
174 | echo -e " -d | --delete=[<note> | all] : Delete single note or all notes at once" |
175 | echo -e " -s | --show=[<note>]\t\t: Display note using your favourite PAGER" |
176 | echo -e " -r | --restore=[<dir>]\t: Restore a previous backup from dir" |
9eb02251 |
177 | echo -e " -v | --version\t\t: Print version" |
178 | echo -e " --userconf\t\t\t: Export User config file" |
179 | echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder" |
e3670e83 |
180 | echo "" |
a4aaf855 |
181 | } |
9eb02251 |
182 | |
3951cc3d |
183 | function configtext() { |
184 | cat << __NOWCONF__ |
185 | ${BASENAME} configuration is: |
186 | |
187 | base directory: ${BASEDIR}/ |
188 | notes archive: ${NOTESDIR}/ |
189 | notes database: ${DB} |
190 | rc file: $RCFILE |
191 | debug file: /tmp/debug_bash-note.log |
192 | |
193 | text editor: ${EDITOR} |
194 | terminal: ${TERMINAL} |
195 | jq executable: ${JQ} |
196 | PAGER: ${PAGER} |
197 | __NOWCONF__ |
198 | |
199 | } |
200 | |
a4aaf855 |
201 | function addnote() { |
026502da |
202 | # remove eventually existing temp DB file |
203 | if [[ -f $TMPDB ]]; then |
204 | rm $TMPDB |
205 | fi |
206 | |
53f2ed57 |
207 | NOTETITLE="$1" |
208 | echo "adding new note - \"$NOTETITLE\"" |
b9f21021 |
209 | # shellcheck disable=SC2086 |
e3670e83 |
210 | LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) |
211 | # [ "" == $LASTID ] && LASTID=0 |
b9f21021 |
212 | NOTEID=$(( LASTID + 1 )) |
213 | # shellcheck disable=SC2086 |
a4aaf855 |
214 | touch ${NOTESDIR}/${NOW} |
b9f21021 |
215 | # shellcheck disable=SC2016 |
a4aaf855 |
216 | $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB |
b9f21021 |
217 | # shellcheck disable=SC2086 |
a4aaf855 |
218 | mv $TMPDB $DB |
b648c006 |
219 | # example for alacritty: |
220 | # alacritty --class notes --title notes -e /usr/bin/vim ... |
b9f21021 |
221 | # shellcheck disable=SC2086,SC2091 |
e3670e83 |
222 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) |
a4aaf855 |
223 | } |
9eb02251 |
224 | function backup_data() { |
225 | BACKUPDIR="$1" |
226 | echo "backing up data in $BACKUPDIR" |
227 | |
228 | |
229 | if [ -d $BACKUPDIR ]; then |
230 | if [ $(/bin/ls -A $BACKUPDIR) ]; then |
231 | echo "$BACKUPDIR is not empty. Cannot continue" |
232 | exit |
233 | else |
234 | echo "$BACKUPDIR is ok. Continuing!" |
235 | fi |
236 | else |
237 | # BACKUPDIR doesn't exists |
238 | echo "$BACKUPDIR doesn't exists" |
239 | read -r -p "Do you want me to create it for you? (y/N) " ANSWER |
240 | case $ANSWER in |
241 | y|Y ) |
242 | mkdir -p $BACKUPDIR |
243 | ;; |
244 | * ) |
245 | echo "No changes made. Exiting" |
246 | exit |
247 | ;; |
248 | esac |
249 | fi |
250 | # ok, we have a backup directory |
251 | if [ -r $RCFILE ]; then |
3951cc3d |
252 | BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/* ${BACKUPDIR}) |
9eb02251 |
253 | else |
254 | BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/* ${BACKUPDIR}) |
255 | fi |
256 | # run the command |
257 | if [ "$BCKUP_COMM" ]; then |
3951cc3d |
258 | echo -e "All files backed up." |
9eb02251 |
259 | echo -e "BACKUP directory:\t$BACKUPDIR" |
3951cc3d |
260 | tree $BACKUPDIR | $PAGER |
9eb02251 |
261 | echo; echo "BACKUP COMPLETED" |
262 | fi |
263 | } |
264 | |
3951cc3d |
265 | function backup_restore() { |
266 | BACKUPDIR="$1" |
267 | echo "restoring backup from $BACKUPDIR" |
268 | echo "This will overwrite all your notes and configurations with the backup." |
269 | read -r -p "Do you want to continue? (y/N) " ANSWER |
270 | case $ANSWER in |
271 | y|Y ) |
272 | # restoring rc file |
273 | BACKUPRC=$(basename $RCFILE) |
274 | if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then |
275 | if [ -r ${RCFILE} ]; then |
276 | echo "Backing up current '${RCFILE}'...." |
277 | mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) |
278 | fi |
279 | cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE |
280 | fi |
281 | # restoring notes directory |
282 | if [ -d $BACKUPDIR/notes ]; then |
283 | if [ $(/bin/ls -A $NOTESDIR) ]; then |
284 | rm --verbose $NOTESDIR/* |
285 | fi |
286 | cp -r --verbose $BACKUPDIR/notes $BASEDIR |
287 | fi |
288 | # restoring database |
289 | BACKUPDB=$(basename $DB) |
290 | if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then |
291 | if [ -r ${DB} ]; then |
292 | echo "Backing up current '${DB}'...." |
293 | mv -f ${DB} ${DB}.$(date +%Y%m%d_%H%M) |
294 | fi |
295 | cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB |
296 | fi |
297 | ;; |
298 | * ) |
299 | echo "No changes made. Exiting" |
300 | exit |
301 | ;; |
302 | esac |
303 | } |
304 | |
a4aaf855 |
305 | function editnote() { |
61c91990 |
306 | NOTE=$1 |
b9f21021 |
307 | # shellcheck disable=SC2155 |
308 | local OK=$(check_noteID "$NOTE") |
309 | if [ ! "$OK" ]; then |
61c91990 |
310 | echo "invalid note \"$NOTE\"" |
b9f21021 |
311 | echo "Use the note ID that you can fetch after listing your notes" |
61c91990 |
312 | exit 1 |
313 | fi |
314 | |
b9f21021 |
315 | # shellcheck disable=SC2016,SC2086 |
61c91990 |
316 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
b9f21021 |
317 | # shellcheck disable=SC2016,SC2086 |
61c91990 |
318 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
44abbfe7 |
319 | if [ "$TITLE" ]; then |
320 | echo "editing note $TITLE" |
b9f21021 |
321 | # shellcheck disable=SC2086,SC2091 |
b648c006 |
322 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE}) |
44abbfe7 |
323 | else |
324 | echo "note not found" |
325 | exit 1 |
326 | fi |
a4aaf855 |
327 | } |
fb711183 |
328 | function listnotes() { |
329 | # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored" |
330 | if [[ $(ls -A "$NOTESDIR") ]]; then |
331 | if [ $PLAIN == false ]; then |
332 | echo "listing all notes" |
333 | echo "" |
334 | fi |
335 | [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]" |
336 | for i in "${NOTESDIR}"/*; do |
337 | # shellcheck disable=SC2155 |
338 | local fname=$(basename $i) |
339 | DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z") |
340 | # shellcheck disable=SC2016,SC2086 |
341 | TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB) |
342 | # shellcheck disable=SC2016,SC2086 |
343 | ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB) |
344 | [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}" |
345 | done |
346 | else |
347 | echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\"" |
348 | fi |
349 | } |
a4aaf855 |
350 | function rmnote() { |
026502da |
351 | # remove eventually existing temp DB file |
352 | if [[ -f $TMPDB ]]; then |
353 | rm $TMPDB |
b648c006 |
354 | fi |
355 | |
026502da |
356 | NOTE=$1 |
b9f21021 |
357 | if [ "all" == "$NOTE" ]; then |
026502da |
358 | echo "You're going to delete all notes." |
359 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
360 | case $ANSWER in |
361 | y|Y ) |
b9f21021 |
362 | # shellcheck disable=SC2086 |
026502da |
363 | $JQ 'del(.notes[])' $DB > $TMPDB |
b9f21021 |
364 | # shellcheck disable=SC2086 |
026502da |
365 | mv $TMPDB $DB |
b9f21021 |
366 | # shellcheck disable=SC2086 |
026502da |
367 | rm $NOTESDIR/* |
368 | echo "Deleted all notes" |
369 | ;; |
370 | * ) |
371 | echo "Aborting, no notes were deleted." |
372 | exit 1 |
373 | ;; |
374 | esac |
b648c006 |
375 | else |
b9f21021 |
376 | # shellcheck disable=SC2155 |
377 | local OK=$(check_noteID "$NOTE") |
378 | if [ ! "$OK" ]; then |
026502da |
379 | echo "invalid note \"$NOTE\"" |
b9f21021 |
380 | echo "Use the note ID that you can fetch after listing your notes" |
026502da |
381 | exit 1 |
382 | fi |
383 | |
b9f21021 |
384 | # shellcheck disable=SC2016,SC2086 |
026502da |
385 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
b9f21021 |
386 | # shellcheck disable=SC2016,SC2086 |
026502da |
387 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
388 | if [ "$TITLE" ]; then |
ad818a9d |
389 | # shellcheck disable=SC2016,SC2086 |
390 | $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB |
391 | # shellcheck disable=SC2086 |
392 | mv $TMPDB $DB |
393 | rm $NOTESDIR/$FILE |
394 | echo "Deleted note $TITLE" |
026502da |
395 | else |
396 | echo "note not found" |
397 | exit 1 |
398 | fi |
b648c006 |
399 | fi |
a4aaf855 |
400 | } |
ad818a9d |
401 | function shownote() { |
402 | NOTE=$1 |
403 | |
404 | # shellcheck disable=SC2155 |
405 | local OK=$(check_noteID "$NOTE") |
406 | if [ ! "$OK" ]; then |
407 | echo "invalid note \"$NOTE\"" |
408 | echo "Use the note ID that you can fetch after listing your notes" |
409 | exit 1 |
410 | fi |
411 | |
412 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
413 | |
414 | if [ "$FILE" ]; then |
415 | $PAGER ${NOTESDIR}/${FILE} |
416 | fi |
417 | } |
b9f21021 |
418 | # shellcheck disable=SC2006 |
3951cc3d |
419 | GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@") |
53f2ed57 |
420 | |
b9f21021 |
421 | # shellcheck disable=SC2181 |
cb8fcb2f |
422 | if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi |
53f2ed57 |
423 | |
424 | # Note the quotes around `$GOPT': they are essential! |
425 | eval set -- "$GOPT" |
ad818a9d |
426 | unset GOPT |
53f2ed57 |
427 | |
428 | while true; do |
429 | case "$1" in |
430 | -h | --help ) |
431 | helptext |
432 | exit |
433 | ;; |
434 | -v | --version ) |
435 | echo $BASENAME v${VERSION} |
436 | exit |
437 | ;; |
c018122c |
438 | -p | --plain ) |
439 | PLAIN=true |
440 | shift |
441 | ;; |
53f2ed57 |
442 | -l | --list ) |
443 | listnotes |
444 | exit |
445 | ;; |
446 | -a | --add ) |
ad818a9d |
447 | case "$2" in |
448 | '' ) |
449 | read -r -p "Title: " TITLE |
450 | ;; |
451 | * ) |
452 | TITLE=$2 |
453 | ;; |
454 | esac |
53f2ed57 |
455 | shift 2 |
456 | addnote "$TITLE" |
07d42c7a |
457 | exit |
53f2ed57 |
458 | ;; |
6c152f7e |
459 | -e | --edit ) |
ad818a9d |
460 | case "$2" in |
461 | '' ) |
462 | read -r -p "Note ID: " NOTE |
463 | ;; |
464 | * ) |
465 | NOTE=$2 |
466 | ;; |
467 | esac |
53f2ed57 |
468 | shift 2 |
469 | editnote "$NOTE" |
07d42c7a |
470 | exit |
53f2ed57 |
471 | ;; |
b648c006 |
472 | -d | --delete ) |
ad818a9d |
473 | case "$2" in |
474 | '' ) |
475 | read -r -p "Note ID: " NOTE |
476 | ;; |
477 | * ) |
478 | NOTE=$2 |
479 | ;; |
480 | esac |
53f2ed57 |
481 | shift 2 |
482 | rmnote "$NOTE" |
07d42c7a |
483 | exit |
53f2ed57 |
484 | ;; |
ad818a9d |
485 | -s | --show ) |
486 | case "$2" in |
487 | '' ) |
488 | read -r -p "Note ID: " NOTE |
489 | ;; |
490 | * ) |
491 | NOTE=$2 |
492 | ;; |
493 | esac |
494 | shift 2 |
495 | shownote "$NOTE" |
07d42c7a |
496 | exit |
ad818a9d |
497 | ;; |
3951cc3d |
498 | -r | --restore ) |
499 | case "$2" in |
500 | '' ) |
501 | read -r -p "Backup Dir: " RDIR |
502 | ;; |
503 | * ) |
504 | RDIR=$2 |
505 | ;; |
506 | esac |
507 | shift 2 |
508 | backup_restore $RDIR |
509 | exit |
510 | ;; |
d80ac20a |
511 | --userconf ) |
512 | export_config |
b9f21021 |
513 | # shellcheck disable=SC2317 |
d80ac20a |
514 | echo "config exported to \"$RCFILE\"" |
b9f21021 |
515 | # shellcheck disable=SC2317 |
d80ac20a |
516 | exit |
53f2ed57 |
517 | ;; |
9eb02251 |
518 | --backup ) |
519 | case "$2" in |
520 | '' ) |
521 | read -r -p "Backup Dir: " BDIR |
522 | ;; |
523 | * ) |
524 | BDIR=$2 |
525 | ;; |
526 | esac |
527 | shift 2 |
528 | backup_data $BDIR |
529 | exit |
530 | ;; |
53f2ed57 |
531 | -- ) |
026502da |
532 | shift |
533 | break |
53f2ed57 |
534 | ;; |
535 | * ) |
536 | break |
537 | ;; |
538 | esac |
a4aaf855 |
539 | done |