#!/bin/sh

GREEN="\033[0;32;1m"
RED="\033[0;31;1m"
YELLOW="\033[1;33;1m"
RESET="\033[m"

cat <<EOT
Groonga CloudSearch example data importer
-----------------------------------------

This script setup an example domain and import data into the domain.

EOT

cat <<EOT

NOTICE:

If you have data in the domain whose name is 'example', it will be updated.


Hit enter to continue. Ctrl-C to break.
EOT
read enter


path_prefixes="`dirname $0`/.."
if [ -L "$0" ]; then
  real_dirname="`dirname $0`/`readlink $0`"
  path_prefixes="$path_prefixes `dirname $real_dirname`/.."
fi
path_prefixes="$path_prefixes `npm root`/gcs `npm -g root`/gcs"
base_path=
bin_path=
examples_path=
for path_prefix in $path_prefixes
do
  base_path="$path_prefix"

  if [ -z "$bin_path" ]; then
    bin_path="$base_path/bin"
    if [ ! -d "$bin_path" ]; then
      bin_path=
    fi
  fi

  if [ -z "$examples_path" ]; then
    examples_path="$base_path/examples"
    if [ ! -d "$examples_path" ]; then
      examples_path=
    fi
  fi
done


echo "==== Deleting 'example' domain (if exists)"
$bin_path/gcs-delete-domain --domain-name example --force
echo

echo "==== Creating 'example' domain"

$bin_path/gcs-create-domain --domain-name example
echo
echo

documents_endpoint=`$bin_path/gcs-describe-domain --domain-name example | grep "Document Service endpoint" | cut -d " " -f 4`
search_endpoint=`$bin_path/gcs-describe-domain --domain-name example | grep "Search Service endpoint" | cut -d " " -f 4`

echo "==== Adding index fields"
echo "== Creating 'name' field"
$bin_path/gcs-configure-fields --domain-name example --name name --type text --option result
$bin_path/gcs-configure-fields --domain-name example --name name --type text --option facet
echo
echo "== Creating 'address' field"
$bin_path/gcs-configure-fields --domain-name example --name address --type text --option result
$bin_path/gcs-configure-fields --domain-name example --name address --type text --option facet
echo
echo "== Creating 'email_address' field"
$bin_path/gcs-configure-fields --domain-name example --name email_address --type text --option result
$bin_path/gcs-configure-fields --domain-name example --name email_address --type text --option facet
echo
echo "== Creating 'products' field"
$bin_path/gcs-configure-fields --domain-name example --name products --type literal --option search
$bin_path/gcs-configure-fields --domain-name example --name products --type literal --option result
$bin_path/gcs-configure-fields --domain-name example --name products --type literal --option facet
echo

echo
echo "==== Indexing data"
$bin_path/gcs-post-sdf --domain-name example --source $examples_path/example.sdf.json
echo

echo
echo "Done."

cat <<EOT

Now you can try searching by

$ curl "http://$search_endpoint/2011-02-01/search?q=Tokyo"

or, open
http://localhost:7575
for web dashboard.
EOT
