fb711183 |
1 | #! /bin/bash |
2 | |
3 | # bash-notes © 2023 by danix is licensed under CC BY-NC 4.0. |
4 | # To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/ |
5 | |
6 | # to debug the script run it like: |
7 | # DEBUG=true notes.sh ... |
8 | # and check /tmp/debug_bash-notes.log |
9 | if [[ $DEBUG == true ]]; then |
10 | exec 5> /tmp/debug_bash-notes.log |
11 | BASH_XTRACEFD="5" |
12 | PS4='$LINENO: ' |
13 | set -x |
14 | fi |
15 | |
16 | PID=$$ |
17 | BASENAME=$( basename "$0" ) |
18 | NOW=$(date +%s) |
19 | |
20 | VERSION="0.3" |
21 | DBVERSION=${VERSION}_${NOW} |
22 | |
23 | set_defaults() { |
24 | # Binaries to use |
25 | JQ=${JQ:-/usr/bin/jq} |
26 | EDITOR=${EDITOR:-/usr/bin/vim} |
27 | TERMINAL=${TERMINAL:-/usr/bin/alacritty} |
28 | # add options for your terminal. Remember to add the last option to execute |
29 | # your editor program, otherwise the script will fail. |
30 | # see example in the addnote function |
31 | TERM_OPTS="--class notes --title notes -e " |
32 | # Setting PAGER here overrides whatever is set in your default shell |
33 | # comment this option to use your default pager if set in your shell. |
34 | PAGER=${PAGER:-/usr/bin/more} |
35 | |
36 | # set this to true to have output in plain text |
37 | # or use the -p option on the command line before every other option |
38 | PLAIN=false |
39 | # base directory for program files |
40 | BASEDIR=${BASEDIR:-~/.local/share/bash-notes} |
41 | # notes database in json format |
42 | DB=${BASEDIR}/db.json |
43 | # directory containing the actual notes |
44 | NOTESDIR=${BASEDIR}/notes |
45 | |
46 | } # end set_defaults, do not change this line. |
47 | |
48 | set_defaults |
49 | |
50 | # Do not edit below this point |
51 | RCFILE=${RCFILE:-~/.config/bash-notes.rc} |
52 | TMPDB=/tmp/db.json |
53 | |
54 | if [ ! -x "$JQ" ]; then |
55 | echo "jq not found in your PATH" |
56 | echo "install jq to continue" |
57 | exit 1 |
58 | fi |
59 | |
60 | # IMPORT USER DEFINED OPTIONS IF ANY |
61 | if [[ -f $RCFILE ]]; then |
62 | # shellcheck disable=SC1090 |
63 | source "$RCFILE" |
64 | fi |
65 | |
66 | # We prevent the program from running more than one instance: |
67 | PIDFILE=/var/tmp/$(basename "$0" .sh).pid |
68 | |
69 | # Make sure the PID file is removed when we kill the process |
70 | trap 'rm -f $PIDFILE; exit 1' TERM INT |
71 | |
72 | if [[ -r $PIDFILE ]]; then |
73 | # PIDFILE exists, so I guess there's already an instance running |
74 | # let's kill it and run again |
75 | # shellcheck disable=SC2046,SC2086 |
76 | kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1 |
77 | # should already be deleted by trap, but just to be sure |
78 | rm "$PIDFILE" |
79 | fi |
80 | |
81 | # create PIDFILE |
82 | echo $PID > "$PIDFILE" |
83 | |
84 | # Export config to file |
85 | function export_config() { |
86 | if [ -r ${RCFILE} ]; then |
87 | echo "Backing up current '${RCFILE}'...." |
88 | mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) |
89 | fi |
90 | echo "Writing '${RCFILE}'...." |
91 | sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \ |
92 | | grep -v set_defaults \ |
93 | | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \ |
94 | > ${RCFILE} |
95 | if [ -r ${RCFILE} ]; then |
96 | echo "Taking no further action." |
97 | exit 0 |
98 | else |
99 | echo "Could not write '${RCFILE}'...!" |
100 | exit 1 |
101 | fi |
102 | } |
103 | |
104 | # we should expand on this function to add a sample note and explain a little bit |
105 | # how the program works. |
106 | function firstrun() { |
107 | [ -f $RCFILE ] && RC=$RCFILE || RC="none" |
108 | |
109 | clear |
110 | echo "${BASENAME} configuration: |
111 | |
112 | base directory: ${BASEDIR}/ |
113 | notes archive: ${NOTESDIR}/ |
114 | notes database: ${DB} |
115 | rc file: $RC |
116 | text editor: ${EDITOR} |
117 | terminal: ${TERMINAL} |
118 | jq executable: ${JQ} |
119 | " |
120 | |
121 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
122 | case $ANSWER in |
123 | y|Y ) |
124 | mkdir -p $NOTESDIR |
125 | cat << __EOL__ > ${DB} |
126 | { |
127 | "params": { |
128 | "version": "${VERSION}", |
129 | "dbversion": "${DBVERSION}" |
130 | }, |
131 | "notes": [] |
132 | } |
133 | __EOL__ |
134 | echo; echo "All done, you can now write your first note." |
135 | ;; |
136 | * ) |
137 | echo "No changes made. Exiting" |
138 | exit |
139 | ;; |
140 | esac |
141 | } |
142 | |
143 | # check for notes dir existance and create it in case it doesn't exists |
144 | if [[ ! -d $NOTESDIR ]]; then |
145 | # we don't have a directory. FIRST RUN? |
146 | firstrun |
147 | fi |