sync operations now check for last sync before running again. It's possible to set...
[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
4cbcb39e 20VERSION="0.4git"
fb711183 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}
4cbcb39e 28# Git binary only used if $USEGIT is true - See below
29GIT=${GIT:-/usr/bin/git}
fb711183 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
33TERM_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.
36PAGER=${PAGER:-/usr/bin/more}
37
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
40PLAIN=false
41# base directory for program files
42BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
43# notes database in json format
44DB=${BASEDIR}/db.json
45# directory containing the actual notes
46NOTESDIR=${BASEDIR}/notes
47
4cbcb39e 48### GIT SUPPORT
49
50# If you want to store your notes in a git repository set this to true
51USEGIT=true
52# Address of your remote repository
53GITREMOTE=${GITREMOTE:-""}
cf6d89bc 54# How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour)
55GITSYNCDELAY=${GITSYNCDELAY:-3600}
4cbcb39e 56
fb711183 57} # end set_defaults, do not change this line.
58
59set_defaults
60
61# Do not edit below this point
62RCFILE=${RCFILE:-~/.config/bash-notes.rc}
63TMPDB=/tmp/db.json
64
65if [ ! -x "$JQ" ]; then
66 echo "jq not found in your PATH"
67 echo "install jq to continue"
68 exit 1
69fi
70
71# IMPORT USER DEFINED OPTIONS IF ANY
72if [[ -f $RCFILE ]]; then
73 # shellcheck disable=SC1090
74 source "$RCFILE"
75fi
76
77# We prevent the program from running more than one instance:
78PIDFILE=/var/tmp/$(basename "$0" .sh).pid
79
80# Make sure the PID file is removed when we kill the process
81trap 'rm -f $PIDFILE; exit 1' TERM INT
82
83if [[ -r $PIDFILE ]]; then
84 # PIDFILE exists, so I guess there's already an instance running
85 # let's kill it and run again
86 # shellcheck disable=SC2046,SC2086
87 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
88 # should already be deleted by trap, but just to be sure
89 rm "$PIDFILE"
90fi
91
92# create PIDFILE
93echo $PID > "$PIDFILE"
94
95# Export config to file
b1e2a018 96export_config() {
fb711183 97 if [ -r ${RCFILE} ]; then
98 echo "Backing up current '${RCFILE}'...."
99 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
100 fi
101 echo "Writing '${RCFILE}'...."
102 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
103 | grep -v set_defaults \
104 | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
105 > ${RCFILE}
106 if [ -r ${RCFILE} ]; then
107 echo "Taking no further action."
108 exit 0
109 else
110 echo "Could not write '${RCFILE}'...!"
111 exit 1
112 fi
113}
114
115# we should expand on this function to add a sample note and explain a little bit
116# how the program works.
b1e2a018 117firstrun() {
fb711183 118 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
119
120 clear
121 echo "${BASENAME} configuration:
122
123base directory: ${BASEDIR}/
124notes archive: ${NOTESDIR}/
125notes database: ${DB}
126rc file: $RC
127text editor: ${EDITOR}
128terminal: ${TERMINAL}
129jq executable: ${JQ}
130"
131
9eb02251 132 echo "Now I'll create the needed files and directories."
fb711183 133 read -r -p "Do you wish to continue? (y/N) " ANSWER
134 case $ANSWER in
135 y|Y )
136 mkdir -p $NOTESDIR
137 cat << __EOL__ > ${DB}
138{
139 "params": {
140 "version": "${VERSION}",
141 "dbversion": "${DBVERSION}"
142 },
87a368fe 143 "git": {
144 "lastpull": ""
145 },
fb711183 146 "notes": []
147}
148__EOL__
149 echo; echo "All done, you can now write your first note."
150 ;;
151 * )
152 echo "No changes made. Exiting"
153 exit
154 ;;
155 esac
156}
157
158# check for notes dir existance and create it in case it doesn't exists
159if [[ ! -d $NOTESDIR ]]; then
160 # we don't have a directory. FIRST RUN?
161 firstrun
162fi