#!/bin/bash

#set -x

show_usage()
{
   i=0
   i=$((i+1)); usage[$i]="Usage: $0 [-r<data_dir>] [-d<dump_dir>] [-c] [-f<path_to_p2j.jar>] [-a<path_to_app.jar>] \
--db_admin=<dbadmin> --db_adminpw=<dbadminpw> --db_user=<dbuser> --db_userpw=<dbuserpw> --db_names=<db names> --db_host=<dbhost> --db_port=<dbport>"
   i=$((i+1)); usage[$i]="Where:"
   i=$((i+1)); usage[$i]="\t r = Relative path to the data directory (def=$reldata_dir_name)"
   i=$((i+1)); usage[$i]="\t d = Path to the data dump directories if performing an import (def=$dump_dir_name)"
   i=$((i+1)); usage[$i]="\t c = Running in a container. If this option is passed in, \"--db_host=\" specifies a container"
   i=$((i+1)); usage[$i]="\t f = Path to p2j.jar (def=$fwd_jar)"
   i=$((i+1)); usage[$i]="\t a = Path to app.jar (def=$app_jar)"
   i=$((i+1)); usage[$i]="\t--db_admin=<dbadmin> DB admin user for DB creation (def=$dbadmin)"
   i=$((i+1)); usage[$i]="\t--db_adminpw=<dbadminpw> DB admin user password for the import (def=$dbadmin_pw)"
   i=$((i+1)); usage[$i]="\t--db_user=<dbuser> DB user for the import (def=$dbuser)"
   i=$((i+1)); usage[$i]="\t--db_userpw=<dbuserpw> DB user password for the import (def=$dbuser_pw)"
   i=$((i+1)); usage[$i]="\t--db_names=<db names> DB names for import (def=$dbnames)"
   i=$((i+1)); usage[$i]="\tOption format is \"<db names (separate multiples with commas)>\")"
   i=$((i+1)); usage[$i]="\t\t For example: \"--db_names=hotel\" or \"--db_names=hotel,ledger,prices\""
   i=$((i+1)); usage[$i]="\t--db_host=<dbhost> PostGreSQL server name or IP. If -c is specified, this is a container name (def=$dbhost)"
   i=$((i+1)); usage[$i]="\t--db_port=<dbport> Port to connect to for DB access (def=$dbport)"
   i=$((i+1)); usage[$i]="\tNOTE: APP_DBADMIN_PASSWORD and APP_DBUSER_PASSWORD variables can be set in the environment instead of passed on command line."

   for i in "${usage[@]}"; do
      echo -e $i
   done
}

# Function to split a comma-delimited string into an array
split_string()
{
   local input_string="$1"   # The input comma-delimited string

   IFS=',' read -ra array <<< "$input_string"
   echo "${array[@]}"
   unset IFS
}

get_container_ip()
{
   local cname=$1
   if [ "$cname" == "localhost" ]; then
      echo $cname
   else 
      docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $cname
   fi
}

dbadmin=$APP_DBADMIN                   # fwd_admin
dbadmin_pw=$APP_DBADMIN_PASSWORD       # admin
db_table_schema=$APP_TABLE_SCHEMA      # From conversion: ddl/schema_table_${dbnames}_postgresql.sql
dbuser=$APP_DBUSER                     # fwd_user
dbuser_pw=$APP_DBUSER_PASSWORD         # user
dbnames=$APP_DBNAMES                   # appdb
dbport=$APP_DBPORT                     # 5432
reldata_dir_name=$APP_RELDATA_DIR      # data
dump_dir_name=$APP_DUMP_DIR            # $APP_DUMP_DIR
dbhost=$DB_CONTAINER                   # app_pg14
fwd_jar=/opt/fwd/build/lib/p2j.jar
app_jar=/opt/hotel/lib/hotel.jar

# Sanity check default values
[ -z "$dbadmin" ] && dbadmin="fwd_admin"
[ -z "$dbadmin_pw" ] && dbadmin_pw="admin"
[ -z "$dbuser" ] && dbuser="fwd_user"
[ -z "$dbuser_pw" ] && dbuser_pw="user"
[ -z "$dbnames" ] && dbnames="hotel"
[ -z "$dbport" ] && dbport="5432"
[ -z "$reldata_dir_name" ] && reldata_dir_name="data"
[ -z "$db_table_schema" ] && db_table_schema="ddl/schema_table_${dbnames}_postgresql.sql"
[ -z "$dump_dir_name" ] && dump_dir_name="${reldata_dir_name}/dump"
[ -z "$dbhost" ] && dbhost="localhost"
[ -z "$fwd_jar" ] && fwd_jar="./p2j/build/lib/p2j.jar"
[ -z "$app_jar" ] && app_jar="./build/lib/hotel.jar"

