add function mostly working. Fresh install now sets up the directories correctly
[bash-notes.git] / notes.sh
CommitLineData
a4aaf855 1#! /bin/bash
2
3# set -ex
4
5PID=$$
6VERSION="0.1"
53f2ed57 7
a4aaf855 8EDITOR=${EDITOR:-/usr/bin/vim}
53f2ed57 9TERMINAL=${TERMINAL:-/usr/bin/alacritty}
10JQ=$(which jq)
11
12BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
13RCFILE=${RCFILE:-~/.bash-notes.rc}
a4aaf855 14DB=${BASEDIR}/db.json
a4aaf855 15NOTESDIR=${BASEDIR}/notes
53f2ed57 16
17TMPDB=/tmp/db.json
a4aaf855 18BASENAME=$( basename $0 )
a4aaf855 19
20if [ ! -x $JQ ]; then
21 echo "jq not found in your PATH"
22 echo "install jq to continue"
23 exit 1
24fi
25
53f2ed57 26# IMPORT USER DEFINED OPTIONS IF ANY
27if [[ -f $RCFILE ]]; then
28 source $RCFILE
29fi
30
a4aaf855 31# We prevent the program from running more than one instance:
32PIDFILE=/var/tmp/$(basename $0 .sh).pid
33
34# Make sure the PID file is removed when we kill the process
35trap 'rm -f $PIDFILE; exit 1' TERM INT
36
37if [[ -r $PIDFILE ]]; then
38 # PIDFILE exists, so I guess there's already an instance running
39 # let's kill it and run again
40 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
41 # should already be deleted by trap, but just to be sure
42 rm $PIDFILE
43fi
44
45# create PIDFILE
46echo $PID > $PIDFILE
47
48function helptext() {
49 echo "Parameters are:"
50 echo " -h : This help text"
51 echo " -s <directory> : specify directory where to store all notes."
52 echo " -e <editor> : specify EDITOR for this session only."
53 echo " -l : List existing notes"
54 echo " -a : Add new note"
55 echo " -m <note> : Modify note"
56 echo " -d <note> : Modify date for note"
57 echo " -r <note> : Remove note"
58 echo " -v : Print version"
59}
60
61function addnote() {
53f2ed57 62 NOTETITLE="$1"
63 echo "adding new note - \"$NOTETITLE\""
a4aaf855 64 NOW=$(date +%s)
53f2ed57 65 LASTID=$($JQ '.notes[-1].id | tonumber' $DB)
a4aaf855 66 [ null == $LASTID ] && LASTID=0
67 NOTEID=$(( $LASTID + 1 ))
68 touch ${NOTESDIR}/${NOW}
69 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
70 mv $TMPDB $DB
53f2ed57 71 $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW})
a4aaf855 72}
73
74function listnotes() {
75 echo "list all notes"
76}
77
78function editnote() {
79 echo "edit note \"${1}\""
80}
81
82function datenote() {
83 echo "edit date for note \"${1}\""
53f2ed57 84 # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
85
a4aaf855 86}
87
88function rmnote() {
53f2ed57 89 NOTE=$1
90 echo "removing note $NOTE"
a4aaf855 91}
92
53f2ed57 93# we should expand on this function to add a sample note and explain a little bit
94# how the program works.
a4aaf855 95function firstrun() {
53f2ed57 96 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
97
98 clear
99 echo "${BASENAME} configuration:
100
101base directory: ${BASEDIR}/
102notes archive: ${NOTESDIR}/
103notes database: ${DB}
104rc file: $RC
105text editor: ${EDITOR}
106terminal: ${TERMINAL}
107jq executable: ${JQ}
108"
109
110 read -r -p "Do you wish to continue? (y/N) " ANSWER
111 case $ANSWER in
112 y|Y )
113 mkdir -p $NOTESDIR
114 cat << "__EOL__" > ${DB}
a4aaf855 115{
116 "notes": []
117}
118__EOL__
53f2ed57 119 ;;
120 n|N )
121 echo "No changes made. Exiting"
122 exit
123 ;;
124 * )
125 echo "No changes made. Exiting"
126 exit
127 ;;
128 esac
a4aaf855 129}
130
131# check for notes dir existance and create it in case it doesn't exists
132if [[ ! -d $NOTESDIR ]]; then
133 # we don't have a directory. FIRST RUN?
134 firstrun
135fi
136
53f2ed57 137# NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
138# separately; see below.
139GOPT=`getopt -o hvla:m:d:r: --long help,version,list,add:,modify:,date:,remove:,editor:,storage: \
140 -n 'bash-notes' -- "$@"`
141
142if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
143
144# Note the quotes around `$GOPT': they are essential!
145eval set -- "$GOPT"
146
147while true; do
148 case "$1" in
149 -h | --help )
150 helptext
151 exit
152 ;;
153 -v | --version )
154 echo $BASENAME v${VERSION}
155 exit
156 ;;
157 -l | --list )
158 listnotes
159 exit
160 ;;
161 -a | --add )
162 TITLE="$2"
163 shift 2
164 addnote "$TITLE"
165 ;;
166 -m | --modify )
167 NOTE="$2"
168 shift 2
169 editnote "$NOTE"
170 ;;
171 -d | --date )
172 NOTE="$2"
173 shift 2
174 datenote "$NOTE"
175 ;;
176 -r | --remove )
177 NOTE="$2"
178 shift 2
179 rmnote "$NOTE"
180 ;;
181 --editor )
182 EDITOR="$2"
183 shift 2
184 echo "changed EDITOR TO \"$EDITOR\""
185 ;;
186 --storage )
187 BASEDIR="$2"
188 shift 2
189 echo "changed BASEDIR TO \"$BASEDIR\""
190 # firstrun
191 ;;
192 -- )
193 shift; break
194 ;;
195 * )
196 break
197 ;;
198 esac
a4aaf855 199done
200