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