Add options and refactoring
diff --git a/coverage-tool/coverage-reporting/merge.sh b/coverage-tool/coverage-reporting/merge.sh
index 8304487..e0ec69c 100755
--- a/coverage-tool/coverage-reporting/merge.sh
+++ b/coverage-tool/coverage-reporting/merge.sh
@@ -138,8 +138,7 @@
 #   1-Json object that defines the locations of the info and json
 #       files
 #   2-Folder to save the info and json files
-#   3-Variable that holds the name of the variable that will hold
-#       the name of the file to be downloaded (reference argument)
+#   3-Reference argument to hold the copied file name location
 # Outputs:
 #   None
 ################################################################
@@ -192,27 +191,35 @@
 }
 
 #####################################################################
-# Get (download/copy) info and json files from the input json file
+# Get (download/copy) info and json files from the configuration json
+# file
 # Globals:
-#   merge_input_json_file: Input json file with locations of info
-#                          and intermediate json files to be merged.
-#   input_folder: Folder to put info and json files to be merged
+#   merge_configuration_file: Input json file with locations of info
+#                             and json scm files to be merged.
+#   info_files: Array of info file locations.
 # Arguments:
-#   None
+#   1: Target folder to download info and json files to be merged.
 # Outputs:
 #   None
 ###################################################################
 get_info_json_files() {
-  json_string="$(cat $merge_input_json_file)"
-  nf=$(get_json_object "$json_string" "-files")
+  local input_folder="${1:-$LCOV_FOLDER}"
+  local json_string="$(cat $merge_configuration_file)"
+  local config_json_file=""
+  local info_file=""
+  # Get files array
+  local nf=$(get_json_object "$json_string" "-files")
+  # Init target folder
   rm -rf $input_folder > /dev/null || true
   mkdir -p $input_folder
+  # Iterate through each file element and get the files
   for f in $(seq 0 $(($nf - 1)));
   do
     pushd $input_folder > /dev/null
     _file=$(get_json_object "$json_string" "files.$f")
+    # The name of the folder is the 'id' value
     folder=$(get_json_object "$_file" "*id")
-    echo "Geting files from project '$folder' into '$input_folder'..."
+    echo "Getting files from project '$folder' into '$input_folder'..."
     mkdir -p $folder
     bundles=$(get_json_object "$_file" "bundles" None)
     if [ "$bundles" != "None" ];then
@@ -222,8 +229,10 @@
         get_file "$(get_json_object "$bundles" "$n")" $folder
       done
     fi
+    # Download/copy files and save their locations
     get_file "$(get_json_object "$_file" "config")" $folder config_json_file
     get_file "$(get_json_object "$_file" "info")" $folder info_file
+    info_files+=($info_file)
     popd > /dev/null
   done
 }
@@ -231,36 +240,62 @@
 #################################################################
 # Merge json and info files and generate branch coverage report
 # Globals:
-#   output_coverage_file: Location and name for merge coverage info
-#   output_json_file: Location and name for merge json output
-#   input_folder: Location where reside json and info files
-#   LOCAL_WORKSPACE: Local workspace folder with the source files
+#   merged_coverage_file: Location and name for merged coverage info
+#   merged_json_file: Location and name for merged json scm sources
+#   LOCAL_WORKSPACE: Local workspace folder with the source code files
+#   generate_local: Flag to generate local lcov reports
 # Arguments:
-#   None
+#   1: Location where reside json and info files
 # Outputs:
-#   Output merge coverage file
-#   Output merge json file
+#   Merged coverage file
+#   Merged json file
 ################################################################
 merge_files() {
+  local input_folder="${1:-$LCOV_FOLDER}"
 # Merge info and json files
   local lc=" "
   if [ -n "$LOCAL_WORKSPACE" ];then
-    # Translation to be done in the info files to local workspace
+    # Translation from info workspaces into local workspace
     lc=" --local-workspace $LOCAL_WORKSPACE"
   fi
+  if [ "$generate_local" = true ];then
+    # Generate local reports
+    lc="${lc} -k"
+  fi
   # Getting the path of the merge.py must reside at the same
   # path as the merge.sh
   python3 ${DIR}/merge.py \
       $(find $input_folder -name "*.info" -exec echo "-a {}" \;) \
       $(find $input_folder -name "*.json" -exec echo "-j {}" \;) \
-      -o $output_coverage_file \
-      -m $output_json_file \
+      -o $merged_coverage_file \
+      -m $merged_json_file \
       $lc
 
 }
 
 
 #################################################################
+# Generate local lcov reports
+# Globals:
+#   info_files: Array of locations and names of info files
+# Arguments:
+#   None
+# Outputs:
+#   Lcov report files for each info file
+################################################################
+generate_local_reports() {
+  for i in ${!info_files[@]};
+  do
+    local info_file=${info_files[$i]}
+    local parentdir=$(dirname "$info_file")
+    local t_info_file="${info_file/.info/_local.info}"
+    genhtml --branch-coverage $t_info_file \
+        --output-directory $parentdir
+  done
+}
+
+
+#################################################################
 # Print scripts usage
 # Arguments:
 #   None
@@ -271,20 +306,20 @@
   clear
   echo "Usage:"
   echo "merge -h              Display this help message."
