#!/bin/ksh
###############################################################################
#  Script Name : log_lsa
#
#  Overview 
#    
#
###############################################################################

###############################################################################
# function : print out usage info
###############################################################################
print_msg() {
  if [ "$kor_yn" = "yes" ]
  then
    case $1 in 
       ERROR_EXIT) 
         echo "################################################################"
         echo " ERROR : ¿À·ù·Î ÀÎÇØ ½º³À¼¦ º¹Á¦¸¦ Á¾·áÇÕ´Ï´Ù."
         echo "################################################################"
          ;;
       START) 
         echo "################################################################"
         echo "# < ½ÇÇà ¼ø¼­ >                                                #"
         echo "# STEP 1 : º¹Á¦ÇÒ classµéÀ» masterdb¿¡¼­ unload ÇÕ´Ï´Ù.        #"
         echo "# STEP 2 : º¹Á¦ÇÒ classµéÀ» slavedb·Î loadÇÕ´Ï´Ù.              #"
         echo "# STEP 3 : loadÇÑ classµé¿¡ ´ëÇÑ Á¤º¸¸¦ distdb¿¡ ±â·Ï ÇÕ´Ï´Ù.  #"
         echo "################################################################"
          ;;
       DISTPW) 
         echo 
         echo "ERROR: ¹èÆ÷ DBÀÇ password°¡ ¸ÂÁö ¾Ê½À´Ï´Ù."
          ;;
       STEP1) 
         echo
         echo " STEP 1 : º¹Á¦ÇÒ classµéÀ» masterdb¿¡¼­ unload ÇÕ´Ï´Ù."
         echo
          ;;
       STEP2) 
         echo
         echo " STEP 2 : º¹Á¦ÇÒ classµéÀ» slavedb·Î loadÇÕ´Ï´Ù."
         echo
          ;;
       STEP3) 
         echo
         echo " STEP 3 : loadÇÑ classµé¿¡ ´ëÇÑ Á¤º¸¸¦ distdb¿¡ ±â·Ï ÇÕ´Ï´Ù."
         echo
          ;;
       W_MASTER_NO_RESULT) 
         echo 
         echo " ERROR : ¹èÆ÷ DB¿¡¼­ MasterDB Á¤º¸¸¦ Ã£À» ¼ö ¾ø½À´Ï´Ù."
          ;;
       W_SLAVE_NO_RESULT) 
         echo 
         echo " ERROR : ¹èÆ÷ DB¿¡¼­ SlaveDB Á¤º¸¸¦ Ã£À» ¼ö ¾ø½À´Ï´Ù."
          ;;
       E_LSA) 
         echo 
         echo " Log LSA°¡ Á¤È®ÇÏÁö ¾Ê½À´Ï´Ù."
          ;;
       E_FILE_NOT_FOUND) 
         echo 
         echo " ÆÄÀÏÀ» Ã£À» ¼ö ¾ø½À´Ï´Ù. : $2"
          ;;
       E_AGENT) 
         echo 
         echo " ERROR : repl_agent°¡ ÀÌ¹Ì ½ÇÇà ÁßÀÔ´Ï´Ù."
          ;;
       END) 
         echo "################################################################"
         echo " ½º³À¼¦ º¹Á¦°¡ Á¤»ó Á¾·á µÇ¾ú½À´Ï´Ù."
         echo "################################################################"
          ;;
       *)
          ;;
    esac
  else
    case $1 in 
       ERROR_EXIT) 
         echo "################################################################"
         echo " ERROR : Be finished Snapshot  Replication  because of ERROR. "
         echo "################################################################"
          ;;
       START) 
         echo "################################################################"
         echo "# < run step >                                                 #"
         echo "# step 1 : unload classes to replicate from masterdb.          #"
         echo "# step 2 : load  classes to replicate into slavedb.            #"
         echo "# step 3 : put record information about loaded classes on distdb#"
         echo "################################################################"
          ;;
       DISTPW) 
         echo
         echo " ERROR: dist_db_password is not correct!! "
          ;;
       STEP1) 
         echo
         echo " STEP 1 : unload classes to replicate from masterdb. "
         echo
          ;;
       STEP2) 
         echo
         echo " STEP 2 : load  classes to replicate into slavedb."
         echo
          ;;
       STEP3) 
         echo
         echo " STEP 3: put record information about loaded classes on distdb."
         echo
          ;;
       W_MASTER_NO_RESULT) 
         echo 
         echo " ERROR : MasterDB information is not found in the DistDB. "
          ;;
       W_SLAVE_NO_RESULT) 
         echo 
         echo " ERROR : SlaveDB  information is not found in the DistDB. "
          ;;
       E_LSA) 
         echo 
         echo " ERROR : Log LSA is not correct."
          ;;
       E_FILE_NOT_FOUND) 
         echo 
         echo " ERROR : File is not found : $2"
          ;;
       E_AGENT) 
         echo 
         echo " ERROR : repl_agent is already executing. "
          ;;
       END) 
         echo "################################################################"
         echo " Be completed Snapshot Replication correctly. "
         echo "################################################################"
          ;;
       *)
          ;;
    esac
  fi
}

