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