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