working on the backup and backup restore functionalities.
[bash-notes.git] / notes.sh
... / ...
CommitLineData
1#! /bin/bash
2
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
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
15
16PID=$$
17BASENAME=$( basename "$0" )
18NOW=$(date +%s)
19
20VERSION="0.3"
21DBVERSION=${VERSION}_${NOW}
22
23set_defaults() {
24# Binaries to use
25JQ=${JQ:-/usr/bin/jq}
26EDITOR=${EDITOR:-/usr/bin/vim}
27TERMINAL=${TERMINAL:-/usr/bin/alacritty}
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
31TERM_OPTS="--class notes --title notes -e "
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.
34PAGER=${PAGER:-/usr/bin/more}
35
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
38PLAIN=false
39# base directory for program files
40BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
41# notes database in json format
42DB=${BASEDIR}/db.json
43# directory containing the actual notes
44NOTESDIR=${BASEDIR}/notes
45
46} # end set_defaults, do not change this line.
47
48set_defaults
49
50# Do not edit below this point
51RCFILE=${RCFILE:-~/.config/bash-notes.rc}
52TMPDB=/tmp/db.json
53
54if [ ! -x "$JQ" ]; then
55 echo "jq not found in your PATH"
56 echo "install jq to continue"
57 exit 1
58fi
59
60# IMPORT USER DEFINED OPTIONS IF ANY
61if [[ -f $RCFILE ]]; then
62 # shellcheck disable=SC1090
63 source "$RCFILE"
64fi
65
66# We prevent the program from running more than one instance:
67PIDFILE=/var/tmp/$(basename "$0" .sh).pid
68
69# Make sure the PID file is removed when we kill the process
70trap 'rm -f $PIDFILE; exit 1' TERM INT
71
72if [[ -r $PIDFILE ]]; then
73 # PIDFILE exists, so I guess there's already an instance running
74 # let's kill it and run again
75 # shellcheck disable=SC2046,SC2086
76 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
77 # should already be deleted by trap, but just to be sure
78 rm "$PIDFILE"
79fi
80
81# create PIDFILE
82echo $PID > "$PIDFILE"
83
84# Export config to file
85function 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.
106function firstrun() {
107 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
108
109 clear
110 echo "${BASENAME} configuration:
111
112base directory: ${BASEDIR}/
113notes archive: ${NOTESDIR}/
114notes database: ${DB}
115rc file: $RC
116text editor: ${EDITOR}
117terminal: ${TERMINAL}
118jq executable: ${JQ}
119"
120
121 echo "Now I'll create the needed files and directories."
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
145if [[ ! -d $NOTESDIR ]]; then
146 # we don't have a directory. FIRST RUN?
147 firstrun
148fi
149# check if input is a number, returns false or the number itself
150function check_noteID() {
151 IN=$1
152 case $IN in
153 ''|*[!0-9]*)
154 return 1
155 ;;
156 *)
157 echo "$IN"
158 ;;
159 esac
160}
161
162function helptext() {
163 echo "Usage:"
164 echo " $0 [PARAMS] ..."
165 echo ""
166 cat << __NOWCONF__
167${BASENAME} configuration is:
168
169base directory: ${BASEDIR}/
170notes archive: ${NOTESDIR}/
171notes database: ${DB}
172rc file: $RCFILE
173debug file: /tmp/debug_bash-note.log
174
175text editor: ${EDITOR}
176terminal: ${TERMINAL}
177jq executable: ${JQ}
178PAGER: ${PAGER}
179__NOWCONF__
180
181 echo ""
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 " -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"
195 echo ""
196}
197
198function addnote() {
199 # remove eventually existing temp DB file
200 if [[ -f $TMPDB ]]; then
201 rm $TMPDB
202 fi
203
204 NOTETITLE="$1"
205 echo "adding new note - \"$NOTETITLE\""
206 # shellcheck disable=SC2086
207 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
208 # [ "" == $LASTID ] && LASTID=0
209 NOTEID=$(( LASTID + 1 ))
210 # shellcheck disable=SC2086
211 touch ${NOTESDIR}/${NOW}
212 # shellcheck disable=SC2016
213 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
214 # shellcheck disable=SC2086
215 mv $TMPDB $DB
216 # example for alacritty:
217 # alacritty --class notes --title notes -e /usr/bin/vim ...
218 # shellcheck disable=SC2086,SC2091
219 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
220}
221function 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
261function editnote() {
262 NOTE=$1
263 # shellcheck disable=SC2155
264 local OK=$(check_noteID "$NOTE")
265 if [ ! "$OK" ]; then
266 echo "invalid note \"$NOTE\""
267 echo "Use the note ID that you can fetch after listing your notes"
268 exit 1
269 fi
270
271 # shellcheck disable=SC2016,SC2086
272 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
273 # shellcheck disable=SC2016,SC2086
274 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
275 if [ "$TITLE" ]; then
276 echo "editing note $TITLE"
277 # shellcheck disable=SC2086,SC2091
278 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
279 else
280 echo "note not found"
281 exit 1
282 fi
283}
284function 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}
306function rmnote() {
307 # remove eventually existing temp DB file
308 if [[ -f $TMPDB ]]; then
309 rm $TMPDB
310 fi
311
312 NOTE=$1
313 if [ "all" == "$NOTE" ]; then
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 )
318 # shellcheck disable=SC2086
319 $JQ 'del(.notes[])' $DB > $TMPDB
320 # shellcheck disable=SC2086
321 mv $TMPDB $DB
322 # shellcheck disable=SC2086
323 rm $NOTESDIR/*
324 echo "Deleted all notes"
325 ;;
326 * )
327 echo "Aborting, no notes were deleted."
328 exit 1
329 ;;
330 esac
331 else
332 # shellcheck disable=SC2155
333 local OK=$(check_noteID "$NOTE")
334 if [ ! "$OK" ]; then
335 echo "invalid note \"$NOTE\""
336 echo "Use the note ID that you can fetch after listing your notes"
337 exit 1
338 fi
339
340 # shellcheck disable=SC2016,SC2086
341 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
342 # shellcheck disable=SC2016,SC2086
343 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
344 if [ "$TITLE" ]; then
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"
351 else
352 echo "note not found"
353 exit 1
354 fi
355 fi
356}
357function 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}
374# shellcheck disable=SC2006
375GOPT=$(getopt -o hvpla::e::d::s:: --long help,version,list,plain,userconf,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
376
377# shellcheck disable=SC2181
378if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
379
380# Note the quotes around `$GOPT': they are essential!
381eval set -- "$GOPT"
382unset GOPT
383
384while 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 ;;
394 -p | --plain )
395 PLAIN=true
396 shift
397 ;;
398 -l | --list )
399 listnotes
400 exit
401 ;;
402 -a | --add )
403 case "$2" in
404 '' )
405 read -r -p "Title: " TITLE
406 ;;
407 * )
408 TITLE=$2
409 ;;
410 esac
411 shift 2
412 addnote "$TITLE"
413 exit
414 ;;
415 -e | --edit )
416 case "$2" in
417 '' )
418 read -r -p "Note ID: " NOTE
419 ;;
420 * )
421 NOTE=$2
422 ;;
423 esac
424 shift 2
425 editnote "$NOTE"
426 exit
427 ;;
428 -d | --delete )
429 case "$2" in
430 '' )
431 read -r -p "Note ID: " NOTE
432 ;;
433 * )
434 NOTE=$2
435 ;;
436 esac
437 shift 2
438 rmnote "$NOTE"
439 exit
440 ;;
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"
452 exit
453 ;;
454 --userconf )
455 export_config
456 # shellcheck disable=SC2317
457 echo "config exported to \"$RCFILE\""
458 # shellcheck disable=SC2317
459 exit
460 ;;
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 ;;
474 -- )
475 shift
476 break
477 ;;
478 * )
479 break
480 ;;
481 esac
482done