#!/bin/bash
################################################################################
#
# Fitting Road Network Data to a Graph Tutorial
#
# This script used to back the Fitting Road Network Data to a Graph Tutorial on
# the doc site.  It is now merely a proof of concept.
#
# A local Kinetica instance will handle calls to /create/graph, 
# /create/jointable, and /create/projection for a D.C.-based graph
#
# NOTE:
#       Running the script requires that the 'dc_shape' dataset be ingested
#       into the same Kinetica instance this script will be run against. This
#       dataset can be downloaded from the documentation site and imported via
#       GAdmin.
#
#       Also, this script makes use of the Python json.tool library for decoding
#       the endpoint response, and then within it, the API response string,
#       which should match the output returned by GAdmin and referenced in the
#       tutorial.
#
# EXAMPLE:
#       ./fit_data.sh http://localhost:9191 someuser somepassword
#
################################################################################

THIS_SCRIPT=$(basename ${BASH_SOURCE[0]})
THIS_SCRIPT_LOG=${THIS_SCRIPT%.*}.log
THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HOST_URL="${1:-http://localhost:9191}"
USERNAME="${2}"
PASSWORD="${3}"
JSON_CMD="python -mjson.tool --sort-keys"


# Extract the data_str attribute from the given JSON response object and output
# it JSONified
function extract_data_str
{
  RESP=$($@)
  if echo ${RESP} | ${JSON_CMD} | grep status | grep OK > /dev/null
  then
    echo ${RESP} | ${JSON_CMD} | \
      grep data_str | grep -o {.*} | xargs -0 printf '%b\n' | ${JSON_CMD}
  else
    echo ${RESP} | ${JSON_CMD} | grep message | cut -f4 -d'"'
  fi
}


function drop_schema
{
  curl -sS --header "Content-Type: application/json" \
    --user ${USERNAME}:${PASSWORD} \
    --data @drop_schema.json ${HOST_URL}/drop/schema
}

function create_schema
{
  curl -sS --header "Content-Type: application/json" \
    --user ${USERNAME}:${PASSWORD} \
    --data @create_schema.json ${HOST_URL}/create/schema
}

function create_graph
{
  local graph_json="create_graph.json" 
  [ "$1" = "exp" ] && graph_json="create_graph_expansion.json"

  curl -sS --header "Content-Type: application/json" \
    --user ${USERNAME}:${PASSWORD} \
    --data @${graph_json} ${HOST_URL}/create/graph
}

function create_join
{
  curl -sS --header "Content-Type: application/json" \
    --user ${USERNAME}:${PASSWORD} \
    --data @create_join_table.json ${HOST_URL}/create/jointable
}

function create_projection
{
  curl -sS --header "Content-Type: application/json" \
    --user ${USERNAME}:${PASSWORD} \
    --data @create_projection.json ${HOST_URL}/create/projection
}


echo
echo "FITTING ROAD NETWORK DATA OUTPUT"
echo "================================"
echo

echo "(RE)CREATE SCHEMA"
echo "-----------------"
echo 

extract_data_str drop_schema
echo

extract_data_str create_schema
echo
echo

echo "CREATE GRAPH (INLINE EXPRESSION)"
echo "--------------------------------"
echo 

extract_data_str create_graph
echo
echo

echo "CREATE JOIN TABLE"
echo "-----------------"
echo 

extract_data_str create_join
echo
echo

echo "CREATE PROJECTION"
echo "-----------------"
echo 

extract_data_str create_projection
echo
echo

echo "CREATE GRAPH (EXPANSION)"
echo "------------------------"
echo 

extract_data_str create_graph exp
echo
echo
