added TODO list
[bash-notes.git] / notes.sh
index 687fefe..a9afd60 100755 (executable)
--- a/notes.sh
+++ b/notes.sh
@@ -49,11 +49,11 @@ NOTESDIR=${BASEDIR}/notes
 
 # If you want to store your notes in a git repository set this to true
 USEGIT=true
-# Address of your remote repository
+# Address of your remote repository. Without this GIT will refuse to work
 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
+# The name of this client. If left empty, defaults to the output of hostname
 GITCLIENT=${GITCLIENT:-""}
 
 } # end set_defaults, do not change this line.
@@ -167,7 +167,7 @@ check_noteID() {
        IN=$1
        case $IN in
                ''|*[!0-9]*)
-                       return 1
+                       false
                        ;;
                *)
                        echo "$IN"
@@ -193,29 +193,37 @@ helptext() {
     echo -e "  -v | --version\t\t: Print version"
     echo -e "  --userconf\t\t\t: Export User config file"
     echo -e "  --backup [<dest>]\t\t: Backup your data in your destination folder"
+    echo -e "  --showconf\t\t\t: Display running options"
+    echo -e "  --sync\t\t\t: Sync notes to git repository"
     echo ""
     echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
 }
 
 configtext() {
     [ $USEGIT ] && GITUSE="enabled" || GITUSE="disabled"
+    if [ -n $GITCLIENT ]; then
+        CLIENTGIT="$( hostname )"
+    else
+        CLIENTGIT="$GITCLIENT"
+    fi
     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}"
+    echo -e "\tbase directory:     ${BASEDIR}/"
+    echo -e "\tnotes archive:      ${NOTESDIR}/"
+    echo -e "\tnotes database:     ${DB}"
+    echo -e "\trc file:            $RCFILE"
+    echo -e "\tdebug file:         /tmp/debug_bash-note.log"
+    echo
+    echo -e "\ttext editor:        ${EDITOR}"
+    echo -e "\tterminal:           ${TERMINAL}"
+    echo -e "\tjq executable:      ${JQ}"
+    echo -e "\tPAGER:              ${PAGER}"
+    echo
+    echo -e "\tGIT:                ${GITUSE} - ${GIT}"
+    echo -e "\tGIT remote:         ${GITREMOTE}"
+    echo -e "\tGIT sync delay:     ${GITSYNCDELAY}"
+    echo -e "\tGIT client name:    ${CLIENTGIT}"
 }
 
 # this function returns a random 2 words title
@@ -268,7 +276,7 @@ gitsync() {
             $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
             mv $TMPDB $DB
             cd $BASEDIR
-            $GIT pull
+            [ $PLAIN == false ] && $GIT pull || $GIT pull -q
         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")
@@ -278,7 +286,7 @@ gitsync() {
                 $JQ --arg n "$NOWSYNC" '.git["lastpull"] = $n' "$DB" > $TMPDB
                 mv $TMPDB $DB
                 cd $BASEDIR
-                $GIT pull
+                [ $PLAIN == false ] && $GIT pull || $GIT pull -q
             else
                 # Last synced less than $GITSYNCDELAY seconds ago. We shall wait
                 [ $PLAIN == false ] && echo "Last synced less than $GITSYNCDELAY seconds ago. We shall wait"
@@ -331,13 +339,15 @@ gitremove() {
             $GIT commit -m "$(basename $0) - ${GITCLIENT} removing all notes."
             $GIT push origin master
         else
-            echo "Deleting note ID ${NOTE}"
             local OK=$(check_noteID "$NOTE")
-            cd $BASEDIR
-            $GIT rm notes/${FILE}
-            $GIT add .
-            $GIT commit -m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}."
-            $GIT push origin master
+            if [[ "$OK" ]]; then
+                echo "Deleting note ID ${NOTE}"
+                cd $BASEDIR
+                $GIT rm notes/${FILE}
+                $GIT add .
+                $GIT commit -m "$(basename $0) - ${GITCLIENT} removing note ID ${NOTE}."
+                $GIT push origin master
+            fi
         fi
     else
         # no git, so we just keep going
@@ -371,8 +381,22 @@ addnote() {
                rm $TMPDB
        fi
 
+       # RANDOM TITLE
        RTITLE=$(random_title)
-       [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
+
+       if [[ -z $1 ]]; then
+               read -r -p "Title: " TITLE
+               case "$TITLE" in
+                       '' )
+                               NOTETITLE="$RTITLE"
+                               ;;
+                       * )
+                               NOTETITLE=$TITLE
+                               ;;
+               esac
+       fi
+
+       # [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
        echo "adding new note - \"$NOTETITLE\""
        # shellcheck disable=SC2086
        LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
@@ -602,7 +626,7 @@ shownote() {
        fi
 }
 # shellcheck disable=SC2006
-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' -- "$@")
+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
@@ -630,84 +654,39 @@ while true; do
                        exit
                ;;
            -a | --add )
-                       case "$2" in
-                               '' )
-                                       read -r -p "Title: " TITLE
-                                       ;;
-                               * )
-                                       TITLE=$2
-                                       ;;
-                       esac
+                       TITLE=$2
                        shift 2
                        addnote "$TITLE"
                        exit
                ;;
                -e | --edit )
-                       case "$2" in
-                               '' )
-                                       read -r -p "Note ID: " NOTE
-                                       ;;
-                               * )
-                                       NOTE=$2
-                                       ;;
-                       esac
+                       NOTE=$2
                        shift 2
                        editnote "$NOTE"
                        exit
                        ;;
                -d | --delete )
-                       case "$2" in
-                               '' )
-                                       read -r -p "Note ID: " NOTE
-                                       ;;
-                               * )
-                                       NOTE=$2
-                                       ;;
-                       esac
+                       NOTE=$2
                        shift 2
                        rmnote "$NOTE"
                        exit
                        ;;
                -s | --show )
-                       case "$2" in
-                               '' )
-                                       read -r -p "Note ID: " NOTE
-                                       ;;
-                               * )
-                                       NOTE=$2
-                                       ;;
-                       esac
+                       NOTE=$2
                        shift 2
                        shownote "$NOTE"
                        exit
                        ;;
                -r | --restore )
-                       case "$2" in
-                               '' )
-                                       read -r -p "Backup Dir: " RDIR
-                                       ;;
-                               * )
-                                       RDIR=$2
-                                       ;;
-                       esac
+                       RDIR=$2
                        shift 2
                        backup_restore $RDIR
                        exit
                        ;;
                --sync )
-                       case "$2" in
-                               '' )
-                                       gitsync
-                                       ;;
-                               '-f' )
-                                       gitsync -f
-                                       ;;
-                               * )
-                                       helptext
-                                       exit
-                                       ;;
-                       esac
-                       shift 2
+                       # I'm forcing it because if you run it manually, chances are that you need to.
+                       gitsync -f
+                       shift
                        exit
                        ;;
                --userconf )
@@ -722,14 +701,7 @@ while true; do
                        exit
                        ;;
                --backup )
-                       case "$2" in
-                               '' )
-                                       read -r -p "Backup Dir: " BDIR
-                                       ;;
-                               * )
-                                       BDIR=$2
-                                       ;;
-                       esac
+                       BDIR=$2
                        shift 2
                        backup_data $BDIR
                        exit