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} |
d80ac20a |
12 | JQ=${JQ:-/usr/bin/jq} |
53f2ed57 |
13 | |
d80ac20a |
14 | # base directory for program files |
53f2ed57 |
15 | BASEDIR=${BASEDIR:-~/.local/share/bash-notes} |
d80ac20a |
16 | # notes database in json format |
a4aaf855 |
17 | DB=${BASEDIR}/db.json |
d80ac20a |
18 | # directory containing the actual notes |
a4aaf855 |
19 | NOTESDIR=${BASEDIR}/notes |
53f2ed57 |
20 | |
d80ac20a |
21 | } # end set_defaults, do not change this line. |
22 | |
23 | set_defaults |
24 | |
25 | # Do not edit below this point |
26 | RCFILE=${RCFILE:-~/.config/bash-notes.rc} |
53f2ed57 |
27 | TMPDB=/tmp/db.json |
a4aaf855 |
28 | BASENAME=$( basename $0 ) |
d80ac20a |
29 | NOW=$(date +%s) |
a4aaf855 |
30 | |
31 | if [ ! -x $JQ ]; then |
32 | echo "jq not found in your PATH" |
33 | echo "install jq to continue" |
34 | exit 1 |
35 | fi |
36 | |
53f2ed57 |
37 | # IMPORT USER DEFINED OPTIONS IF ANY |
38 | if [[ -f $RCFILE ]]; then |
39 | source $RCFILE |
40 | fi |
41 | |
a4aaf855 |
42 | # We prevent the program from running more than one instance: |
43 | PIDFILE=/var/tmp/$(basename $0 .sh).pid |
44 | |
45 | # Make sure the PID file is removed when we kill the process |
46 | trap 'rm -f $PIDFILE; exit 1' TERM INT |
47 | |
48 | if [[ -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 |
54 | fi |
55 | |
56 | # create PIDFILE |
57 | echo $PID > $PIDFILE |
58 | |
59 | function helptext() { |
d80ac20a |
60 | echo "Usage:" |
61 | echo " $0 [OPTION] ..." |
62 | echo "" |
63 | cat << __NOWCONF__ |
64 | ${BASENAME} configuration is: |
65 | |
66 | base directory: ${BASEDIR}/ |
67 | notes archive: ${NOTESDIR}/ |
68 | notes database: ${DB} |
69 | rc file: $RCFILE |
70 | text editor: ${EDITOR} |
71 | terminal: ${TERMINAL} |
72 | jq 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 | |
87 | function 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 | |
99 | function listnotes() { |
100 | echo "list all notes" |
101 | } |
102 | |
103 | function editnote() { |
104 | echo "edit note \"${1}\"" |
105 | } |
106 | |
107 | function datenote() { |
108 | echo "edit date for note \"${1}\"" |
53f2ed57 |
109 | # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T) |
110 | |
a4aaf855 |
111 | } |
112 | |
113 | function rmnote() { |
53f2ed57 |
114 | NOTE=$1 |
115 | echo "removing note $NOTE" |
a4aaf855 |
116 | } |
117 | |
d80ac20a |
118 | function export_config() { |
119 | if [ -r ${RCFILE} ]; then |
120 | echo "Backing up current '${RCFILE}'...." |
121 | mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) |
122 | fi |
123 | echo "Writing '${RCFILE}'...." |
124 | sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \ |
125 | | grep -v set_defaults \ |
126 | | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \ |
127 | > ${RCFILE} |
128 | if [ -r ${RCFILE} ]; then |
129 | echo "Taking no further action." |
130 | exit 0 |
131 | else |
132 | echo "Could not write '${RCFILE}'...!" |
133 | exit 1 |
134 | fi |
135 | } |
136 | |
53f2ed57 |
137 | # we should expand on this function to add a sample note and explain a little bit |
138 | # how the program works. |
a4aaf855 |
139 | function firstrun() { |
53f2ed57 |
140 | [ -f $RCFILE ] && RC=$RCFILE || RC="none" |
141 | |
142 | clear |
143 | echo "${BASENAME} configuration: |
144 | |
145 | base directory: ${BASEDIR}/ |
146 | notes archive: ${NOTESDIR}/ |
147 | notes database: ${DB} |
148 | rc file: $RC |
149 | text editor: ${EDITOR} |
150 | terminal: ${TERMINAL} |
151 | jq executable: ${JQ} |
152 | " |
153 | |
154 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
155 | case $ANSWER in |
156 | y|Y ) |
157 | mkdir -p $NOTESDIR |
d80ac20a |
158 | cat << __EOL__ > ${DB} |
a4aaf855 |
159 | { |
d80ac20a |
160 | "params": { |
161 | "version": "${VERSION}", |
162 | "dbversion": "${NOW}" |
163 | }, |
a4aaf855 |
164 | "notes": [] |
165 | } |
166 | __EOL__ |
d80ac20a |
167 | echo; echo "All done, you can now write your first note." |
53f2ed57 |
168 | ;; |
169 | * ) |
170 | echo "No changes made. Exiting" |
171 | exit |
172 | ;; |
173 | esac |
a4aaf855 |
174 | } |
175 | |
176 | # check for notes dir existance and create it in case it doesn't exists |
177 | if [[ ! -d $NOTESDIR ]]; then |
178 | # we don't have a directory. FIRST RUN? |
179 | firstrun |
180 | fi |
181 | |
53f2ed57 |
182 | # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this |
183 | # separately; see below. |
d80ac20a |
184 | GOPT=`getopt -o hvla:m:d:r: --long help,version,list,userconf,add:,modify:,date:,remove:,editor:,storage: \ |
53f2ed57 |
185 | -n 'bash-notes' -- "$@"` |
186 | |
187 | if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi |
188 | |
189 | # Note the quotes around `$GOPT': they are essential! |
190 | eval set -- "$GOPT" |
191 | |
192 | while true; do |
193 | case "$1" in |
194 | -h | --help ) |
195 | helptext |
196 | exit |
197 | ;; |
198 | -v | --version ) |
199 | echo $BASENAME v${VERSION} |
200 | exit |
201 | ;; |
202 | -l | --list ) |
203 | listnotes |
204 | exit |
205 | ;; |
206 | -a | --add ) |
207 | TITLE="$2" |
208 | shift 2 |
209 | addnote "$TITLE" |
210 | ;; |
211 | -m | --modify ) |
212 | NOTE="$2" |
213 | shift 2 |
214 | editnote "$NOTE" |
215 | ;; |
216 | -d | --date ) |
217 | NOTE="$2" |
218 | shift 2 |
219 | datenote "$NOTE" |
220 | ;; |
221 | -r | --remove ) |
222 | NOTE="$2" |
223 | shift 2 |
224 | rmnote "$NOTE" |
225 | ;; |
d80ac20a |
226 | --userconf ) |
227 | export_config |
228 | echo "config exported to \"$RCFILE\"" |
229 | exit |
53f2ed57 |
230 | ;; |
231 | -- ) |
232 | shift; break |
233 | ;; |
234 | * ) |
235 | break |
236 | ;; |
237 | esac |
a4aaf855 |
238 | done |
239 | |