we can now edit a note and list all notes
[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
59function helptext() {
d80ac20a 60 echo "Usage:"
61 echo " $0 [OPTION] ..."
62 echo ""
63 cat << __NOWCONF__
64${BASENAME} configuration is:
65
66base directory: ${BASEDIR}/
67notes archive: ${NOTESDIR}/
68notes database: ${DB}
69rc file: $RCFILE
70text editor: ${EDITOR}
71terminal: ${TERMINAL}
72jq executable: ${JQ}
73__NOWCONF__
74
75 echo ""
76 echo "The script's parameters are:"
77 echo " -h | --help : This help text"
78 echo " -l | --list : List existing notes"
79 echo " -a | --add <title> : Add new note"
80 echo " -m | --modify <note> : Modify note"
81 echo " -d | -date <note> : Modify date for note"
82 echo " -r | --remove <note> : Remove note"
83 echo " -v | --version : Print version"
84 echo " --userconf : Export User config file"
a4aaf855 85}
86
87function addnote() {
53f2ed57 88 NOTETITLE="$1"
89 echo "adding new note - \"$NOTETITLE\""
53f2ed57 90 LASTID=$($JQ '.notes[-1].id | tonumber' $DB)
a4aaf855 91 [ null == $LASTID ] && LASTID=0
92 NOTEID=$(( $LASTID + 1 ))
93 touch ${NOTESDIR}/${NOW}
94 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
95 mv $TMPDB $DB
53f2ed57 96 $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW})
a4aaf855 97}
98
99function listnotes() {
44abbfe7 100 echo "listing all notes"
101 echo ""
102 echo "[ID] [TITLE]"
103 for i in ${NOTESDIR}/*; do
104 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
105 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
106
107 echo "[${ID}] ${TITLE}"
108 done
a4aaf855 109}
110
111function editnote() {
44abbfe7 112 TITLE=$($JQ --arg i $1 '.notes[] | select(.id == $i) | .title' $DB)
113 FILE=$($JQ -r --arg i $1 '.notes[] | select(.id == $i) | .file' $DB)
114 if [ "$TITLE" ]; then
115 echo "editing note $TITLE"
116 $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${FILE})
117 else
118 echo "note not found"
119 exit 1
120 fi
a4aaf855 121}
122
123function datenote() {
124 echo "edit date for note \"${1}\""
53f2ed57 125 # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
126
a4aaf855 127}
128
129function rmnote() {
53f2ed57 130 NOTE=$1
131 echo "removing note $NOTE"
a4aaf855 132}
133
d80ac20a 134function export_config() {
135 if [ -r ${RCFILE} ]; then
136 echo "Backing up current '${RCFILE}'...."
137 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
138 fi
139 echo "Writing '${RCFILE}'...."
140 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
141 | grep -v set_defaults \
142 | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
143 > ${RCFILE}
144 if [ -r ${RCFILE} ]; then
145 echo "Taking no further action."
146 exit 0
147 else
148 echo "Could not write '${RCFILE}'...!"
149 exit 1
150 fi
151}
152
53f2ed57 153# we should expand on this function to add a sample note and explain a little bit
154# how the program works.
a4aaf855 155function firstrun() {
53f2ed57 156 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
157
158 clear
159 echo "${BASENAME} configuration:
160
161base directory: ${BASEDIR}/
162notes archive: ${NOTESDIR}/
163notes database: ${DB}
164rc file: $RC
165text editor: ${EDITOR}
166terminal: ${TERMINAL}
167jq executable: ${JQ}
168"
169
170 read -r -p "Do you wish to continue? (y/N) " ANSWER
171 case $ANSWER in
172 y|Y )
173 mkdir -p $NOTESDIR
d80ac20a 174 cat << __EOL__ > ${DB}
a4aaf855 175{
d80ac20a 176 "params": {
177 "version": "${VERSION}",
178 "dbversion": "${NOW}"
179 },
a4aaf855 180 "notes": []
181}
182__EOL__
d80ac20a 183 echo; echo "All done, you can now write your first note."
53f2ed57 184 ;;
185 * )
186 echo "No changes made. Exiting"
187 exit
188 ;;
189 esac
a4aaf855 190}
191
192# check for notes dir existance and create it in case it doesn't exists
193if [[ ! -d $NOTESDIR ]]; then
194 # we don't have a directory. FIRST RUN?
195 firstrun
196fi
197
53f2ed57 198# NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
199# separately; see below.
d80ac20a 200GOPT=`getopt -o hvla:m:d:r: --long help,version,list,userconf,add:,modify:,date:,remove:,editor:,storage: \
53f2ed57 201 -n 'bash-notes' -- "$@"`
202
203if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
204
205# Note the quotes around `$GOPT': they are essential!
206eval set -- "$GOPT"
207
208while true; do
209 case "$1" in
210 -h | --help )
211 helptext
212 exit
213 ;;
214 -v | --version )
215 echo $BASENAME v${VERSION}
216 exit
217 ;;
218 -l | --list )
219 listnotes
220 exit
221 ;;
222 -a | --add )
223 TITLE="$2"
224 shift 2
225 addnote "$TITLE"
226 ;;
227 -m | --modify )
228 NOTE="$2"
229 shift 2
230 editnote "$NOTE"
231 ;;
232 -d | --date )
233 NOTE="$2"
234 shift 2
235 datenote "$NOTE"
236 ;;
237 -r | --remove )
238 NOTE="$2"
239 shift 2
240 rmnote "$NOTE"
241 ;;
d80ac20a 242 --userconf )
243 export_config
244 echo "config exported to \"$RCFILE\""
245 exit
53f2ed57 246 ;;
247 -- )
248 shift; break
249 ;;
250 * )
251 break
252 ;;
253 esac
a4aaf855 254done
255