#!/bin/bash
# Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# IBM makes no representations or warranties about the suitability of
# this program, either express or implied, including but not limited to
# the implied warranties of merchantability, fitness for a particular
# purpose, or non-infringement. 

# Usage of the script
if [[ $# -lt 1 ]]
then
   echo "Usage 1:";
   echo $0" --option=recreate --data_ts=data-tablespace-name --index_ts=index_tablespace_name"
   echo "data_ts and index_ts are optional"
   echo;
   echo "Usage 2:";
   echo $0" --option=info"
   echo "Lists rundates currently in metadata_statistics table"
   echo;
   echo "Usage 3:";
   echo $0" --option=delete --date=date-to-delete-old-data"
   echo "Example: "$0" --option=delete --date=2007-01-01"
   exit 1;
fi

BASE_PATH=$TOP/src/db/scripts/impl_analyzer
#BASE_PATH="/home/db2inst1/wpc_anal/scripts"
cd $BASE_PATH

# Extract the variables
option1=`echo $1 | cut -d "=" -f1`; export option1
value1=`echo $1 | cut -d "=" -f2`; export value1
option2=`echo $2 | cut -d "=" -f1`; export option2
value2=`echo $2 | cut -d "=" -f2`; export value2
option3=`echo $3 | cut -d "=" -f1`; export option3
value3=`echo $3 | cut -d "=" -f2`; export value3

# Validate input parameters
if [ ! $option1 ]; then 
 echo "Options not valid exiting... "; exit 0
fi

if [ ! $value1 ]; then
 echo "Check options " $1; exit 0
elif [ $value1 == "recreate" ] || [ $value1 == "info" ] || [ $value1 == "delete" ]; then
 valid=1
else 
 "Option 1 is not valid" $1 ; exit 0
fi
. ${TOP}/bin/get_params.sh

. ${TOP}/src/db/schema/util/set_db.sh \
  --db_user_name=${_CCD_DB_USER_NAME} \
  --db_user_pwd=${_CCD_DB_USER_PWD} \
  --db_service_name=${_CCD_DB_SERVICE_NAME}

echo "Executing for " $CCD_DB
if [ $CCD_DB == "db2" ]; then

# DB2 Section
cnct_no_term=`echo $CONNECTION | cut -d";" -f1`
echo $cnct_no_term
db2 $cnct_no_term
cd $BASE_PATH/db2

flag=0
case $value1 in
"recreate") 
   echo "Recreating table ..."
   if [ $option2 ] && [ $option2 == "--data_ts" ] && [ $value2 ] ; then
    #echo "Change data ts" $value2
    if [ $option3 ] && [ $option3 == "--index_ts" ] && [ $value3 ]; then
      flag=1
    else 
     sed -e 's/_users_ts_/'$value2'/g' -e 's/_index_ts_/indx/g' create_table_template > metadata_statistics.sql
     db2 "drop table metadata_statistics" > tmp/drop.out
     db2 -tvf metadata_statistics.sql
    fi
   elif [ $option2 ] && [ $option2 == "--index_ts" ] && [ $value2 ] ; then
    #echo "Change index ts opt-2" $value2
    sed -e 's/_users_ts_/users/g' -e 's/_index_ts_/'$value2'/g' create_table_template > metadata_statistics.sql
    db2 "drop table metadata_statistics" > tmp/drop.out
    db2 -tvf metadata_statistics.sql
   else
    sed -e 's/_users_ts_/users/g' -e 's/_index_ts_/indx/g' create_table_template > metadata_statistics.sql
    db2 "drop table metadata_statistics" > tmp/drop.out
    db2 -tvf metadata_statistics.sql
   fi
 #echo "proceed creating table"
;;
"info") 
   echo "Retreiving rundate info... "
   db2 "select rundate available_rundates, count(1) as total_count  \
        from metadata_statistics group by rundate with ur"
;;
"delete")
   if [ $option2 ] && [ $option2 == "--date" ] && [ $value2 ] ; then
    echo "Deleting old statistics before" $value2
    db2 "delete from metadata_statistics \
         where rundate < '"$value2"-00.00.00.000000'"
   else
    echo "provide valid date of 'YYYY-MM-DD' format" $2
   fi
;;
esac

if [ $flag == 1 ]; then
  sed -e 's/_users_ts_/'$value2'/g' -e 's/_index_ts_/'$value3'/g' create_table_template > metadata_statistics.sql
  db2 "drop table metadata_statistics" > tmp/drop.out
  db2 -tvf metadata_statistics.sql
fi

# ORACLE Section
elif [ $CCD_DB == "oracle" ]; then
 echo "its oracle connection" $CONNECTION
cd $BASE_PATH/oracle

flag=0
case $value1 in
"recreate") 
   echo "Recreating table ..."
   if [ $option2 ] && [ $option2 == "--data_ts" ] && [ $value2 ] ; then
    #echo "Change data ts" $value2
    if [ $option3 ] && [ $option3 == "--index_ts" ] && [ $value3 ]; then
      flag=1
    else 
     sed -e 's/_users_ts_/'$value2'/g' -e 's/_index_ts_/indx/g' create_table_template > metadata_statistics.sql
     sqlplus -s $CONNECTION <<EOF > tmp/drop.out
drop table metadata_statistics ;
commit;
EOF
     cat metadata_statistics.sql
     sqlplus -s $CONNECTION @metadata_statistics
    fi
   elif [ $option2 ] && [ $option2 == "--index_ts" ] && [ $value2 ] ; then
    #echo "Change index ts opt-2" $value2
    sed -e 's/_users_ts_/users/g' -e 's/_index_ts_/'$value2'/g' create_table_template > metadata_statistics.sql
    sqlplus -s $CONNECTION <<EOF > tmp/drop.out
drop table metadata_statistics ;
commit;
EOF
    cat metadata_statistics.sql
    sqlplus -s $CONNECTION @metadata_statistics
   else
    sed -e 's/_users_ts_/users/g' -e 's/_index_ts_/indx/g' create_table_template > metadata_statistics.sql
     sqlplus -s $CONNECTION <<EOF > tmp/drop.out
drop table metadata_statistics ;
commit;
EOF
    cat metadata_statistics.sql
    sqlplus -s $CONNECTION @metadata_statistics
   fi
 #echo "proceed creating table"
;;
"info") 
   echo "Retreiving rundate info... "
   sqlplus $CONNECTION <<EOF
   select to_char(rundate,'YYYY-MM-DD-HH24.MI.SS') available_rundates, 
   count(1) as total_count  
   from metadata_statistics group by 
   to_char(rundate,'YYYY-MM-DD-HH24.MI.SS') ; 
EOF
;;
"delete")
   if [ $option2 ] && [ $option2 == "--date" ] && [ $value2 ] ; then
    echo "Deleting old statistics before" $value2
    sqlplus $CONNECTION <<EOF
    delete from metadata_statistics 
    where to_char(rundate,'YYYY-MM-DD-HH24.MI.SS') <= '$value2-00.00.00';
EOF
   else
    echo "provide valid date of 'YYYY-MM-DD' format" $2
   fi
;;
esac

if [ $flag == 1 ]; then
  sed -e 's/_users_ts_/'$value2'/g' -e 's/_index_ts_/'$value3'/g' create_table_template > metadata_statistics.sql
  sqlplus -s $CONNECTION <<EOF > tmp/drop.out
drop table metadata_statistics ;
commit;
EOF
  cat metadata_statistics.sql
  sqlplus -s $CONNECTION @metadata_statistics
fi

# End of specific db check
else
 echo "not a valid db"
fi

exit 0
