#!/bin/bash
#
# install_example script
#
# Allows rapid and silent installation of vertica example software
# included with the Vertica Analytic Database product
#
# The following example can be installed:
#
# Vmart
#
# The corresponding example directories must exist in the /opt/vertica/examples directory
#
# This Vertica Script is bound to the Terms and Conditions referred to in the Vertica Script EULA Agreement (http://www.vertica.com/termsofuse)
#
check_file_exists(){
  local message
  if [ -z "$1" ]
  then
    echo "-Parameter #1 is missing. Please try again.-"  # Or no parameter passed.
    exit 1 
  fi
  if [ -z "$2" ]
  then
    message="File"
  else
    message=$2
  fi

  if [ ! -f $1 ]
  then
    echo "$message $1 does not exist"
    exit 1
  fi
}

check_file_executable(){
  local message
  if [ -z "$1" ]
  then
    echo "-Parameter #1 is missing. Please try again.-"  # Or no parameter passed.
    exit 1
  fi
  if [ -z "$2" ]
  then
    message="File"
  else
    message=$2
  fi

  if [ ! -x $1 ]
  then
    echo "$message $1 does not have execute permission"
    exit 1
  fi
}

check_dir_exists(){
  local message
  if [ -z "$1" ]
  then
    echo "-Parameter #1 is missing. Please try again.-"  # Or no parameter passed.
    exit 1
  fi
  if [ -z "$2" ]
  then
    message="Directory"
  else
    message=$2
  fi

  if [ ! -d $1 ]
  then
    echo "$message $1 does not exist"
    exit 1
  fi
}

usage(){
  echo "Script to install the Vertica Analytic Database example database"
  echo "Usage: $progname schema_example"
  echo "where schema_example is the following:"
  echo "  [ $examples ]"
}

ARGS=1
progname=`basename $0`
examples='VMart'

if [ `id -u` -eq 0 ]; then
  echo "The root user is not allowed to run this script."
  exit 1
fi

if [ $# -ne "$ARGS" ]
then
  usage
  exit 1
fi

#echo "$progname $1"
vertica_install_dir=/opt/vertica
vertica_example_dir=$vertica_install_dir/examples
vertica_example_log=$vertica_example_dir/log/ExampleInstall.txt
example=''
for i in $examples; do if [ `echo $i|awk '{print tolower($0)}'` = `echo $1|awk '{print tolower($0)}'` ]; then  example=$i;example_lower=`echo $i|awk '{print tolower($0)}'`; fi; done
if [ -z "$example" ]
then
  echo "Wrong parameter was provided"
  echo "Allowed parameters:" $examples
  usage
  exit 1
fi

# Check if we have permisions and all scripts to do the job
check_dir_exists ${vertica_example_dir}/${example}_Schema
example_dir=${vertica_example_dir}/${example}_Schema
check_file_exists ${example_dir}/${example_lower}_gen "Data generator"
check_file_executable ${example_dir}/${example_lower}_gen "Data generator" 
check_file_exists ${example_dir}/${example_lower}_define_schema.sql "Create schema script"
check_file_exists ${example_dir}/${example_lower}_load_data.sql "Data load script"
check_file_exists ${vertica_install_dir}/bin/vsql "vsql file"
check_file_executable ${vertica_install_dir}/bin/vsql "vsql file"
check_file_exists ${vertica_install_dir}/bin/adminTools "adminTools file"
check_file_executable ${vertica_install_dir}/bin/adminTools "adminTools file"

eula_acceptance="${vertica_install_dir}/config/d5415f948449e9d4c421b568f2411140.dat"
if [ ! -f ${eula_acceptance} ]; then
    echo  "EULA not yet accepted.  Please run admintools"
    exit 1
fi

# Check if database already exists
active_dbs=`${vertica_install_dir}/bin/adminTools -t show_active_db`
for i in $active_dbs; do if [ "$i" = "$example" ]; then  echo "Database $example already exists"; exit 1; fi; done

echo "Installing $1 example database"

# Create database
if [ ! -d $vertica_example_dir/log ]
then
   mkdir -p $vertica_example_dir/log
fi

rm -rf $vertica_example_log
date >> $vertica_example_log
date
echo "Creating Database"
# Get host name to use in db creation from admintools
host=`${vertica_install_dir}/bin/admintools -t list_host | cut -f1 -d,`
${vertica_install_dir}/bin/adminTools -t create_db -s $host -d $example >> ${vertica_example_log}

# Check if created database was successful
created=''
active_dbs=`${vertica_install_dir}/bin/adminTools -t show_active_db`
for i in $active_dbs; do if [ "$i" = "$example" ]; then  created=$i; fi; done
if [ -z "$created" ]
then
  echo "Failed to create database $example"
  exit 1
fi
echo "Completed"
echo "Generating Data. This may take a few minutes."
# Generate data files in $example_dir
cd $example_dir
# echo $example_dir/${example_lower}_gen
${example_dir}/${example_lower}_gen >> ${vertica_example_log}

# Create schema
echo "Completed"
echo "Creating schema"
# echo ${vertica_install_dir}/bin/vsql -d $example -f ${example_dir}/${example_lower}_define_schema.sql
${vertica_install_dir}/bin/vsql -d $example -f ${example_dir}/${example_lower}_define_schema.sql >> ${vertica_example_log}

# Load data
echo "Completed"
echo "Loading 5 million rows of data. Please stand by."
# echo ${vertica_install_dir}/bin/vsql -d $example -f ${example_dir}/${example_lower}_load_data.sql
${vertica_install_dir}/bin/vsql -d $example -f ${example_dir}/${example_lower}_load_data.sql >> ${vertica_example_log}

# remove data files
echo "Completed"
echo "Removing generated data files"
rm -rf ${example_dir}/*.tbl  >> ${vertica_example_log}
#Finish
# echo "$progname $1 finished."
echo "Example database creation complete"
echo 
echo "Log is located in $vertica_example_log"
echo
date
date >> $vertica_example_log
