#!/bin/sh

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

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

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

EOT

echo "Checking prerequisites..."
echo

printf "* gcs running..."
curl --silent "http://localhost:7575" > /dev/null
if [ $? -eq 0 ]
then
  echo " "$GREEN"OK"$RESET
else
  echo " "$RED"not running"$RESET
  printf $YELLOW
cat <<"EOT"

You need to run gcs (by default, on port 7575). Just run gcs command without any options on another terminal.

  $ gcs

EOT
  printf $RESET
fi

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


run() {
  "$@"
  result=$?

  if [ $result -ne 0 ]
  then
    echo " "$RED"Failed:"$RESET $CYAN"$@"$RESET $YELLOW"[$PWD]"$RESET
    exit $result
  fi

  return 0
}

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"

run $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"
run $bin_path/gcs-configure-fields --domain-name example --name name --type text --option result "$@"
#run $bin_path/gcs-configure-fields --domain-name example --name name --type text --option result --option facet "$@"
echo
echo "== Creating 'address' field"
run $bin_path/gcs-configure-fields --domain-name example --name address --type text --option result "$@"
#run $bin_path/gcs-configure-fields --domain-name example --name address --type text --option result --option facet "$@"
echo
echo "== Creating 'email_address' field"
run $bin_path/gcs-configure-fields --domain-name example --name email_address --type text --option result "$@"
#run $bin_path/gcs-configure-fields --domain-name example --name email_address --type text --option result --option facet "$@"
echo
echo "== Creating 'products' field"
run $bin_path/gcs-configure-fields --domain-name example --name products --type literal --option search --option result "$@"
#run $bin_path/gcs-configure-fields --domain-name example --name products --type literal --option search --option result --option facet "$@"
echo

echo
echo "==== Indexing data"
run $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"
EOT
