#!/bin/bash

# =========================================
# initialize variables
# =========================================

# file location variables
PCW_BENCHMARK_HOME_DIRECTORY=~/PostgreSQLBench
PCW_DATABASE_NAME="PostgreSQL 6.5.3"
PCW_DB_DATA_LOCATION=
PCW_DB_LOG_LOCATION=

# command name variables
PCW_MAINFILE=$PCW_BENCHMARK_HOME_DIRECTORY/dbsetup
PCW_SUBROUTINESFILE=$PCW_BENCHMARK_HOME_DIRECTORY/subroutines
PCW_RUNSQL_ADMIN=$PCW_BENCHMARK_HOME_DIRECTORY/bin/runsql
PCW_RUNSQL_PCWEEK=$PCW_BENCHMARK_HOME_DIRECTORY/bin/runsql_pcweek
PCW_START_DB=$PCW_BENCHMARK_HOME_DIRECTORY/bin/start_db
PCW_STOP_DB=$PCW_BENCHMARK_HOME_DIRECTORY/bin/stop_db
PCW_FILE_COMPARE=cmp

# =========================================
# subroutines
# =========================================

FILES_MISSING_ERROR ()
{
echo
echo The benchmark files are not located in $PCW_BENCHMARK_HOME_DIRECTORY.
echo Please place them there and run this file again.
echo 
exit
}

CHECK_BENCHMARK ()
{
echo 
echo ====================================================
echo Checking if benchmark files are in the right place 
echo ====================================================
echo 

echo 1. Check that benchmark files exist
if [ ! -d "$PCW_BENCHMARK_HOME_DIRECTORY" ]; then FILES_MISSING_ERROR; fi;
if [ ! -f "$PCW_MAINFILE" ]; then FILES_MISSING_ERROR; fi;
if [ ! -f "$PCW_SUBROUTINESFILE" ]; then FILES_MISSING_ERROR; fi;
echo OK.
}

STARTMESSAGE ()
{
echo 
echo "Running PC Week database benchmark setup..."
echo "  version for $PCW_DATABASE_NAME"
echo 
echo Current date and time:
date
}

ENDMESSAGE ()
{
echo 
echo Current date and time:
date
echo 
echo Benchmark setup completed.
echo
}

CONFIG_DB()
{
echo 
echo =========================================
echo B. Configure Database Server
echo =========================================
echo 

cd "B - Configure Database Server"

echo Press any key to configure the benchmark database...
echo 
read

echo 1. Configuring database
echo

echo Done.

$PCW_START_DB
# leave PostgreSQL running for next step

cd $PCW_BENCHMARK_HOME_DIRECTORY
}

CREATE_DB ()
{
echo 
echo =========================================
echo C. Create Benchmark Database
echo =========================================
echo 

cd "C - Create Benchmark Database"

echo "Create 'pcweek' database"
echo

echo "1. Create 'pcweek' database"
$PCW_RUNSQL_ADMIN c1-create_db

echo "2. Create 'pcweek' database user, password is 'pcweek'"
$PCW_RUNSQL_ADMIN c2-create_login

echo "3. Create database schema (tables and views)"
$PCW_RUNSQL_PCWEEK c3-create_tables

echo 4. Create stored procedures for benchmark workload
echo
echo Create stored procedures for OLTP write transactions
$PCW_RUNSQL_PCWEEK c4-create_oltp_write_sprocs
echo 

echo 5. Save list of server settings
$PCW_RUNSQL_PCWEEK c5-show_parameters

echo 
echo Stop and restart database
echo
$PCW_STOP_DB
$PCW_START_DB

cd $PCW_BENCHMARK_HOME_DIRECTORY
}

LOAD_DB ()
{
echo 
echo =========================================
echo D. Load Database
echo =========================================
echo 

cd "D - Load Database"

echo Starting full data load
echo 
echo Current date and time:
date

echo
echo 1. Bulk load data into tables
$PCW_RUNSQL_PCWEEK d1-load_data

echo 
echo Finished full data load
echo 
echo Current date and time:
date

echo 
echo Stop and restart database
echo
$PCW_STOP_DB
$PCW_START_DB

cd $PCW_BENCHMARK_HOME_DIRECTORY
}

INDEX_DB ()
{
echo 
echo =========================================
echo E. Index Database
echo =========================================
echo 

cd "E - Index Database"

echo Starting full indexing run
echo 
echo Current date and time:
date
echo 

echo 1. Build indices and add constraints
$PCW_RUNSQL_PCWEEK e1-create_indices

echo 
echo Finished full indexing run
echo 
echo Current date and time:
date

echo 
echo Stop and restart database
echo
$PCW_STOP_DB
$PCW_START_DB

cd $PCW_BENCHMARK_HOME_DIRECTORY
}