###############################################################################
# function : print out usage info
###############################################################################
usage() {
  exec_name=`basename $0`
  echo "usage: $exec_name -m masterdb_name -s slavedb_name -d distdb_name -cf classes_file_name [-p dist_db_password]" 1>&2
  exit 1
}

###############################################################################
# function :  for processing error cases, clear all the temporary files
###############################################################################
error_exit() {
  echo
  print_msg ERROR_EXIT
  rm .result.tmp
  echo
  exit 1
}

###############################################################################
# function :  for processing error cases, clear all loaded classes
###############################################################################
drop_classes() {
  sql=""
  for name in $1
  do
    sql="$sql
         DROP class $name;";
  done
  sql="$sql commit;"
  $rootdir/bin/csql --no-auto-commit $slave_db_name -u "$userid" -p "$passwd" -c "$sql" > .result.tmp
}

###############################################################################
# trying to get root
###############################################################################
RootDir="CUBRID"

for i in $RootDir
do
  seevar="echo \$$i"
  rootdir=`eval $seevar`
  if [ -n "$rootdir" ]
  then
    break;
  fi
done

lang="$CUBRID_LANG"
if [ -z "$lang" ]
then
  lang="en_US"
fi

kor_yn="no"
if [ ! -z "$lang" ] && [ "$lang" = "ko_KR.euckr" ]
then
  kor_yn="yes"
fi


print_msg START

CurDir=`eval pwd`

###############################################################################
# STEP 0 : read the parameters 
###############################################################################
distpw=""

while [ $# -ne 0 ]
do
  case $1 in 
     -m) 
        shift
        master_db_name=$1
        ;;
     -s) 
        shift
        slave_db_name=$1
        ;;
     -d) 
        shift
        dist_db_name=$1
        ;;
     -cf) 
        shift
        classes_file=$1
        ;;
     -p)
        shift
        distpw=$1
        ;;
     *)
        ;;
  esac
  shift
done

#check inputs
if [ -z "$master_db_name" ]
then
  usage
fi
if [ -z "$slave_db_name" ]
then
  usage
fi
if [ -z "$dist_db_name" ]
then
  usage
fi
if [ -z "$classes_file" ]
then
  usage
fi


#check dist_db DBA password
$rootdir/bin/csql $dist_db_name -u DBA -p "$distpw" -c "commit" 2> .result.tmp
result=`eval cat .result.tmp | grep Incorrect`
if [ -n "$result" ]
then
  print_msg DISTPW
  error_exit
fi

#check repl_agent
$rootdir/bin/cubrid service status > .result.tmp
result=`eval cat .result.tmp | grep "repl_agent $dist_db_name"`
if [ -n "$result" ]
then
  print_msg E_AGENT
  error_exit
fi

###############################################################################
# STEP 1 : unload the database
###############################################################################
print_msg STEP1

# select master host ip
sql="select master_ip from master_info where dbname='$master_db_name'"
$rootdir/bin/csql $dist_db_name -u DBA -p "$distpw" -c "$sql" > .result.tmp

