#!/bin/bash

# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
#
# sqgen script - generates various files

function Usage {
    script_name=`/bin/basename $0`
    echo
    echo $script_name generates various Trafodion files in the $TRAF_HOME/sql/scripts directory.
    echo
    echo "Usage: $script_name {-ft | -perf | [sqconfig_filename] | -h}"
    echo "  -h    Help"
    echo "  [sqconfig_filename] Name of the SQ config file (in $TRAF_HOME/sql/scripts directory)(defaults to 'sqconfig')"
    echo
    exit 1;
}



function GetSQcnfg {
# Get SQ Node configuration
 TempList=`grep -o 'node-name=.[A-Za-z0-9\.\-]*' $TRAF_HOME/sql/scripts/sqconfig | cut -d "=" -f 2 | cut -d ";" -f 1 | sort -u`

 i=0
 for NODE in $TempList
   do
     SQNodeNames[$i]=$NODE
     ((i=i+1))

     done

 # Check that the Node names were corretly added
 NumberOfSQnodes=${#SQNodeNames[*]}
 ExNodeList="$(echo ${SQNodeNames[@]} | tr ' ' ',')"


 if [ ! -z ${ExNodeList[@]} ]; then   
     echo "${ExNodeList[@]}"
 
 else
     echo
     echo "Could not parse $TRAF_HOME/sql/scripts/sqconfig file."
     echo "Please ensure sqenv.sh has been sourced and the sqconfig file is valid.  Then, re-run sqgen."
     echo
     exit 1;
 fi

 }

###########################################################
# MAIN portion of sqgen begins
###########################################################

FT_FLAG=1
PERF_FLAG=0
SQCONFIG_FILE=sqconfig
SQCONFIG_DB_FILE=sqconfig.db

if [ -z $TRAF_HOME ]; then
    echo
    echo "The TRAF_HOME environment variable does not exist."
    echo "Please ensure sqenv.sh has been sourced."
    echo
    exit 1;
fi


# Check whether the SQ environment is already up.
sqcheck -i 1 -d 1 > /dev/null 2>&1
sq_stat=$?
if [[ $sq_stat == 0 ]]; then
   echo "SQ environment is up. sqgen not executed. Exiting..."
   exit 1
elif [[ $sq_stat == 1 ]]; then
   echo "SQ environment is partially up. sqgen not executed. Exiting..."
   exit 1
elif [[ $sq_stat == 2 ]]; then
   echo "SQ environment is partially up. sqgen not executed. Exiting..."
   exit 1
fi

# Make sure sqgen uses the latest environment
cd $TRAF_HOME
if [ "$SQ_BUILD_TYPE" == "release" -a -f sqenvr.sh ]; then
  . ./sqenvr.sh
else
  . ./sqenv.sh
fi

if [ -n "$CHANGED_SQ_ENV_RESTART_SHELL" ]; then
  # This is set by sqenvcom.sh when the environment has changed
  # in the shell. The CLASSPATH and other variables are not reliable
  # in such a situation and we should not do an sqgen.
  echo "A change in environment variables occurred."
  echo "Please retry sqgen in a new shell. Exiting..."
  exit 1
fi

cd $TRAF_HOME/sql/scripts

# Check to make sure this is a real cluster
if  [[ -n "$node_count" ]] && [[ "$node_count" -gt "1" ]]; then 
    # The configuration database can only be created the first time.		
    # Since nodes can be added and deleted through the 'shell node add/delete'		
    # commands, the 'sqconfig' node section may not reflect the current node		
    # configured. The 'sqregen' script can be used to update the SQCONFIG_FILE		
    # node section to reflect the current node configuration stored in		
    # the SQCONFIG_DB_FILE.		
    echo "Checking for the configuration file ($SQCONFIG_DB_FILE)."		
    if [ -f $SQCONFIG_DB_FILE ]; then		
        echo		
        echo "The configuration file ($SQCONFIG_DB_FILE) exists."		
        echo "Use the 'sqshell node add/delete' commands to change the node membership in the configuration database."		
        echo "Use the 'sqshell persist add/delete' commands to change the persist object configuration in the configuration database."		
        echo "Use the 'sqregen -node' script to update the $SQCONFIG_FILE 'node' section with the"		
        echo "current node membership in the configuration database."		
        echo "Use the 'sqregen -persist' script to update the $SQCONFIG_FILE 'persist' section with the"		
        echo "current persist configuration stored in the configuration database."		
        exit 1		
    fi
    GetSQcnfg

else
    echo
    echo "Workstation environment - Not a clustered environment"
    if [ -f $SQCONFIG_DB_FILE ]; then		
        echo		
        echo "The configuration file ($SQCONFIG_DB_FILE) was previously created."		
        echo "Removing and re-creating $SQCONFIG_DB_FILE!"		
        rm -f $SQCONFIG_DB_FILE		
    fi		
fi

# assume option is SQCONFIG_FILE
while [ $# != 0 ]
  do
    flag="$1"
    case "$flag" in
        -h)  Usage ;;
        -?)  Usage ;;
        *)   SQCONFIG_FILE=$1 ;;
    esac
    shift
  done

