#! /bin/bash
# $PostgreSQL: pgsql/src/test/mb/mbregress.sh,v 1.7 2003/11/29 19:52:14 pgsql Exp $
# CREATED BY Tatsuo Ishii 1998/7/22
# CONTAINS: Set of tests for multibyte supporting extentions for PostgreSQL. 
# LAST UPDATE BY Ayush Parashar 2005/July/7
# ENHANCEMENTS:
#   1. - Include extensive multi-byte regression tests for copy command.
#      - Uses following complete character sets and encoding types for testing: 
#        "big5 gbk uhc gb18030 euc_cn latin8 iso_8859_6 iso_8859_7 euc_kr euc_jp" 
#   2. Include help and usage for various tests.
#   3. Include cleanup function, to remove the log files and database objects that have been created.
# REQUIREMENTS:
#  - Installed PostgreSQL
#  - The location of bin directory of PostgreSQL should be included in "PATH" variable. 

usage()
{
  echo "Usage:"
  echo "$0 --help"
  echo "$0 test"
  echo "$0 clean"
  exit 1 
}

help_mbregress()
{
  echo "mbregress.sh HELP:"
  echo "$0 --help"
  echo "  - Help for mbregress.sh"
  echo "$0 test"
  echo "  - Creates necessary database and tables"
  echo "  - Tests multi-byte character sets & client encoding for create table, insert & select statements"
  echo "  - Tests multi-byte character sets & client encoding for copy command using complete character sets"
  echo "  - If some test fails, output thrown in regression.diffs"
  echo "$0 clean"
  echo "  - Cleans up the log files"
  echo "  - Cleans up the database and tables created during the run of \"./mbregress.sh test\" "
  echo "mbregress.sh REQUIREMENTS:"
  echo "  - PostgreSQL installation (with database cluster already initialized using initdb)"
  echo "  - The location of bin directory of PostgreSQL should be included in \"PATH\" variable."
  exit 0
}

clean()
{
  echo "Cleaning up.."
  \rm ./regression.diffs >> ./results/db_commands.log 2>&1
  \rm ./expected/*.od >> ./results/db_commands.log 2>&1
  \rm ./results/*.od >> ./results/db_commands.log 2>&1 
  \rm ./results/*unicode_utf8 >> ./results/db_commands.log 2>&1
  dropdb unitest >> ./results/db_commands.log 2>&1
  dropdb euc_cn >> ./results/db_commands.log 2>&1
  dropdb euc_jp >> ./results/db_commands.log 2>&1
  dropdb euc_kr >> ./results/db_commands.log 2>&1
  dropdb euc_tw >> ./results/db_commands.log 2>&1
  dropdb mule_internal >> ./results/db_commands.log 2>&1
  dropdb unicode >> ./results/db_commands.log 2>&1
  echo "done."
  exit 0
}

if [ $# -ne 1 ]; then
  usage
else 
  if [ $1 == "--help" ]; then
    help_mbregress
  else
    if [ $1 == "clean" ]; then
      clean
    else
      if [ $1 != "test" ]; then
        usage
      fi
    fi
  fi
fi

if echo '\c' | grep -s c >/dev/null 2>&1
then
  ECHO_N="echo -n"
  ECHO_C=""
else
  ECHO_N="echo"
  ECHO_C='\c'
fi

if [ ! -d results ];then
  mkdir results
fi

\rm ./results/db_commands.log  > /dev/null 2>&1
dropdb unitest >> ./results/db_commands.log 2>&1 
createdb -E UNICODE unitest >> ./results/db_commands.log 2>&1

PSQL="psql -n -e -q"

# STARTING THE MULTI-BYTE TESTS FOR CREATE TABLE, CREATE INDEX, INSERT, SELECT
echo "---------------------------------------------"
echo "Starting multi-byte tests for create table, create index, insert, and select statements"
tests="euc_jp sjis euc_kr euc_cn euc_tw big5 unicode mule_internal"
unset PGCLIENTENCODING
for i in $tests
do
  $ECHO_N "${i} .. " $ECHO_C
  if [ $i = sjis ];then
    PGCLIENTENCODING=SJIS
    export PGCLIENTENCODING
    $PSQL euc_jp < sql/sjis.sql > results/sjis.out 2>&1
    unset PGCLIENTENCODING
  elif [ $i = big5 ];then
    PGCLIENTENCODING=BIG5
    export PGCLIENTENCODING
    $PSQL euc_tw < sql/big5.sql > results/big5.out 2>&1
    unset PGCLIENTENCODING
  else
    dropdb $i >/dev/null 2>&1
    createdb -E `echo $i | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'` $i >/dev/null
    $PSQL $i < sql/${i}.sql > results/${i}.out 2>&1
  fi

  if [ -f expected/${i}-${SYSTEM}.out ]
  then
    EXPECTED="expected/${i}-${SYSTEM}.out"
  else
    EXPECTED="expected/${i}.out"
  fi
  
  if [ `diff ${EXPECTED} results/${i}.out | wc -l` -ne 0 ]
  then
    ( diff -wC3 ${EXPECTED} results/${i}.out; \
    echo "";  \
    echo "----------------------"; \
    echo "" ) >> regression.diffs
    echo failed
  else
    echo ok
  fi
done

# STARTING THE CHARACTER ENCODING/ CLIENT ENCODING TESTS FOR COPY COMMAND
echo "---------------------------------------------"
#copy_command=copy
copy_command="\copy"
echo "Starting multi-byte character encoding tests for copy command" 
copy_tests="big5 gbk uhc gb18030 euc_cn latin8 iso_8859_6 iso_8859_7 euc_kr euc_jp"
CWD=`pwd`
unset PGCLIENTENCODING
for j in $copy_tests
do
  $PSQL unitest -c "create table ${j}_ce_copy(c1 varchar)" >> ./results/db_commands.log 2>&1 
  PGCLIENTENCODING=${j}
  export PGCLIENTENCODING
  $PSQL unitest -c "$copy_command ${j}_ce_copy from '$CWD/sql/${j}.charset' " >> ./results/db_commands.log 2>&1
  unset PGCLIENTENCODING
  $PSQL unitest -c "$copy_command ${j}_ce_copy to '$CWD/results/${j}.unicode_utf8' " >> ./results/db_commands.log 2>&1
  od -d $CWD/expected/${j}.unicode_utf8 > $CWD/expected/${j}.unicode_utf8.od
  od -d $CWD/results/${j}.unicode_utf8 > $CWD/results/${j}.unicode_utf8.od
  if [ `diff $CWD/expected/${j}.unicode_utf8.od $CWD/results/${j}.unicode_utf8.od | wc -l` -ne 0 ]
  then
    ( diff $CWD/expected/${j}.unicode_utf8.od $CWD/results/${j}.unicode_utf8.od; \
    echo ""; \
    echo "----------------------"; \
    echo "" ) >> regression.diffs
    echo ${j} failed
  else
    echo ${j} .. ok 
  fi
done
echo "---------------------------------------------"