Refactor logic from gerrit.sh into gerrit/filters.  Add modular usage/help

Change-Id: I2ff1d0eda2bb7e80dab8d345721f39c621357774
diff --git a/jira/jira-search.sh b/jira/bin/jira-search.sh
similarity index 93%
rename from jira/jira-search.sh
rename to jira/bin/jira-search.sh
index 31c22ce..97b8cc2 100755
--- a/jira/jira-search.sh
+++ b/jira/bin/jira-search.sh
@@ -3,6 +3,21 @@
 ## Intent: Construct a jira ticket query with attributes
 ## --------------------------------------------------------------------
 
+{ # loader
+    declare pgm=''
+    pgm="$(realpath --canonicalize-existing "$0")"
+
+    declare root=''
+    root="${pgm%%/jira/bin/jira-search.sh}"
+    source "$root/lf/onf-common/common.sh" '--common-args-begin--'
+
+    pgm_lib="${root}/jira/jira-search"
+    readonly pgm_lib
+
+    pgm_bin="${root}/bin"
+    readonly pgm_bin
+}
+
 # set -euo pipefail
 #source ~/.sandbox/trainlab-common/common.sh '--common-args-begin--'
 
@@ -22,9 +37,12 @@
 declare -g -a projects=()
 
 path="$(realpath $0 --canonicalize-existing)"
-source "${path%\.sh}/utils.sh"
-source "$pgmlib/fixversion.sh"
-source "$pgmlib/resolved.sh"
+# source "${path%\.sh}/utils.sh"
+source "${pgm_lib}/utils.sh"
+#source "$pgmlib/fixversion.sh"
+#source "$pgmlib/resolved.sh"
+source "${pgm_lib}/fixversion.sh"
+source "${pgm_lib}/resolved.sh"
 
 ## --------------------------------------------------------------------
 ## --------------------------------------------------------------------
@@ -55,7 +73,7 @@
 function conjunction()
 {
     return
-    
+
     local -n ref=$1; shift
     [[ $# -gt 0 ]] && { local literal="$1"; shift; }
 
@@ -96,7 +114,7 @@
     declare -n ans=$1; shift
 
     # [ -z ${args+word} ] && { args=(); }
-    
+
     if [[ ${#args[@]} -gt 0 ]]; then
 
         local modifier
@@ -270,7 +288,10 @@
     do
         _tmp+=("$val" 'AND')
     done
-    unset _tmp[-1]
+
+    if [[ ${#_tmp[@]} -gt 0 ]]; then
+        unset _tmp[-1]
+    fi
 
     ## -----------------------
     ## Massage with html codes
@@ -304,10 +325,12 @@
 Usage: $0 VOL-xxxx
   --debug       Enable script debug mode
   --dry-run     Simulate
+  --todo        Display future enhancements
 
   VOL-{xxxx}    View a jira ticket by ID
 
 [SERVER]
+  --server {cord,onf}
   --onf         jira.opennetworking.org (default)
   --opencord    jira.opencord.org
 
@@ -430,8 +453,17 @@
         ##------------------##
         ##---]  SERVER  [---##
         ##------------------##
-        -*onf) declare server='jira.opennetworking.org'; error "FOUND --onf" ;;
-        -*cord) declare server='jira.opencord.org' ;;
+        --serv*)
+            arg="$1"; shift
+            case "$arg" in
+                *cord*) server='jira.opencord.org' ;;
+                 *onf*) server='jira.opennetworking.org' ;;
+                 *) error "--server [$arg] expected opencord or onf" ;;
+            esac
+            ;;
+
+         --onf) declare server='jira.opennetworking.org' ;;
+        --cord) declare server='jira.opencord.org'     ;;
 
         ##---------------------##
         ##---]  SEARCH-BY  [---##
@@ -474,6 +506,7 @@
             ;;
 
         --all) set -- '--resolved-is-none' "$@" ;; # alias: --[un-]resolved
+        --todo) source "${pgm_lib}/todo.sh" ;;
 
         --proj*) projects+=("$1"); shift ;;
 
@@ -583,6 +616,11 @@
     esac
 done
 
+## --------------
+## Required check
+## --------------
+[[ ! -v server ]] && { error "--server={cord,onf} is required"; }
+
 ## ----------------------
 ## Construct query filter
 ## ----------------------
diff --git a/jira/resolved.sh b/jira/bin/resolved.sh
similarity index 100%
rename from jira/resolved.sh
rename to jira/bin/resolved.sh
diff --git a/jira/jira-search/todo.sh b/jira/jira-search/todo.sh
new file mode 100644
index 0000000..c083b77
--- /dev/null
+++ b/jira/jira-search/todo.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+## -----------------------------------------------------------------------
+## -----------------------------------------------------------------------
+function __anonymous()
+{
+    cat <<EOT
+
+** -----------------------------------------------------------------------
+** [TODO]
+** -----------------------------------------------------------------------
+    o --component bbsim --component ci-management
+      # Component not working unless project passed
+    project = VOL AND component in (bbsim, bbsim-sadis-server, ci-management)
+EOT
+    return
+}
+
+__anonymous
+unset __anonymous
+
+: # ($?==0) for source script return
+# [EOF]