export SQETC_DIR=$TRAF_HOME/etc
export SQLOG_DIR=$TRAF_HOME/logs
mkdir -p $SQETC_DIR
mkdir -p $SQLOG_DIR
mkdir -p $MPI_TMPDIR
# mkdir a dir for CBF data used by SQL IUS feature
mkdir -p $HOME/cbfs

if  [[ -n "$node_count" ]] && [[ "$node_count" -gt "1" ]]; then    
    echo
    echo "Creating directories on cluster nodes"

    echo "$PDSH -w ${ExNodeList[@]} -x `uname -n` $PDSH_SSH_CMD mkdir -p $SQETC_DIR "
    $PDSH -w ${ExNodeList[@]} -x `uname -n` $PDSH_SSH_CMD mkdir -p $SQETC_DIR
    
    echo "$PDSH -w ${ExNodeList[@]} -x `uname -n` $PDSH_SSH_CMD mkdir -p $SQLOG_DIR "
    $PDSH -w ${ExNodeList[@]} -x `uname -n` $PDSH_SSH_CMD mkdir -p $SQLOG_DIR

    echo "$PDSH -w ${ExNodeList[@]} -x `uname -n` $PDSH_SSH_CMD mkdir -p $MPI_TMPDIR "
    $PDSH -w ${ExNodeList[@]} -x `uname -n` $PDSH_SSH_CMD mkdir -p $MPI_TMPDIR

    echo "$PDSH -w ${ExNodeList[@]} -x `uname -n` $PDSH_SSH_CMD mkdir -p $PWD "
    $PDSH -w ${ExNodeList[@]} -x `uname -n` $PDSH_SSH_CMD mkdir -p $PWD

fi

SQSCRIPT_FILE=./gomon
SQESPENV_FILE=$TRAF_HOME/sql/scripts/tdm_arkesp.env

echo
if [ -f $SQETC_DIR/ms.env ]; then
    echo "The SQ environment variable file $SQETC_DIR/ms.env exists."
    echo "The file will not be re-generated."
    echo
else
    echo "Generating SQ environment variable file: $SQETC_DIR/ms.env"
    echo
    ./genms > $SQETC_DIR/ms.env
    lv_retcode=$?
    if [[ $lv_retcode != 0 ]]; then 
	echo "Error $lv_retcode while executing genms. Exiting..."
	exit $lv_retcode
    fi
fi

./gensqstatem2lenv > $SQETC_DIR/sqstatem2l.env

# Create configuration database tables
sqlite3 -init createConfigDb $SQCONFIG_DB_FILE <<eof 
.quit
eof

./gensq.pl $SQSCRIPT_FILE `hostname` $FT_FLAG $PERF_FLAG $SQCONFIG_FILE $TRAF_HOME/sql/scripts/sqconfig.persist
sq_stat=$?
if [[ $sq_stat != 0 ]]; then 
    exit $sq_stat;
fi

