#!/usr/bin/ksh

# Usage of the file

if [[ $# -ne 6 ]]
then
 ${ECHO} CREATE_COMPANY_INFO_USAGE_SH " db2_migration.sh --db=databasename --dbuser=touser --dbpassword=touserpassword --backupdir=DumpDirectory --fromversion=wpcversion"
 ${ECHO} MIGRATION_INFO_EXAMPLE_SH " db2_migration.sh --db=wpcdb --dbuser=wpc2 --dbpassword=ibm4wpc2 --backupdir=/home/dbdump --fromversion=530" 
 ${ECHO} "Possible values for fromversion is one of 53, 5301, 531, 531TFx or 532"
 exit 0
fi

# Declare the variables
DBName=`${ECHO} $1 |${CUT} -d "=" -f2`;export DBName
DBUSER=`${ECHO} $2 |${CUT} -d "=" -f2`;export DBUSER
DBUSER=`${ECHO} ${DBUSER}| ${TR} 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
export DBUSER
DBUserPassword=`${ECHO} $3 |${CUT} -d "=" -f2`;export DBUserPassword
DUMPDIR=`${ECHO} $4 |${CUT} -d "=" -f2`;export DUMPDIR
FROMVERSION=`${ECHO} $5 |${CUT} -d "=" -f2`;export FROMVERSION
FROMVERSION=`${ECHO} ${FROMVERSION}| ${TR} 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
export FROMVERSION

if [ "$CCD_DB" != "db2" ]
  then
    ${ECHO} MIGRATION_INFO_MIGRATE_VALID_DB2_ONLY_SH
    exit 0;
fi
if [ "${FROMVERSION}" == "53" ]
  then
     ${CP} $DB_SCRIPTS/create_schema_530_532.sql $DUMPDIR/create_schema_with_integer.sql
elif [ "${FROMVERSION}" == "5301" ] || [ "${FROMVERSION}" == "531" ] || [ "${FROMVERSION}" = "531X" ]
  then
     ${CP} $DB_SCRIPTS/create_schema_5301_532.sql $DUMPDIR/create_schema_with_integer.sql
elif [ "${FROMVERSION}" == "531TFX" ]
  then
     ${CP} $DB_SCRIPTS/create_schema_531TF2_532.sql $DUMPDIR/create_schema_with_integer.sql
elif [ "${FROMVERSION}" == "532" ]
  then
     ${CP} $DB_SCRIPTS/create_schema_532bigint_532.sql $DUMPDIR/create_schema_with_integer.sql
else 
    ${ECHO} "Value provided for fromversion argument is wrong. It should be one of 53, 5301, 531, 531TFx or 532."
    exit 0;
fi

cd ${DUMPDIR}

# Read the EXPORT.out file for any errors
if [ `${GREP} -c SQLSTATE ${DUMPDIR}/EXPORT.out` -ne "0" ]
 then
  ${ECHO} MIGRATION_INFO_MIGRATE_EXPORT_ERR_SH
  ${ECHO} MIGRATION_INFO_MIGRATE_EXPORT_FILE_SH "${DUMPDIR}/EXPORT.out"
  exit 0;
fi

# Filter for the import only tables from db2look file
# to create only tables, indexes and functions. NO sequences will be created
${SED} -e '/-- This CLP file was created using/,/-- DDL statements for User Defined Functions/d' -e '/-- Database and Database Manager configuration parameters/,/-- Environment Variables settings/d'  db2look.sql  > create_schema.sql

${CP}  db2move.lst  db2move.lst.bkp

exporteduser=`${GREP} "SET CURRENT SCHEMA = " create_schema.sql | sort -u |${CUT} -d "=" -f2 |${CUT} -d "\"" -f2|${CUT} -d " " -f1`;export exporteduser
if [ "${exporteduser}"  !=  "$DBUSER" ]
then
 # replace the username
 ${SED} -e "s/${exporteduser}/$DBUSER/g"  create_schema.sql > create_schema_${DBUSER}.sql
 ${SED} -e "s/${exporteduser}/$DBUSER/g"  db2move.lst >  db2move.lst_${DBUSER}.sql
 ${MV} db2move.lst_${DBUSER}.sql db2move.lst
  
else
 ${CP} create_schema.sql create_schema_${DBUSER}.sql
fi

${ECHO} "commit work;" >> create_schema_${DBUSER}.sql
${ECHO} "terminate;" >> create_schema_${DBUSER}.sql

${SED} -e "s/530_to_532/$DBUSER/g"  create_schema_with_integer.sql > create_schema_with_integer_${DBUSER}.sql
${ECHO} "commit work;" >> create_schema_with_integer_${DBUSER}.sql
${ECHO} "terminate;" >> create_schema_with_integer_${DBUSER}.sql


# create only tables, indexes and functions

db2 "connect to ${DBName} user ${DBUSER} using ${DBUserPassword}"
db2 -tvf create_schema_${DBUSER}.sql | tee  create_schema_${DBUSER}.log


db2 "connect to ${DBName} user ${DBUSER} using ${DBUserPassword}"
db2 -tvf create_schema_with_integer_${DBUSER}.sql | tee  create_schema_with_integer_${DBUSER}.log

# Import data
db2 "connect to ${DBName} user ${DBUSER} using ${DBUserPassword}"
db2 -v "create table ${DBUSER}.seqtab (seqname varchar(30), latestval integer)"
db2move ${DBName} import  -u ${DBUSER} -p ${DBUserPassword} -io insert | tee import_${DBUSER}.log
db2 terminate

# Importing the Sequences from the backup
db2 -v "connect to ${DBName} user ${DBUSER} using ${DBUserPassword}"
db2 "select 'create sequence ${DBUSER}.',seqname,' start with ',latestval,';' from seqtab" > create_sequences.sql
${CAT} create_sequences.sql | ${GREP} -i "create sequence ${DBUSER}" > create_sequences_${DBUSER}.sql
db2 -tvf create_sequences_${DBUSER}.sql | tee create_sequences_${DBUSER}.log
db2 terminate

# Running runstats to collect statistics on the tables and indexes for optimizer
db2 "connect to ${DBName} user ${DBUSER} using ${DBUserPassword}"
db2 "select 'runstats on table ${DBUSER}.'||tabname||' and indexes all;' from syscat.tables where TABSCHEMA = '${DBUSER}' and type = 'T'" >runstats.sql
${CAT} runstats.sql|${GREP} -i "runstats on table" > runstats_${DBUSER}.sql
db2 -tvf runstats_${DBUSER}.sql | tee runstats_${DBUSER}.log
db2 terminate

# Restoring the original files for the next import
${MV} db2move.lst.bkp db2move.lst
${RM} create_schema.sql
${RM} create_schema_${DBUSER}.sql
${RM} create_schema_with_integer_${DBUSER}.sql
${RM} create_sequences_${DBUSER}.sql
${RM} runstats.sql
${RM} runstats_${DBUSER}.sql
${RM} create_sequences.sql
${RM} create_schema_with_integer.sql
exit 0

