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