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/
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
17 BASENAME
=$
( basename "$0" )
21 DBVERSION
=${VERSION}_
${NOW}
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}
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
39 # base directory for program files
40 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
41 # notes database in json format
43 # directory containing the actual notes
44 NOTESDIR
=${BASEDIR}/notes
46 } # end set_defaults, do not change this line.
50 # Do not edit below this point
51 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
54 if [ ! -x "$JQ" ]; then
55 echo "jq not found in your PATH"
56 echo "install jq to continue"
60 # IMPORT USER DEFINED OPTIONS IF ANY
61 if [[ -f $RCFILE ]]; then
62 # shellcheck disable=SC1090
66 # We prevent the program from running more than one instance:
67 PIDFILE
=/var
/tmp
/$
(basename "$0" .sh
).pid
69 # Make sure the PID file is removed when we kill the process
70 trap 'rm -f $PIDFILE; exit 1' TERM INT
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
82 echo $PID > "$PIDFILE"
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
)
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/' \
95 if [ -r ${RCFILE} ]; then
96 echo "Taking no further action."
99 echo "Could not write '${RCFILE}'...!"
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"
110 echo "${BASENAME} configuration:
112 base directory: ${BASEDIR}/
113 notes archive: ${NOTESDIR}/
114 notes database: ${DB}
116 text editor: ${EDITOR}
117 terminal: ${TERMINAL}
121 echo "Now I'll create the needed files and directories."
122 read -r -p "Do you wish to continue? (y/N) " ANSWER
126 cat << __EOL__ > ${DB}
129 "version": "${VERSION}",
130 "dbversion": "${DBVERSION}"
135 echo; echo "All done, you can now write your first note."
138 echo "No changes made. Exiting"
144 # check for notes dir existance and create it in case it doesn't exists
145 if [[ ! -d $NOTESDIR ]]; then
146 # we don't have a directory. FIRST RUN?