-  echo "-j <input json file>  Input json file(info and intermediate json files to be merged)."
-  echo "-l <report folder>    Folder for branch coverage report. Defaults to ./lcov_folder."
-  echo "-i <Path>             Folder to copy/download info and json files. Defaults to ./input."
-  echo "-w <Folder>           Local workspace folder for source files."
-  echo "-o <name>             Name of the merged info file. Defaults to ./coverage_merge.info"
-  echo "-m <name>             Name of the merged metadata json file. Defaults to ./merge_output.json"
-  echo "-c                    If it is set, sources from merged json files will be cloned/copied to local workspace folder."
+  echo "-j <JSON filename>    JSON configuration file (info and intermediate json filenames to be merged)."
+  echo "[-l <Report path>]    Coverage reports directory. Defaults to ./Coverage"
+  echo "[-w <Workspace path>] Workspace directory for source code files."
+  echo "[-o <Info filename>]  Merged info file. Defaults to ./merged_coverage.info"
+  echo "[-m <JSON filename>]  JSON merged SCM sources. Defaults to ./merged_scm.json"
+  echo "[-c]                  Flag to download/copy the source files from the JSON merged SCM into the workspace directory."
+  echo "[-g]                  Flag to generate local reports for each info/json instance."
   echo "$help_message"
 }
 
 help_message=$(cat <<EOF
 
-# The script that merges the info data (code coverage) and json metadata
-# (intermediate layer) needs as an input a json file with the following
+# The script merging the info files (code coverage) and json SCM sources
+# (intermediate layer) needs a JSON configuration file with the following
 # properties:
 # files: array of objects that describe the type of file/project to be
 # merged.
@@ -333,23 +368,17 @@
 )
 
 clear
-# Local workspace folder to contain source files
 LOCAL_WORKSPACE=""
-# If this is true then will clone/copy sources from merged json
-# file into local workspace
 CLONE_SOURCES=false
-# Location of the input json file that contains information about
-# the info and json files to be merged and produced a report
-merge_input_json_file=""
-# Folder to download json and info files
-input_folder="./input_folder"
+merge_configuration_file=""
+generate_local=false
 # Folder to to put the reports
-LCOV_FOLDER="./lcov_folder"
+LCOV_FOLDER="./Coverage"
 # File name for merge coverage info
-output_coverage_file="./coverage_merge.info"
-# File name for merge json output
-output_json_file="./merge_output.json"
-while getopts ":hj:o:l:w:i:cm:" opt; do
+merged_coverage_file="./merged_coverage.info"
+merged_json_file="./merged_scm.json"
+info_files=() # Array of info files
+while getopts ":hj:o:l:w:i:cm:g" opt; do
   case ${opt} in
     h )
       usage
@@ -358,23 +387,23 @@
     w )
       LOCAL_WORKSPACE=$(cd $OPTARG; pwd)
       ;;
-    i )
-      input_folder=$OPTARG
-      ;;
     c )
       CLONE_SOURCES=true
       ;;
     j )
-      merge_input_json_file=$OPTARG
+      merge_configuration_file=$OPTARG
       ;;
     l )
       LCOV_FOLDER=$OPTARG
       ;;
     o )
-      output_coverage_file=$OPTARG
+      merged_coverage_file=$OPTARG
       ;;
     m )
-      output_json_file=$OPTARG
+      merged_json_file=$OPTARG
+      ;;
+    g )
+      generate_local=true
       ;;
     \? )
       echo "Invalid option: $OPTARG" 1>&2
@@ -389,29 +418,32 @@
   esac
 done
 shift $((OPTIND -1))
-if [ -z "$merge_input_json_file" ]; then
-  echo "Input json file required"
+if [ -z "$merge_configuration_file" ]; then
+  echo "Merged configuration file required."
   usage
   exit -1
 fi
 if [ -z "$LOCAL_WORKSPACE" ] && [ $CLONE_SOURCES = true ]; then
-    echo "Need to define a local workspace folder to clone/copy sources!"
+    echo "A local workspace directory is required to clone/copy sources!"
     exit -1
 fi
-# Getting the script folder where other script files must reside, i.e
+# Getting the script folder where other qa-tools script files must reside, i.e
 # merge.py, clone_sources.py
 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
-input_folder="$(get_abs_path $input_folder)"
-LCOV_FOLDER="$(get_abs_path  $LCOV_FOLDER)"
-output_coverage_file="$(get_abs_path $output_coverage_file)"
-output_json_file="$(get_abs_path $output_json_file)"
+LCOV_FOLDER="$(get_abs_path $LCOV_FOLDER)"
+merged_coverage_file="$(get_abs_path $merged_coverage_file)"
+merged_json_file="$(get_abs_path $merged_json_file)"
 param_cloned=""
 get_info_json_files
 merge_files
 if [ $CLONE_SOURCES = true ];then
-    clone_repos $output_json_file
+    clone_repos $merged_json_file
 fi
+
 # Generate branch coverage report
-genhtml --branch-coverage $output_coverage_file \
+genhtml --branch-coverage $merged_coverage_file \
     --output-directory $LCOV_FOLDER
-cd -
+
+if [ "$generate_local" = true ];then
+  generate_local_reports
+fi