# process options
while getopts "h?cr:d:f:a:-:" opt; do
   case $opt in
      c  ) use_container=true
           ;;
      r  ) reldata_dir_name=$OPTARG
           ;;
      d  ) dump_dir_name=$OPTARG
           ;;
      f  ) fwd_jar=$OPTARG
           ;;
      a  ) app_jar=$OPTARG
           ;;
      -  ) case ${OPTARG} in
              "db_admin"*    ) dbadmin=$(echo $OPTARG | cut -d"=" -f2) ;;
              "db_adminpw"*  ) dbadmin_pw=$(echo $OPTARG | cut -d"=" -f2) ;;
              "db_user"*     ) dbuser=$(echo $OPTARG | cut -d"=" -f2) ;;
              "db_userpw"*   ) dbuser_pw=$(echo $OPTARG | cut -d"=" -f2) ;;
              "db_names"*    ) dbnames=$(echo $OPTARG | cut -d"=" -f2) ;;
              "db_port"*     ) dbport=$(echo $OPTARG | cut -d"=" -f2) ;;
              "db_schema"*   ) db_table_schema=$(echo $OPTARG | cut -d"=" -f2) ;;
              "db_host"*     ) dbhost=$(echo $OPTARG | cut -d"=" -f2) ;;
           esac ;;
      h | \
      \? ) show_usage && exit 1
           ;;
   esac
done
shift $(($OPTIND - 1))

# Sanity check passed in values
db_array=($(split_string "$dbnames"))
[ "$use_container" == "true" ] && pg_name=$(get_container_ip ${dbhost}) || pg_name=$dbhost
if [ ! -z "$dump_dir_name" ] && [ ! -e "$dump_dir_name" ]; then
   echo "ERROR: cannot find ${dump_dir_name}. Check -d option."
   show_usage && exit 1
fi
if [ ! -e "$fwd_jar" ]; then
   echo "ERROR: cannot find ${fwd_jar}. Check -f option."
   show_usage && exit 1
fi
if [ ! -e "$app_jar" ]; then
   echo "ERROR: cannot find ${app_jar}. Check -a option."
   show_usage && exit 1
fi

# Grab the DDL from the app.jar
temp_dir=$(mktemp -d "tempdir_ddl_XXXXXX")
unzip -qj $app_jar ddl/schema_table_* -d $temp_dir
for item in "${db_array[@]}"; do
   db_table_schema="schema_table_${item}_postgresql.sql"
   if [ ! -e "${temp_dir}/${db_table_schema}" ]; then
      echo "ERROR: cannot find ${db_table_schema} in ${app_jar}. Check build."
      show_usage && exit 1
   fi
done

# Loop through our list of DBs
for item in "${db_array[@]}"; do
   export PGPASSWORD=$dbadmin_pw
   # Drop the DB, if necessary
   if psql -U $dbadmin -h $pg_name -p $dbport -lqt | cut -d \| -f 1 | tr -s '[:space:]' ' ' | grep -qw "$item"; then
      echo "Dropping DB $item ..."
      dropdb -U $dbadmin -h $pg_name -p $dbport $item
   fi

   # Create the DB
   echo "Creating DB $item using OWNER $dbadmin ..."
   createdb -U $dbadmin -h $pg_name -p $dbport $item

   db_table_schema=${temp_dir}/schema_table_${item}_postgresql.sql
   echo "Creating schema for $item from $db_table_schema ..."
   export PGPASSWORD=$dbuser_pw
   psql -U $dbuser -h $pg_name -p $dbport -f $db_table_schema $item

   echo "Setup UDFs ..."
   db_url=jdbc:postgresql://${pg_name}:${dbport}/${item}
   cmd="java -Xmx1g -Dfile.encoding=UTF-8 -cp $fwd_jar com.goldencode.p2j.persist.deploy.ScriptRunner $db_url $dbadmin $dbadmin_pw udf.install.search_path"
   echo "Performing cmd=$cmd"
   eval $cmd

   if [ ! -z "$dump_dir_name" ]; then
      echo "Import dump from ${dump_dir_name}/${item} into $item  ..."
      cmd="java -Xmx1g -Djava.system.class.loader=com.goldencode.asm.AsmClassLoader -Dfile.encoding=UTF-8 \
          -cp ${fwd_jar}:${app_jar} com.goldencode.p2j.pattern.PatternEngine \
          -d 2 \
          "dbName=\\\"${item}\\\"" \
          "targetDb=\\\"postgresql\\\"" \
          "url=\\\"${db_url}\\\"" \
          "uid=\\\"${dbuser}\\\"" \
          "pw=\\\"${dbuser_pw}\\\"" \
          "dataPath=\\\"${dump_dir_name}/${item}/\\\"" \
          "maxThreads=4" \
          schema/import \
          ${reldata_dir_name}/ \
          ${item}.p2o"

      echo "Performing cmd=$cmd"
      eval $cmd
   else
      echo "WARNING: no data import requested."
   fi
done

unset PGPASSWORD
rm -fr $temp_dir