# error check
if [ $? -ne 0 ]
then
  cat .result.tmp
  error_exit
fi
result=`eval cat .result.tmp | egrep "no results"`
if [ -n "$result" ]
then
  print_msg W_MASTER_NO_RESULT "$master_db_name"
  error_exit
fi

# get host ip
count=1
while read dum1
do
  if [ $count -gt 5 ]
  then
    hostname=`echo "$dum1" | awk '{print $1}'`
    hostname=`eval expr $hostname`
    break;
  fi
  (( count=count+1 ))
done < .result.tmp

# unload classes
$rootdir/bin/cubrid unloaddb -v -C -i $classes_file --input-class-only "$master_db_name"@"$hostname" > .result.tmp
# error check
if [ $? -ne 0 ]
then
  cat .result.tmp
  error_exit
fi

###############################################################################
# STEP 2 : load the database
###############################################################################
print_msg STEP2

# select userid, password of slavedb
sql="select userid, passwd from slave_info where dbname='$slave_db_name'"
$rootdir/bin/csql $dist_db_name -u DBA -p "$distpw" -c "$sql" > .result.tmp
# error check
if [ $? -ne 0 ]
then
  cat .result.tmp
  error_exit
fi
result=`eval cat .result.tmp | egrep "no results"`
if [ -n "$result" ]
then
  print_msg W_SLAVE_NO_RESULT "$slave_db_name"
  error_exit
fi

# get userid, password
count=1
while read dum1 dum2
do
  if [ $count -gt 5 ]
  then
    userid=$dum1
    userid=`eval expr $userid`
    passwd=$dum2
    passwd=`eval expr $passwd`
    break;
  fi
  (( count=count+1 ))
done < .result.tmp

# master objects file : delete class hash info
# result : objects_file
objects_file=".object.txt"
echo "" > $objects_file
while read prename name
do
  if [ "$prename" != "%id" ]
  then
    echo "$prename $name" >> $objects_file
  fi
done < "$master_db_name"_objects

# slavedb : load objects
$rootdir/bin/cubrid loaddb --CS-hidden -v -u $userid -p $passwd -s "$master_db_name"_schema -d $objects_file $slave_db_name > .result.tmp
# error check
if [ $? -ne 0 ]
then
  cat .result.tmp
  error_exit
fi
# remove temp objects_file
rm $objects_file

###############################################################################
# STEP 1 : load the database
###############################################################################
print_msg STEP3

# total line count of unload log file
unload_log_file="unload.log"
# file check
if [ ! -f "$unload_log_file" ]
then
  print_msg E_FILE_NOT_FOUND "$unload_log_file"
  error_exit
fi
line_count=`wc -l $unload_log_file | awk '{print $1}'`

# get name list of unloaded classes and Log LSA
count=0
pageid=-1
while read name dum2 pageid offset
do
  if [ "$name" = "LSA" ]
  then
    pageid=$pageid
    offset=$offset
    break;
  fi 
  (( count=count+1 ))
done < $unload_log_file

class_names=""
while read name
do
    class_names="$class_names $name"
done < $classes_file

# check Log LSA
if [ "$pageid" -lt 0 ]
then
  print_msg E_LSA "$pageid" "$offset"
  drop_classes $class_names
  error_exit
fi

# create query : insert repl_group instance
sql=""
for name in $class_names
do
  sql="$sql
       INSERT INTO repl_group(master_dbid, slave_dbid, class_name, 
                              start_pageid, start_offset) 
       VALUES ( (select dbid FROM master_info WHERE dbname = '$master_db_name'),
            (select dbid FROM slave_info WHERE dbname = '$slave_db_name'),
             '$name',
             $pageid, 
             $offset);"
done
sql="$sql COMMIT;"

# execute query : insert repl_group instance
$rootdir/bin/csql --no-auto-commit $dist_db_name -u DBA -p "$distpw" -c "$sql" > .result.tmp
# error check
if [ $? -ne 0 ]
then
  cat .result.tmp
  drop_classes "$class_names"
  error_exit
fi

print_msg END