sq_seamonster=$SQ_SEAMONSTER
if [ -f $SQESPENV_FILE ]; then
    if [[ $sq_seamonster == 1 ]]; then 
        echo
        echo "Enabling tdm_arkesp.env file in $TRAF_HOME/etc/ms.env"
        echo "cat $TRAF_HOME/etc/ms.env | sed -e "s@^# SQ_PROPS_TDM_ARKESP=tdm_arkesp.env@SQ_PROPS_TDM_ARKESP=tdm_arkesp.env@" > $TRAF_HOME/etc/ms.env.TEMP"
        if [ -f $TRAF_HOME/etc/ms.env.TEMP ]; then
            rm $TRAF_HOME/etc/ms.env.TEMP
        fi
        cat $TRAF_HOME/etc/ms.env | sed -e "s@^# SQ_PROPS_TDM_ARKESP=tdm_arkesp.env@SQ_PROPS_TDM_ARKESP=tdm_arkesp.env@" > $TRAF_HOME/etc/ms.env.TEMP
        cp $TRAF_HOME/etc/ms.env.TEMP $TRAF_HOME/etc/ms.env
        rm $TRAF_HOME/etc/ms.env.TEMP
    else
        echo
        echo "Disabling tdm_arkesp.env file in $TRAF_HOME/etc/ms.env"
        echo "cat $TRAF_HOME/etc/ms.env | sed -e "s@^SQ_PROPS_TDM_ARKESP=tdm_arkesp.env@# SQ_PROPS_TDM_ARKESP=tdm_arkesp.env@" > $TRAF_HOME/etc/ms.env.TEMP"
        if [ -f $TRAF_HOME/etc/ms.env.TEMP ]; then
            rm $TRAF_HOME/etc/ms.env.TEMP
        fi
        cat $TRAF_HOME/etc/ms.env | sed -e "s@^SQ_PROPS_TDM_ARKESP=tdm_arkesp.env@# SQ_PROPS_TDM_ARKESP=tdm_arkesp.env@" > $TRAF_HOME/etc/ms.env.TEMP
        cp $TRAF_HOME/etc/ms.env.TEMP $TRAF_HOME/etc/ms.env
        rm $TRAF_HOME/etc/ms.env.TEMP
    fi
fi

if  [[ -n "$node_count" ]] && [[ "$node_count" -gt "1" ]]; then    
    echo
    echo
    echo "Copying the generated files to all the nodes in the cluster"
    echo

    echo
    echo "Copying $SQETC_DIR/ms.env to $SQETC_DIR of all the nodes"
    echo "$PDCP -w ${ExNodeList[@]} -x `uname -n` $SQETC_DIR/ms.env   $SQETC_DIR "
    $PDCP -w ${ExNodeList[@]} -x `uname -n` $SQETC_DIR/ms.env   $SQETC_DIR

    echo
    echo "Copying $SQETC_DIR/seamonster.env to $SQETC_DIR of all the nodes"
    echo "$PDCP -w ${ExNodeList[@]} -x `uname -n` $SQETC_DIR/seamonster.env   $SQETC_DIR "
    $PDCP -w ${ExNodeList[@]} -x `uname -n` $SQETC_DIR/seamonster.env   $SQETC_DIR

    if [[ $sq_seamonster == 1 ]]; then 
        if [ -f $SQESPENV_FILE ]; then
            echo
            echo "Copying $SQESPENV_FILE to $TRAF_HOME/sql/scripts of all the nodes"
            echo "$PDCP -w ${ExNodeList[@]} -x `uname -n` $SQESPENV_FILE   $TRAF_HOME/sql/scripts "
            $PDCP -w ${ExNodeList[@]} -x `uname -n` $SQESPENV_FILE   $TRAF_HOME/sql/scripts
        fi
    fi

    echo
    echo "Copying rest of the generated files to $PWD"

    echo "$PDCP -w ${ExNodeList[@]} -x `uname -n` sqconfig sqshell gomon.cold gomon.warm rmsstart rmsstop rmscheck.sql ssmpstart ssmpstop sscpstart sscpstop $PWD"
    $PDCP -w ${ExNodeList[@]} -x `uname -n` sqconfig sqshell gomon.cold gomon.warm rmsstart rmsstop rmscheck.sql ssmpstart ssmpstop sscpstart sscpstop $PWD

    echo "$PDCP -w ${ExNodeList[@]} -x `uname -n` $SQCONFIG_FILE $SQCONFIG_DB_FILE $PWD "
    $PDCP -w ${ExNodeList[@]} -x `uname -n` $SQCONFIG_FILE $SQCONFIG_DB_FILE $PWD


    echo
fi

# mkdir a dir for CBF data used by SQL IUS feature
mkdir -p $HOME/cbfs

echo
echo "******* Generate public/private certificates *******"
echo
# Generate certificates for ODBC, ...
if (test -f $TRAF_HOME/sql/scripts/sqcertgen); then
   $TRAF_HOME/sql/scripts/sqcertgen 2>/dev/null
   echo
else
   echo
   echo "ERROR: Certificate generation script (sqcertgen) does not exist in $TRAF_HOME/sql/scripts folder"
   echo
   exit 1
fi
