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 "" |
166 | cat << __NOWCONF__ |
167 | ${BASENAME} configuration is: |
168 | |
169 | base directory: ${BASEDIR}/ |
170 | notes archive: ${NOTESDIR}/ |
171 | notes database: ${DB} |
172 | rc file: $RCFILE |
cb8fcb2f |
173 | debug file: /tmp/debug_bash-note.log |
c018122c |
174 | |
d80ac20a |
175 | text editor: ${EDITOR} |
176 | terminal: ${TERMINAL} |
177 | jq executable: ${JQ} |
9eb02251 |
178 | PAGER: ${PAGER} |
d80ac20a |
179 | __NOWCONF__ |
180 | |
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" |
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 " -v | --version\t\t: Print version" |
193 | echo -e " --userconf\t\t\t: Export User config file" |
194 | echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder" |
e3670e83 |
195 | echo "" |
a4aaf855 |
196 | } |
9eb02251 |
197 | |
a4aaf855 |
198 | function addnote() { |
026502da |
199 | # remove eventually existing temp DB file |
200 | if [[ -f $TMPDB ]]; then |
201 | rm $TMPDB |
202 | fi |
203 | |
53f2ed57 |
204 | NOTETITLE="$1" |
205 | echo "adding new note - \"$NOTETITLE\"" |
b9f21021 |
206 | # shellcheck disable=SC2086 |
e3670e83 |
207 | LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) |
208 | # [ "" == $LASTID ] && LASTID=0 |
b9f21021 |
209 | NOTEID=$(( LASTID + 1 )) |
210 | # shellcheck disable=SC2086 |
a4aaf855 |
211 | touch ${NOTESDIR}/${NOW} |
b9f21021 |
212 | # shellcheck disable=SC2016 |
a4aaf855 |
213 | $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB |
b9f21021 |
214 | # shellcheck disable=SC2086 |
a4aaf855 |
215 | mv $TMPDB $DB |
b648c006 |
216 | # example for alacritty: |
217 | # alacritty --class notes --title notes -e /usr/bin/vim ... |
b9f21021 |
218 | # shellcheck disable=SC2086,SC2091 |
e3670e83 |
219 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) |
a4aaf855 |
220 | } |
9eb02251 |
221 | function backup_data() { |
222 | BACKUPDIR="$1" |
223 | echo "backing up data in $BACKUPDIR" |
224 | |
225 | |
226 | if [ -d $BACKUPDIR ]; then |
227 | if [ $(/bin/ls -A $BACKUPDIR) ]; then |
228 | echo "$BACKUPDIR is not empty. Cannot continue" |
229 | exit |
230 | else |
231 | echo "$BACKUPDIR is ok. Continuing!" |
232 | fi |
233 | else |
234 | # BACKUPDIR doesn't exists |
235 | echo "$BACKUPDIR doesn't exists" |
236 | read -r -p "Do you want me to create it for you? (y/N) " ANSWER |
237 | case $ANSWER in |
238 | y|Y ) |
239 | mkdir -p $BACKUPDIR |
240 | ;; |
241 | * ) |
242 | echo "No changes made. Exiting" |
243 | exit |
244 | ;; |
245 | esac |
246 | fi |
247 | # ok, we have a backup directory |
248 | if [ -r $RCFILE ]; then |
249 | BCKUP_COMM=$(rsync -avz --progress ${RCFILE} ${BASEDIR}/* ${BACKUPDIR}) |
250 | else |
251 | BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/* ${BACKUPDIR}) |
252 | fi |
253 | # run the command |
254 | if [ "$BCKUP_COMM" ]; then |
255 | echo -e "BASE directory:\t\t$BASEDIR" |
256 | echo -e "BACKUP directory:\t$BACKUPDIR" |
257 | echo; echo "BACKUP COMPLETED" |
258 | fi |
259 | } |
260 | |
a4aaf855 |
261 | function editnote() { |
61c91990 |
262 | NOTE=$1 |
b9f21021 |
263 | # shellcheck disable=SC2155 |
264 | local OK=$(check_noteID "$NOTE") |
265 | if [ ! "$OK" ]; then |
61c91990 |
266 | echo "invalid note \"$NOTE\"" |
b9f21021 |
267 | echo "Use the note ID that you can fetch after listing your notes" |
61c91990 |
268 | exit 1 |
269 | fi |
270 | |
b9f21021 |
271 | # shellcheck disable=SC2016,SC2086 |
61c91990 |
272 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
b9f21021 |
273 | # shellcheck disable=SC2016,SC2086 |
61c91990 |
274 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
44abbfe7 |
275 | if [ "$TITLE" ]; then |
276 | echo "editing note $TITLE" |
b9f21021 |
277 | # shellcheck disable=SC2086,SC2091 |
b648c006 |
278 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE}) |
44abbfe7 |
279 | else |
280 | echo "note not found" |
281 | exit 1 |
282 | fi |
a4aaf855 |
283 | } |
fb711183 |
284 | function listnotes() { |
285 | # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored" |
286 | if [[ $(ls -A "$NOTESDIR") ]]; then |
287 | if [ $PLAIN == false ]; then |
288 | echo "listing all notes" |
289 | echo "" |
290 | fi |
291 | [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]" |
292 | for i in "${NOTESDIR}"/*; do |
293 | # shellcheck disable=SC2155 |
294 | local fname=$(basename $i) |
295 | DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z") |
296 | # shellcheck disable=SC2016,SC2086 |
297 | TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB) |
298 | # shellcheck disable=SC2016,SC2086 |
299 | ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB) |
300 | [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}" |
301 | done |
302 | else |
303 | echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\"" |
304 | fi |
305 | } |
a4aaf855 |
306 | function rmnote() { |
026502da |
307 | # remove eventually existing temp DB file |
308 | if [[ -f $TMPDB ]]; then |
309 | rm $TMPDB |
b648c006 |
310 | fi |
311 | |
026502da |
312 | NOTE=$1 |
b9f21021 |
313 | if [ "all" == "$NOTE" ]; then |
026502da |
314 | echo "You're going to delete all notes." |
315 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
316 | case $ANSWER in |
317 | y|Y ) |
b9f21021 |
318 | # shellcheck disable=SC2086 |
026502da |
319 | $JQ 'del(.notes[])' $DB > $TMPDB |
b9f21021 |
320 | # shellcheck disable=SC2086 |
026502da |
321 | mv $TMPDB $DB |
b9f21021 |
322 | # shellcheck disable=SC2086 |
026502da |
323 | rm $NOTESDIR/* |
324 | echo "Deleted all notes" |
325 | ;; |
326 | * ) |
327 | echo "Aborting, no notes were deleted." |
328 | exit 1 |
329 | ;; |
330 | esac |
b648c006 |
331 | else |
b9f21021 |
332 | # shellcheck disable=SC2155 |
333 | local OK=$(check_noteID "$NOTE") |
334 | if [ ! "$OK" ]; then |
026502da |
335 | echo "invalid note \"$NOTE\"" |
b9f21021 |
336 | echo "Use the note ID that you can fetch after listing your notes" |
026502da |
337 | exit 1 |
338 | fi |
339 | |
b9f21021 |
340 | # shellcheck disable=SC2016,SC2086 |
026502da |
341 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
b9f21021 |
342 | # shellcheck disable=SC2016,SC2086 |
026502da |
343 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
344 | if [ "$TITLE" ]; then |
ad818a9d |
345 | # shellcheck disable=SC2016,SC2086 |
346 | $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB |
347 | # shellcheck disable=SC2086 |
348 | mv $TMPDB $DB |
349 | rm $NOTESDIR/$FILE |
350 | echo "Deleted note $TITLE" |
026502da |
351 | else |
352 | echo "note not found" |
353 | exit 1 |
354 | fi |
b648c006 |
355 | fi |
a4aaf855 |
356 | } |
ad818a9d |
357 | function shownote() { |
358 | NOTE=$1 |
359 | |
360 | # shellcheck disable=SC2155 |
361 | local OK=$(check_noteID "$NOTE") |
362 | if [ ! "$OK" ]; then |
363 | echo "invalid note \"$NOTE\"" |
364 | echo "Use the note ID that you can fetch after listing your notes" |
365 | exit 1 |
366 | fi |
367 | |
368 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
369 | |
370 | if [ "$FILE" ]; then |
371 | $PAGER ${NOTESDIR}/${FILE} |
372 | fi |
373 | } |
b9f21021 |
374 | # shellcheck disable=SC2006 |
ad818a9d |
375 | GOPT=$(getopt -o hvpla::e::d::s:: --long help,version,list,plain,userconf,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@") |
53f2ed57 |
376 | |
b9f21021 |
377 | # shellcheck disable=SC2181 |
cb8fcb2f |
378 | if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi |
53f2ed57 |
379 | |
380 | # Note the quotes around `$GOPT': they are essential! |
381 | eval set -- "$GOPT" |
ad818a9d |
382 | unset GOPT |
53f2ed57 |
383 | |
384 | while true; do |
385 | case "$1" in |
386 | -h | --help ) |
387 | helptext |
388 | exit |
389 | ;; |
390 | -v | --version ) |
391 | echo $BASENAME v${VERSION} |
392 | exit |
393 | ;; |
c018122c |
394 | -p | --plain ) |
395 | PLAIN=true |
396 | shift |
397 | ;; |
53f2ed57 |
398 | -l | --list ) |
399 | listnotes |
400 | exit |
401 | ;; |
402 | -a | --add ) |
ad818a9d |
403 | case "$2" in |
404 | '' ) |
405 | read -r -p "Title: " TITLE |
406 | ;; |
407 | * ) |
408 | TITLE=$2 |
409 | ;; |
410 | esac |
53f2ed57 |
411 | shift 2 |
412 | addnote "$TITLE" |
07d42c7a |
413 | exit |
53f2ed57 |
414 | ;; |
6c152f7e |
415 | -e | --edit ) |
ad818a9d |
416 | case "$2" in |
417 | '' ) |
418 | read -r -p "Note ID: " NOTE |
419 | ;; |
420 | * ) |
421 | NOTE=$2 |
422 | ;; |
423 | esac |
53f2ed57 |
424 | shift 2 |
425 | editnote "$NOTE" |
07d42c7a |
426 | exit |
53f2ed57 |
427 | ;; |
b648c006 |
428 | -d | --delete ) |
ad818a9d |
429 | case "$2" in |
430 | '' ) |
431 | read -r -p "Note ID: " NOTE |
432 | ;; |
433 | * ) |
434 | NOTE=$2 |
435 | ;; |
436 | esac |
53f2ed57 |
437 | shift 2 |
438 | rmnote "$NOTE" |
07d42c7a |
439 | exit |
53f2ed57 |
440 | ;; |
ad818a9d |
441 | -s | --show ) |
442 | case "$2" in |
443 | '' ) |
444 | read -r -p "Note ID: " NOTE |
445 | ;; |
446 | * ) |
447 | NOTE=$2 |
448 | ;; |
449 | esac |
450 | shift 2 |
451 | shownote "$NOTE" |
07d42c7a |
452 | exit |
ad818a9d |
453 | ;; |
d80ac20a |
454 | --userconf ) |
455 | export_config |
b9f21021 |
456 | # shellcheck disable=SC2317 |
d80ac20a |
457 | echo "config exported to \"$RCFILE\"" |
b9f21021 |
458 | # shellcheck disable=SC2317 |
d80ac20a |
459 | exit |
53f2ed57 |
460 | ;; |
9eb02251 |
461 | --backup ) |
462 | case "$2" in |
463 | '' ) |
464 | read -r -p "Backup Dir: " BDIR |
465 | ;; |
466 | * ) |
467 | BDIR=$2 |
468 | ;; |
469 | esac |
470 | shift 2 |
471 | backup_data $BDIR |
472 | exit |
473 | ;; |
53f2ed57 |
474 | -- ) |
026502da |
475 | shift |
476 | break |
53f2ed57 |
477 | ;; |
478 | * ) |
479 | break |
480 | ;; |
481 | esac |
a4aaf855 |
482 | done |