04141e6d25c03e83ff77ff4ec0e490e1186d1b63
[bash-notes.git] / notes.sh
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
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
15
16 PID=$$
17 VERSION="0.2"
18
19 set_defaults() {
20 # Binaries to use
21 JQ=${JQ:-/usr/bin/jq}
22 EDITOR=${EDITOR:-/usr/bin/vim}
23 TERMINAL=${TERMINAL:-/usr/bin/alacritty}
24 # add options for your terminal. Remember to add the last option to execute
25 # your editor program, otherwise the script will fail.
26 # see example in the addnote function
27 TERM_OPTS="--class notes --title notes -e "
28
29 # set this to true to have output in plain text
30 # or use the -p option on the command line before every other option
31 PLAIN=false
32 # base directory for program files
33 BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
34 # notes database in json format
35 DB=${BASEDIR}/db.json
36 # directory containing the actual notes
37 NOTESDIR=${BASEDIR}/notes
38
39 } # end set_defaults, do not change this line.
40
41 set_defaults
42
43 # Do not edit below this point
44 RCFILE=${RCFILE:-~/.config/bash-notes.rc}
45 TMPDB=/tmp/db.json
46 BASENAME=$( basename "$0" )
47 NOW=$(date +%s)
48
49 if [ ! -x "$JQ" ]; then
50 echo "jq not found in your PATH"
51 echo "install jq to continue"
52 exit 1
53 fi
54
55 # IMPORT USER DEFINED OPTIONS IF ANY
56 if [[ -f $RCFILE ]]; then
57 # shellcheck disable=SC1090
58 source "$RCFILE"
59 fi
60
61 # We prevent the program from running more than one instance:
62 PIDFILE=/var/tmp/$(basename "$0" .sh).pid
63
64 # Make sure the PID file is removed when we kill the process
65 trap 'rm -f $PIDFILE; exit 1' TERM INT
66
67 if [[ -r $PIDFILE ]]; then
68 # PIDFILE exists, so I guess there's already an instance running
69 # let's kill it and run again
70 # shellcheck disable=SC2046,SC2086
71 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
72 # should already be deleted by trap, but just to be sure
73 rm "$PIDFILE"
74 fi
75
76 # create PIDFILE
77 echo $PID > "$PIDFILE"
78
79 # check if input is a number, returns false or the number itself
80 function check_noteID() {
81 IN=$1
82 case $IN in
83 ''|*[!0-9]*)
84 return 1
85 ;;
86 *)
87 echo "$IN"
88 ;;
89 esac
90 }
91
92 function helptext() {
93 echo "Usage:"
94 echo " $0 [PARAMS] ..."
95 echo ""
96 cat << __NOWCONF__
97 ${BASENAME} configuration is:
98
99 base directory: ${BASEDIR}/
100 notes archive: ${NOTESDIR}/
101 notes database: ${DB}
102 rc file: $RCFILE
103 debug file: /tmp/debug_bash-note.log
104
105 text editor: ${EDITOR}
106 terminal: ${TERMINAL}
107 jq executable: ${JQ}
108 __NOWCONF__
109
110 echo ""
111 echo "${BASENAME} parameters are:"
112 echo " -h | --help : This help text"
113 echo " -p | --plain : Output is in plain text"
114 echo " (without this option the output is formatted)"
115 echo " (this option must precede all others)"
116 echo " -l | --list : List existing notes"
117 echo " -a | --add [\"<title>\"] : Add new note"
118 echo " -e | --edit [<note>] : Edit note"
119 echo " -d | --delete [<note> | all] : Delete single note or all notes at once"
120 echo " -v | --version : Print version"
121 echo " --userconf : Export User config file"
122 echo ""
123 }
124
125 function addnote() {
126 # remove eventually existing temp DB file
127 if [[ -f $TMPDB ]]; then
128 rm $TMPDB
129 fi
130
131 NOTETITLE="$1"
132 echo "adding new note - \"$NOTETITLE\""
133 # shellcheck disable=SC2086
134 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
135 # [ "" == $LASTID ] && LASTID=0
136 NOTEID=$(( LASTID + 1 ))
137 # shellcheck disable=SC2086
138 touch ${NOTESDIR}/${NOW}
139 # shellcheck disable=SC2016
140 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
141 # shellcheck disable=SC2086
142 mv $TMPDB $DB
143 # example for alacritty:
144 # alacritty --class notes --title notes -e /usr/bin/vim ...
145 # shellcheck disable=SC2086,SC2091
146 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
147 }
148
149 function listnotes() {
150 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
151 if [[ $(ls -A "$NOTESDIR") ]]; then
152 if [ $PLAIN == false ]; then
153 echo "listing all notes"
154 echo ""
155 fi
156 [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
157 for i in "${NOTESDIR}"/*; do
158 # shellcheck disable=SC2155
159 local fname=$(basename $i)
160 DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
161 # shellcheck disable=SC2016,SC2086
162 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
163 # shellcheck disable=SC2016,SC2086
164 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
165 [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
166 done
167 else
168 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
169 fi
170 }
171
172 function editnote() {
173 NOTE=$1
174 # shellcheck disable=SC2155
175 local OK=$(check_noteID "$NOTE")
176 if [ ! "$OK" ]; then
177 echo "invalid note \"$NOTE\""
178 echo "Use the note ID that you can fetch after listing your notes"
179 exit 1
180 fi
181
182 # shellcheck disable=SC2016,SC2086
183 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
184 # shellcheck disable=SC2016,SC2086
185 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
186 if [ "$TITLE" ]; then
187 echo "editing note $TITLE"
188 # shellcheck disable=SC2086,SC2091
189 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
190 else
191 echo "note not found"
192 exit 1
193 fi
194 }
195
196 function rmnote() {
197 # remove eventually existing temp DB file
198 if [[ -f $TMPDB ]]; then
199 rm $TMPDB
200 fi
201
202 NOTE=$1
203 if [ "all" == "$NOTE" ]; then
204 echo "You're going to delete all notes."
205 read -r -p "Do you wish to continue? (y/N) " ANSWER
206 case $ANSWER in
207 y|Y )
208 # shellcheck disable=SC2086
209 $JQ 'del(.notes[])' $DB > $TMPDB
210 # shellcheck disable=SC2086
211 mv $TMPDB $DB
212 # shellcheck disable=SC2086
213 rm $NOTESDIR/*
214 echo "Deleted all notes"
215 ;;
216 * )
217 echo "Aborting, no notes were deleted."
218 exit 1
219 ;;
220 esac
221 else
222 # shellcheck disable=SC2155
223 local OK=$(check_noteID "$NOTE")
224 if [ ! "$OK" ]; then
225 echo "invalid note \"$NOTE\""
226 echo "Use the note ID that you can fetch after listing your notes"
227 exit 1
228 fi
229
230 # shellcheck disable=SC2016,SC2086
231 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
232 # shellcheck disable=SC2016,SC2086
233 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
234 if [ "$TITLE" ]; then
235 # shellcheck disable=SC2016,SC2086
236 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
237 # shellcheck disable=SC2086
238 mv $TMPDB $DB
239 rm $NOTESDIR/$FILE
240 echo "Deleted note $TITLE"
241 else
242 echo "note not found"
243 exit 1
244 fi
245 fi
246 }
247
248 function export_config() {
249 if [ -r ${RCFILE} ]; then
250 echo "Backing up current '${RCFILE}'...."
251 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
252 fi
253 echo "Writing '${RCFILE}'...."
254 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
255 | grep -v set_defaults \
256 | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
257 > ${RCFILE}
258 if [ -r ${RCFILE} ]; then
259 echo "Taking no further action."
260 exit 0
261 else
262 echo "Could not write '${RCFILE}'...!"
263 exit 1
264 fi
265 }
266
267 # we should expand on this function to add a sample note and explain a little bit
268 # how the program works.
269 function firstrun() {
270 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
271
272 clear
273 echo "${BASENAME} configuration:
274
275 base directory: ${BASEDIR}/
276 notes archive: ${NOTESDIR}/
277 notes database: ${DB}
278 rc file: $RC
279 text editor: ${EDITOR}
280 terminal: ${TERMINAL}
281 jq executable: ${JQ}
282 "
283
284 read -r -p "Do you wish to continue? (y/N) " ANSWER
285 case $ANSWER in
286 y|Y )
287 mkdir -p $NOTESDIR
288 cat << __EOL__ > ${DB}
289 {
290 "params": {
291 "version": "${VERSION}",
292 "dbversion": "${NOW}"
293 },
294 "notes": []
295 }
296 __EOL__
297 echo; echo "All done, you can now write your first note."
298 ;;
299 * )
300 echo "No changes made. Exiting"
301 exit
302 ;;
303 esac
304 }
305
306 # check for notes dir existance and create it in case it doesn't exists
307 if [[ ! -d $NOTESDIR ]]; then
308 # we don't have a directory. FIRST RUN?
309 firstrun
310 fi
311
312 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
313 # separately; see below.
314 # shellcheck disable=SC2006
315 GOPT=`getopt -o hvpla:e:d: --long help,version,list,plain,userconf,add:,edit:,delete:,editor:,storage: \
316 -n 'bash-notes' -- "$@"`
317
318 # shellcheck disable=SC2181
319 if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
320
321 # Note the quotes around `$GOPT': they are essential!
322 eval set -- "$GOPT"
323
324 while true; do
325 case "$1" in
326 -h | --help )
327 helptext
328 exit
329 ;;
330 -v | --version )
331 echo $BASENAME v${VERSION}
332 exit
333 ;;
334 -p | --plain )
335 PLAIN=true
336 shift
337 ;;
338 -l | --list )
339 listnotes
340 exit
341 ;;
342 -a | --add )
343 TITLE="$2"
344 shift 2
345 addnote "$TITLE"
346 ;;
347 -e | --edit )
348 NOTE="$2"
349 shift 2
350 editnote "$NOTE"
351 ;;
352 -d | --delete )
353 NOTE="$2"
354 shift 2
355 rmnote "$NOTE"
356 ;;
357 --userconf )
358 export_config
359 # shellcheck disable=SC2317
360 echo "config exported to \"$RCFILE\""
361 # shellcheck disable=SC2317
362 exit
363 ;;
364 -- )
365 shift
366 break
367 ;;
368 * )
369 break
370 ;;
371 esac
372 done
373