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