added showconf option. Fixed bug that prevented the rc file from being correctly...
[bash-notes.git] / notes.sh
index cbb34eb..adc3de7 100755 (executable)
--- a/notes.sh
+++ b/notes.sh
@@ -51,6 +51,10 @@ NOTESDIR=${BASEDIR}/notes
 USEGIT=true
 # Address of your remote repository
 GITREMOTE=${GITREMOTE:-""}
+# How long should we wait (in seconds) between sync on the git remote. Default 3600 (1 hour)
+GITSYNCDELAY=${GITSYNCDELAY:-"3600"}
+# The name of this client. Defaults to the output of hostname
+GITCLIENT=${GITCLIENT:-""}
 
 } # end set_defaults, do not change this line.
 
@@ -91,7 +95,7 @@ fi
 echo $PID > "$PIDFILE"
 
 # Export config to file
-function export_config() {
+export_config() {
        if [ -r ${RCFILE} ]; then
                echo "Backing up current '${RCFILE}'...."
                mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
@@ -112,7 +116,7 @@ function export_config() {
 
 # we should expand on this function to add a sample note and explain a little bit
 # how the program works.
-function firstrun() {
+firstrun() {
        [ -f $RCFILE ] && RC=$RCFILE || RC="none"
 
        clear
@@ -138,6 +142,9 @@ jq executable:              ${JQ}
                "version": "${VERSION}",
                "dbversion": "${DBVERSION}"
        },
+       "git": {
+               "lastpull": ""
+       },
        "notes": []
 }
 __EOL__
@@ -156,7 +163,7 @@ if [[ ! -d $NOTESDIR ]]; then
        firstrun
 fi
 # check if input is a number, returns false or the number itself
-function check_noteID() {
+check_noteID() {
        IN=$1
        case $IN in
                ''|*[!0-9]*)
@@ -168,7 +175,7 @@ function check_noteID() {
        esac
 }
 
-function helptext() {
+helptext() {
     echo "Usage:"
     echo "  $0 [PARAMS] [note ID]..."
        echo ""
@@ -190,26 +197,29 @@ function helptext() {
     echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
 }
 
-function configtext() {
-    cat << __NOWCONF__ 
-${BASENAME} configuration is:
-
-base directory:     ${BASEDIR}/
-notes archive:      ${NOTESDIR}/
-notes database:     ${DB}
-rc file:        $RCFILE
-debug file:     /tmp/debug_bash-note.log
-
-text editor:        ${EDITOR}
-terminal:       ${TERMINAL}
-jq executable:      ${JQ}
-PAGER:                  ${PAGER}
-__NOWCONF__
-
+configtext() {
+    [ $USEGIT ] && GITUSE="enabled" || GITUSE="disabled"
+    clear
+    echo -e "${BASENAME} configuration is:"
+
+    echo -e "base directory:     ${BASEDIR}/"
+    echo -e "notes archive:      ${NOTESDIR}/"
+    echo -e "notes database:     ${DB}"
+    echo -e "rc file:            $RCFILE"
+    echo -e "debug file:         /tmp/debug_bash-note.log"
+
+    echo -e "text editor:        ${EDITOR}"
+    echo -e "terminal:           ${TERMINAL}"
+    echo -e "jq executable:      ${JQ}"
+    echo -e "PAGER:              ${PAGER}"
+
+    echo -e "GIT use:            ${GITUSE}"
+    echo -e "GIT remote:         ${GITREMOTE}"
+    echo -e "GIT sync delay:     ${GITSYNCDELAY}"
 }
 
 # this function returns a random 2 words title
-function random_title() {
+random_title() {
     # Constants 
     X=0
     DICT=/usr/share/dict/words
@@ -230,6 +240,10 @@ function random_title() {
     echo $OUTPUT
 }
 
+# check if GITCLIENT has been set or set it to the output of hostname
+if [ -z "$GITCLIENT" ]; then
+    GITCLIENT=$( hostname )
+fi
 # returns true if the argument provided directory is a git repository
 is_git_repo() {
     DIR=$1
@@ -244,10 +258,50 @@ is_git_repo() {
 }
 
 # sync local repository to remote
+# accepts -f parameter to skip last sync check
 gitsync() {
-    echo "Syncing notes with git on remote \"$GITREMOTE\""
-    cd $BASEDIR
-    $GIT pull
+    FORCE=$1
+    if [[ $USEGIT && -n $GITREMOTE ]]; then
+        [ $PLAIN == false ] && echo "Syncing notes with git on remote \"$GITREMOTE\""
+        NOWSYNC=$(date +%s)
+        if [[ $FORCE == "-f" ]]; then
+            $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
+            mv $TMPDB $DB
+            cd $BASEDIR
+            $GIT pull
+        else
+            # LASTSYNC is the last time we synced to the remote, or 0 if it's the first time.
+            LASTSYNC=$($JQ -r '.git["lastpull"] // 0' "$DB")
+            SYNCDIFF=$(( ${NOWSYNC} - ${LASTSYNC} ))
+            if (( $SYNCDIFF > $GITSYNCDELAY )); then
+                #more than our delay time has passed. We can sync again.
+                $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
+                mv $TMPDB $DB
+                cd $BASEDIR
+                $GIT pull
+            else
+                # Last synced less than $GITSYNCDELAY seconds ago. We shall wait
+                [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait"
+            fi
+        fi
+    else
+        # no git, so we just keep going
+        true
+    fi
+}
+
+# add note to git and push it to remote
+gitadd() {
+    if [[ $USEGIT && -n $GITREMOTE ]]; then
+        [ $PLAIN == false ] && echo "Adding note to remote \"$GITREMOTE\""
+        cd $BASEDIR
+        $GIT add .
+        $GIT commit -m "$(basename $0) - adding note from ${GITCLIENT}"
+        $GIT push origin master
+    else
+        # no git, so we just keep going
+        true
+    fi
 }
 
 # check for USEGIT and subsequent variables
@@ -259,7 +313,7 @@ if [[ $USEGIT && -n $GITREMOTE ]]; then
         $GIT init
         echo "adding all files to git"
         $GIT add .
-        $GIT commit -m "$(basename $0) - initial commit"
+        $GIT commit -m "$(basename $0) - initial commit from ${GITCLIENT}"
         $GIT remote add origin $GITREMOTE
         $GIT push -u origin master
     fi
@@ -268,7 +322,9 @@ elif [[ $USEGIT && -z $GITREMOTE ]]; then
     USEGIT=false
 fi
 
-function addnote() {
+addnote() {
+       # attempt syncing before adding a note
+       gitsync -f
        # remove eventually existing temp DB file
        if [[ -f $TMPDB ]]; then
                rm $TMPDB
@@ -291,8 +347,10 @@ function addnote() {
        # alacritty --class notes --title notes -e /usr/bin/vim ...
        # shellcheck disable=SC2086,SC2091
        $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
+       # add note to git and push to remote
+       gitadd
 }
-function backup_data() {
+backup_data() {
        BACKUPDIR="$1"
     echo "backing up data in $BACKUPDIR"
 
@@ -320,9 +378,9 @@ function backup_data() {
     fi
     # ok, we have a backup directory
     if [ -r $RCFILE ]; then
-       BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/* ${BACKUPDIR})
+       BCKUP_COMM=$(rsync -avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
     else
-       BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/* ${BACKUPDIR})
+       BCKUP_COMM=$(rsync -avz --progress ${BASEDIR}/ ${BACKUPDIR})
     fi
     # run the command
     if [ "$BCKUP_COMM" ]; then 
@@ -333,7 +391,7 @@ function backup_data() {
        fi
 }
 
-function backup_restore() {
+backup_restore() {
        BACKUPDIR="$1"
        echo "restoring backup from $BACKUPDIR"
        echo "This will overwrite all your notes and configurations with the backup."
@@ -365,6 +423,13 @@ function backup_restore() {
                                fi
                                cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
                        fi
+                       # restoring git repo subdirectory
+                       if [ -d $BACKUPDIR/.git ]; then
+                               if [ /bin/ls -A ${BASEDIR}/.git ]; then
+                                       rm -rf ${BASEDIR}/.git
+                               fi
+                               cp -r --verbose ${BACKUPDIR}/.git ${BASEDIR}/
+                       fi
                        ;;
                * )
                        echo "No changes made. Exiting"
@@ -373,7 +438,7 @@ function backup_restore() {
        esac
 }
 
-function editnote() {
+editnote() {
        NOTE=$1
        # shellcheck disable=SC2155
        local OK=$(check_noteID "$NOTE")
@@ -396,7 +461,9 @@ function editnote() {
                 exit 1
        fi
 }
-function listnotes() {
+listnotes() {
+       # attempt syncing before listing all notes
+       gitsync
        # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
        if [[ $(ls -A "$NOTESDIR") ]]; then
                if [ $PLAIN == false ]; then
@@ -418,7 +485,7 @@ function listnotes() {
                echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
        fi
 }
-function rmnote() {
+rmnote() {
        # remove eventually existing temp DB file
        if [[ -f $TMPDB ]]; then
                rm $TMPDB
@@ -473,7 +540,7 @@ function rmnote() {
                fi
        fi
 }
-function shownote() {
+shownote() {
        NOTE=$1
 
        # shellcheck disable=SC2155
@@ -491,7 +558,7 @@ function shownote() {
        fi
 }
 # shellcheck disable=SC2006
-GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,sync,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
+GOPT=$(getopt -o hvplr::a::e::d::s:: --long help,version,list,plain,userconf,showconf,sync::,restore::,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
 
 # shellcheck disable=SC2181
 if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
@@ -584,7 +651,19 @@ while true; do
                        exit
                        ;;
                --sync )
-                       gitsync
+                       case "$2" in
+                               '' )
+                                       gitsync
+                                       ;;
+                               '-f' )
+                                       gitsync -f
+                                       ;;
+                               * )
+                                       helptext
+                                       exit
+                                       ;;
+                       esac
+                       shift 2
                        exit
                        ;;
                --userconf )
@@ -594,6 +673,10 @@ while true; do
                        # shellcheck disable=SC2317
                        exit
                        ;;
+               --showconf )
+                       configtext
+                       exit
+                       ;;
                --backup )
                        case "$2" in
                                '' )