PRECHECK_DB ()
{
echo 
echo =========================================
echo F. Run Pre-Benchmark Correctness Checks
echo =========================================
echo 

cd "F - Run Pre-Benchmark Correctness Checks"

echo 1. Populate consistency tables
echo Populating consistency tables
$PCW_RUNSQL_PCWEEK f1-populate_consistency_tables

echo
echo 2. Check consistency tables
echo Checking contents of consistency tables
$PCW_RUNSQL_PCWEEK f2-check_consistency_tables
$PCW_FILE_COMPARE f2-check_consistency_tables-output.txt f2-check_consistency_tables-output-MASTER.txt

echo
echo 3. Check table sizes
echo Checking table sizes
$PCW_RUNSQL_PCWEEK f3-check_table_sizes
$PCW_FILE_COMPARE f3-check_table_sizes-output.txt f3-check_table_sizes-output-MASTER.txt

echo
echo 4. Check results of OLTP read transactions
echo Checking OLTP read transactions
$PCW_RUNSQL_PCWEEK f4-check_oltp_read_workload
$PCW_FILE_COMPARE f4-check_oltp_read_workload-output.txt f4-check_oltp_read_workload-output-MASTER.txt

echo
echo 5. Check results of OLTP write transactions
echo Checking and then undoing OLTP write transactions
$PCW_RUNSQL_PCWEEK f5-check_oltp_write_workload
$PCW_FILE_COMPARE f5-check_oltp_write_workload-output.txt f5-check_oltp_write_workload-output-MASTER.txt

echo
echo 6. Check results of DSS transactions
echo Checking DSS transactions
$PCW_RUNSQL_PCWEEK f6-check_dss_workload
$PCW_FILE_COMPARE f6-check_dss_workload-output.txt f6-check_dss_workload-output-MASTER.txt

echo 
echo Stop and restart database
echo
$STOP_DB
$START_DB

cd $PCW_BENCHMARK_HOME_DIRECTORY
}

RUN_MIXES ()
{
echo 
echo =========================================
echo G. Run Transaction Mixes
echo =========================================
echo 

cd "G - Run Transaction Mixes"

echo 1. Update statistics for entire database
$PCW_RUNSQL_PCWEEK update_statistics_test

echo
echo Ready to run Benchmark Factory mixes.
echo

cd $PCW_BENCHMARK_HOME_DIRECTORY
}

POSTCHECK_DB ()
{
echo 
echo =========================================
echo H. Run Post-Benchmark Correctness Checks
echo =========================================
echo 

cd "H - Run Post-Benchmark Correctness Checks"

echo Make sure all oltp_write_undo routines have been run, otherwise
echo consistency checks will fail.
echo
echo Press ENTER to continue...
read

echo 1. Check consistency tables
echo Checking contents of consistency tables
$PCW_RUNSQL_PCWEEK h1-check_consistency_tables
$PCW_FILE_COMPARE h1-check_consistency_tables-output.txt h1-check_consistency_tables-output-MASTER.txt

echo
echo 2. Check table sizes
echo Checking table sizes
$PCW_RUNSQL_PCWEEK h2-check_table_sizes
$PCW_FILE_COMPARE h2-check_table_sizes-output.txt h2-check_table_sizes-output-MASTER.txt

echo
echo 3. Check results of OLTP read transactions
echo Checking OLTP read transactions
$PCW_RUNSQL_PCWEEK h3-check_oltp_read_workload
$PCW_FILE_COMPARE h3-check_oltp_read_workload-output.txt h3-check_oltp_read_workload-output-MASTER.txt

echo
echo 4. Check results of OLTP write transactions
echo Checking and then undoing OLTP write transactions
$PCW_RUNSQL_PCWEEK h4-check_oltp_write_workload
$PCW_FILE_COMPARE h4-check_oltp_write_workload-output.txt h4-check_oltp_write_workload-output-MASTER.txt

echo
echo 5. Check results of DSS transactions
echo Checking DSS transactions
$PCW_RUNSQL_PCWEEK h5-check_dss_workload
$PCW_FILE_COMPARE h5-check_dss_workload-output.txt h5-check_dss_workload-output-MASTER.txt

echo 
echo Stop and restart database
echo
$STOP_DB
$START_DB

cd $PCW_BENCHMARK_HOME_DIRECTORY
}

DELETE_DB ()
{
echo 
echo =========================================
echo I. Delete Objects
echo =========================================
echo 

cd "I - Delete Objects"

./delete_entire_db

cd $PCW_BENCHMARK_HOME_DIRECTORY
}

# =========================================
# end
# =========================================

