diff options
| author | danix <danix@danix.xyz> | 2023-03-29 11:19:55 +0200 |
|---|---|---|
| committer | danix <danix@danix.xyz> | 2023-03-29 11:19:55 +0200 |
| commit | fb71118327216a21f6732161dc3721496a16370d (patch) | |
| tree | 96eddf131b1d802be87addd7faece45141e98a9a /SOURCE/head.sh | |
| parent | ad818a9d1e26462bfb0c36d585dcbc33b38b37ca (diff) | |
| download | bash-notes-fb71118327216a21f6732161dc3721496a16370d.tar.gz bash-notes-fb71118327216a21f6732161dc3721496a16370d.zip | |
Separated script into multiple files for easier management. Added Makefile
Diffstat (limited to 'SOURCE/head.sh')
| -rw-r--r-- | SOURCE/head.sh | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/SOURCE/head.sh b/SOURCE/head.sh new file mode 100644 index 0000000..d68d171 --- /dev/null +++ b/SOURCE/head.sh @@ -0,0 +1,147 @@ +#! /bin/bash + +# bash-notes © 2023 by danix is licensed under CC BY-NC 4.0. +# To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/ + +# to debug the script run it like: +# DEBUG=true notes.sh ... +# and check /tmp/debug_bash-notes.log +if [[ $DEBUG == true ]]; then + exec 5> /tmp/debug_bash-notes.log + BASH_XTRACEFD="5" + PS4='$LINENO: ' + set -x +fi + +PID=$$ +BASENAME=$( basename "$0" ) +NOW=$(date +%s) + +VERSION="0.3" +DBVERSION=${VERSION}_${NOW} + +set_defaults() { +# Binaries to use +JQ=${JQ:-/usr/bin/jq} +EDITOR=${EDITOR:-/usr/bin/vim} +TERMINAL=${TERMINAL:-/usr/bin/alacritty} +# add options for your terminal. Remember to add the last option to execute +# your editor program, otherwise the script will fail. +# see example in the addnote function +TERM_OPTS="--class notes --title notes -e " +# Setting PAGER here overrides whatever is set in your default shell +# comment this option to use your default pager if set in your shell. +PAGER=${PAGER:-/usr/bin/more} + +# set this to true to have output in plain text +# or use the -p option on the command line before every other option +PLAIN=false +# base directory for program files +BASEDIR=${BASEDIR:-~/.local/share/bash-notes} +# notes database in json format +DB=${BASEDIR}/db.json +# directory containing the actual notes +NOTESDIR=${BASEDIR}/notes + +} # end set_defaults, do not change this line. + +set_defaults + +# Do not edit below this point +RCFILE=${RCFILE:-~/.config/bash-notes.rc} +TMPDB=/tmp/db.json + +if [ ! -x "$JQ" ]; then + echo "jq not found in your PATH" + echo "install jq to continue" + exit 1 +fi + +# IMPORT USER DEFINED OPTIONS IF ANY +if [[ -f $RCFILE ]]; then + # shellcheck disable=SC1090 + source "$RCFILE" +fi + +# We prevent the program from running more than one instance: +PIDFILE=/var/tmp/$(basename "$0" .sh).pid + +# Make sure the PID file is removed when we kill the process +trap 'rm -f $PIDFILE; exit 1' TERM INT + +if [[ -r $PIDFILE ]]; then + # PIDFILE exists, so I guess there's already an instance running + # let's kill it and run again + # shellcheck disable=SC2046,SC2086 + kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1 + # should already be deleted by trap, but just to be sure + rm "$PIDFILE" +fi + +# create PIDFILE +echo $PID > "$PIDFILE" + +# Export config to file +function export_config() { + if [ -r ${RCFILE} ]; then + echo "Backing up current '${RCFILE}'...." + mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) + fi + echo "Writing '${RCFILE}'...." + sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \ + | grep -v set_defaults \ + | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \ + > ${RCFILE} + if [ -r ${RCFILE} ]; then + echo "Taking no further action." + exit 0 + else + echo "Could not write '${RCFILE}'...!" + exit 1 + fi +} + +# we should expand on this function to add a sample note and explain a little bit +# how the program works. +function firstrun() { + [ -f $RCFILE ] && RC=$RCFILE || RC="none" + + clear + echo "${BASENAME} configuration: + +base directory: ${BASEDIR}/ +notes archive: ${NOTESDIR}/ +notes database: ${DB} +rc file: $RC +text editor: ${EDITOR} +terminal: ${TERMINAL} +jq executable: ${JQ} +" + + read -r -p "Do you wish to continue? (y/N) " ANSWER + case $ANSWER in + y|Y ) + mkdir -p $NOTESDIR + cat << __EOL__ > ${DB} +{ + "params": { + "version": "${VERSION}", + "dbversion": "${DBVERSION}" + }, + "notes": [] +} +__EOL__ + echo; echo "All done, you can now write your first note." + ;; + * ) + echo "No changes made. Exiting" + exit + ;; + esac +} + +# check for notes dir existance and create it in case it doesn't exists +if [[ ! -d $NOTESDIR ]]; then + # we don't have a directory. FIRST RUN? + firstrun +fi |
