working on the backup and backup restore functionalities.
[bash-notes.git] / SOURCE / head.sh
CommitLineData
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
9if [[ $DEBUG == true ]]; then
10 exec 5> /tmp/debug_bash-notes.log
11 BASH_XTRACEFD="5"
12 PS4='$LINENO: '
13 set -x
14fi
15
16PID=$$
17BASENAME=$( basename "$0" )
18NOW=$(date +%s)
19
20VERSION="0.3"
21DBVERSION=${VERSION}_${NOW}
22
23set_defaults() {
24# Binaries to use
25JQ=${JQ:-/usr/bin/jq}
26EDITOR=${EDITOR:-/usr/bin/vim}
27TERMINAL=${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
31TERM_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.
34PAGER=${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
38PLAIN=false
39# base directory for program files
40BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
41# notes database in json format
42DB=${BASEDIR}/db.json
43# directory containing the actual notes
44NOTESDIR=${BASEDIR}/notes
45
46} # end set_defaults, do not change this line.
47
48set_defaults
49
50# Do not edit below this point
51RCFILE=${RCFILE:-~/.config/bash-notes.rc}
52TMPDB=/tmp/db.json
53
54if [ ! -x "$JQ" ]; then
55 echo "jq not found in your PATH"
56 echo "install jq to continue"
57 exit 1
58fi
59
60# IMPORT USER DEFINED OPTIONS IF ANY
61if [[ -f $RCFILE ]]; then
62 # shellcheck disable=SC1090
63 source "$RCFILE"
64fi
65
66# We prevent the program from running more than one instance:
67PIDFILE=/var/tmp/$(basename "$0" .sh).pid
68
69# Make sure the PID file is removed when we kill the process
70trap 'rm -f $PIDFILE; exit 1' TERM INT
71
72if [[ -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"
79fi
80
81# create PIDFILE
82echo $PID > "$PIDFILE"
83
84# Export config to file
85function 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.
106function firstrun() {
107 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
108
109 clear
110 echo "${BASENAME} configuration:
111
112base directory: ${BASEDIR}/
113notes archive: ${NOTESDIR}/
114notes database: ${DB}
115rc file: $RC
116text editor: ${EDITOR}
117terminal: ${TERMINAL}
118jq executable: ${JQ}
119"
120
9eb02251 121 echo "Now I'll create the needed files and directories."
fb711183 122 read -r -p "Do you wish to continue? (y/N) " ANSWER
123 case $ANSWER in
124 y|Y )
125 mkdir -p $NOTESDIR
126 cat << __EOL__ > ${DB}
127{
128 "params": {
129 "version": "${VERSION}",
130 "dbversion": "${DBVERSION}"
131 },
132 "notes": []
133}
134__EOL__
135 echo; echo "All done, you can now write your first note."
136 ;;
137 * )
138 echo "No changes made. Exiting"
139 exit
140 ;;
141 esac
142}
143
144# check for notes dir existance and create it in case it doesn't exists
145if [[ ! -d $NOTESDIR ]]; then
146 # we don't have a directory. FIRST RUN?
147 firstrun
148fi