Separated script into multiple files for easier management. Added Makefile
[bash-notes.git] / notes.sh
index d3e6a53..5d7ae7e 100755 (executable)
--- a/notes.sh
+++ b/notes.sh
@@ -81,6 +81,70 @@ 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
 # check if input is a number, returns false or the number itself
 function check_noteID() {
        IN=$1
@@ -127,7 +191,6 @@ __NOWCONF__
     echo "  --userconf                 : Export User config file"
     echo ""
 }
-
 function addnote() {
        # remove eventually existing temp DB file
        if [[ -f $TMPDB ]]; then
@@ -151,30 +214,6 @@ function addnote() {
        # shellcheck disable=SC2086,SC2091
        $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
 }
-
-function listnotes() {
-       # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
-       if [[ $(ls -A "$NOTESDIR") ]]; then
-               if [ $PLAIN == false ]; then
-                       echo "listing all notes"
-                       echo ""
-               fi
-               [ $PLAIN == false ] && echo "[ID]       [TITLE]         [CREATED]"
-               for i in "${NOTESDIR}"/*; do
-                       # shellcheck disable=SC2155
-                       local fname=$(basename $i)
-                       DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
-                       # shellcheck disable=SC2016,SC2086
-                       TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
-                       # shellcheck disable=SC2016,SC2086
-                       ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
-                       [ $PLAIN == false ] && echo "[${ID}]    ${TITLE}        ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
-               done
-       else
-               echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
-       fi
-}
-
 function editnote() {
        NOTE=$1
        # shellcheck disable=SC2155
@@ -198,7 +237,28 @@ function editnote() {
                 exit 1
        fi
 }
-
+function listnotes() {
+       # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
+       if [[ $(ls -A "$NOTESDIR") ]]; then
+               if [ $PLAIN == false ]; then
+                       echo "listing all notes"
+                       echo ""
+               fi
+               [ $PLAIN == false ] && echo "[ID]       [TITLE]         [CREATED]"
+               for i in "${NOTESDIR}"/*; do
+                       # shellcheck disable=SC2155
+                       local fname=$(basename $i)
+                       DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
+                       # shellcheck disable=SC2016,SC2086
+                       TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
+                       # shellcheck disable=SC2016,SC2086
+                       ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
+                       [ $PLAIN == false ] && echo "[${ID}]    ${TITLE}        ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
+               done
+       else
+               echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
+       fi
+}
 function rmnote() {
        # remove eventually existing temp DB file
        if [[ -f $TMPDB ]]; then
@@ -250,7 +310,6 @@ function rmnote() {
                fi
        fi
 }
-
 function shownote() {
        NOTE=$1
 
@@ -268,71 +327,6 @@ function shownote() {
                $PAGER ${NOTESDIR}/${FILE}
        fi
 }
-
-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
-
 # shellcheck disable=SC2006
 GOPT=$(getopt -o hvpla::e::d::s:: --long help,version,list,plain,userconf,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")