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 # Git binary only used if $USEGIT is true - See below
29 GIT
=${GIT:-/usr/bin/git}
30 # add options for your terminal. Remember to add the last option to execute
31 # your editor program, otherwise the script will fail.
32 # see example in the addnote function
33 TERM_OPTS
="--class notes --title notes -e "
34 # Setting PAGER here overrides whatever is set in your default shell
35 # comment this option to use your default pager if set in your shell.
36 PAGER
=${PAGER:-/usr/bin/more}
38 # set this to true to have output in plain text
39 # or use the -p option on the command line before every other option
41 # base directory for program files
42 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
43 # notes database in json format
45 # directory containing the actual notes
46 NOTESDIR
=${BASEDIR}/notes
50 # If you want to store your notes in a git repository set this to true
52 # Address of your remote repository
53 GITREMOTE
=${GITREMOTE:-""}
54 # How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour)
55 GITSYNCDELAY
=${GITSYNCDELAY:-3600}
56 # The name of this client. Defaults to the output of hostname
57 GITCLIENT
=${GITCLIENT:-""}
59 } # end set_defaults, do not change this line.
63 # Do not edit below this point
64 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
67 if [ ! -x "$JQ" ]; then
68 echo "jq not found in your PATH"
69 echo "install jq to continue"
73 # IMPORT USER DEFINED OPTIONS IF ANY
74 if [[ -f $RCFILE ]]; then
75 # shellcheck disable=SC1090
79 # We prevent the program from running more than one instance:
80 PIDFILE
=/var
/tmp
/$
(basename "$0" .sh
).pid
82 # Make sure the PID file is removed when we kill the process
83 trap 'rm -f $PIDFILE; exit 1' TERM INT
85 if [[ -r $PIDFILE ]]; then
86 # PIDFILE exists, so I guess there's already an instance running
87 # let's kill it and run again
88 # shellcheck disable=SC2046,SC2086
89 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
90 # should already be deleted by trap, but just to be sure
95 echo $PID > "$PIDFILE"
97 # Export config to file
99 if [ -r ${RCFILE} ]; then
100 echo "Backing up current '${RCFILE}'...."
101 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
103 echo "Writing '${RCFILE}'...."
104 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
105 |
grep -v set_defaults \
106 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
108 if [ -r ${RCFILE} ]; then
109 echo "Taking no further action."
112 echo "Could not write '${RCFILE}'...!"
117 # we should expand on this function to add a sample note and explain a little bit
118 # how the program works.
120 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
123 echo "${BASENAME} configuration:
125 base directory: ${BASEDIR}/
126 notes archive: ${NOTESDIR}/
127 notes database: ${DB}
129 text editor: ${EDITOR}
130 terminal: ${TERMINAL}
134 echo "Now I'll create the needed files and directories."
135 read -r -p "Do you wish to continue? (y/N) " ANSWER
139 cat << __EOL__ > ${DB}
142 "version": "${VERSION}",
143 "dbversion": "${DBVERSION}"
151 echo; echo "All done, you can now write your first note."
154 echo "No changes made. Exiting"
160 # check for notes dir existance and create it in case it doesn't exists
161 if [[ ! -d $NOTESDIR ]]; then
162 # we don't have a directory. FIRST RUN?