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