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