#!/bin/sh

#
# This file is released under the terms of the Artistic License.
# Please see the file LICENSE, included in this package, for details.
#
# Copyright The DBT-2 Authors
#

usage() {
	cat << EOF
$(basename "${0}") is the Database Test 2 (DBT-2) database builder.

Usage:
  $(basename "${0}") [OPTION] DBMS [DBNAME]

Options:
  --data=PATH    datagen PATH for generating flat files
  --db-host ADDRESS
                 ADDRESS of database system
  --db-port PORT
                 database listening PORT number
  --db-user USER
                 database USER
  -g             generate flat files
  --parallelism=COUNT
                 Limit datagen to COUNT processes for data loading
  --privileged   run test as a privileged operating system and database user
  --rows-per-commit=ROWS
                 Number of ROWS to load per transactions, default behavior is
                 to load all data in a single transaction
  --tablespaces DIRECTORY
                 build the database using tablspaces, where DIRECTORY contains
                 mountpoints or symlinks per documentation, default is to not
                 use tablespaces
  -V, --version  output version information, then exit
  -w WAREHOUSES, --warehouses=WAREHOUSES
                 number of WAREHOUSES to use in the database, default $WAREHOUSES
  -?, --help     show this help, then exit

MySQL specific options:
  --mysql-socket=FILE
                 specify socket FILE
  --mysql-stored-procedures=PATH
                 load stored procedures found at PATH

PostgreSQL specific options:
  --db-parameters OPTIONS
                 GUC command line OPTIONS to pass to postgresql, when
                 privileged

DBMS options are:
  cockroach  CockroachDB
  mysql      MySQL
  pgsql      PostgreSQL
  sqlite     SQLite

DBNAME is "${DBNAME}" by default.

Database Test 2 (DBT-2) project page: https://github.com/osdldbt/dbt2
EOF
}

BUILDARGS=""
DBNAME="dbt2"
ROWS_PER_COMMIT=0
WAREHOUSES=1

# Custom argument handling for hopefully most portability.
while [ "${#}" -gt 0 ] ; do
	case "${1}" in
	(--data)
		shift
		BUILDARGS="${BUILDARGS} -f ${1}"
		;;
	(--data=?*)
		BUILDARGS="${BUILDARGS} -f ${1#*--data=}"
		;;
	(--db-host)
		shift
		DB_HOSTNAME=${1}
		;;
	(--db-host=?*)
		DB_HOSTNAME=${1#*--db-host=}
		;;
	(--db-parameters)
		shift
		DB_PARAMS="${1}"
		;;
	(--db-parameters=?*)
		DB_PARAMS="${1#*--db-parameters=}"
		;;
	(--db-port)
		shift
		DB_PORT=${1}
		;;
	(--db-port=?*)
		DB_PORT=${1#*--db-port=}
		;;
	(--db-user)
		shift
		DB_USER=${1}
		;;
	(--db-user=?*)
		DB_USER=${1#*--db-user=}
		;;
	(-g)
		BUILDARGS="${BUILDARGS} -g"
		;;
	(--privileged)
		BUILDARGS="${BUILDARGS} -u"
		;;
	(--mysql-socket)
		shift
		BUILDARGS="${BUILDARGS} --socket=${1}"
		;;
	(--mysql-socket=?*)
		BUILDARGS="${BUILDARGS} --socket=${1#*--mysql-socket=}"
		;;
	(--mysql-stored-procedures)
		shift
		BUILDARGS="${BUILDARGS} -F ${1}"
		;;
	(--mysql-stored-procedures=?*)
		BUILDARGS="${BUILDARGS} -F ${1#*--mysql-stored-procedures=}"
		;;
	(--parallelism)
		shift
		BUILDARGS="${BUILDARGS} --parallelism=${1}"
		;;
	(--parallelism=?*)
		BUILDARGS="${BUILDARGS} --parallelism=${1#*--parallelism=}"
		;;
	(--rows-per-commit)
		shift
		export ROWS_PER_COMMIT="${1}"
		;;
	(--rows-per-commit=?*)
		export ROWS_PER_COMMIT=${1#*--rows-per-commit=}
		;;
	(--tablespaces)
		shift
		export DBT2TSDIR="${1}"
		;;
	(--tablespaces=?*)
		export DBT2TSDIR=${1#*--tablespaces=}
		;;
	(-V | --version)
		echo "$(basename "${0}") v0.61.7"
		exit 0
		;;
	(-w | --warehouses)
		shift
		WAREHOUSES=${1}
		;;
	(-w?*)
		WAREHOUSES=${1#*-w}
		;;
	(--warehouses=?*)
		WAREHOUSES=${1#*--warehouses=}
		;;
	(-\? | --help)
		usage
		exit 0
		;;
	(--* | -*)
		echo "$(basename "${0}"): invalid option -- '${1}'"
		echo "try \"$(basename "${0}") --help\" for more information."
		exit 1
		;;
	(*)
		break
		;;
	esac
	shift
done

# Check for required arguments.

if [ $# -eq 0 ]; then
	printf "Specify which DBMS system to test, try \"%s --help\" " \
			"$(basename "${0}")"
	echo "for more information."
	exit 1
fi

DBMS=$1
shift

if [ $# -ne 0 ]; then
	DBNAME=$1
fi

# Construct up BUILDARGS based on command line parameters.

export DBT2DBNAME="${DBNAME}"

if [ "${DBMS}" = "mysql" ]; then
	if [ ! "${DB_HOSTNAME}" = "" ]; then
		BUILDARGS="${BUILDARGS} -h ${DB_HOSTNAME}"
	fi
elif [ "${DBMS}" = "pgsql" ]; then
	if [ ! "${DB_HOSTNAME}" = "" ]; then
		export PGHOST="${DB_HOSTNAME}"
	fi
	if [ ! "${DB_PARAMS}" = "" ]; then
		BUILDARGS="${BUILDARGS} -p \"${DB_PARAMS}\""
	fi
	if [ ! "${DB_PORT}" = "" ]; then
		BUILDARGS="${BUILDARGS} -l ${DB_PORT}"
	fi
	if [ ! "${DB_USER}" = "" ]; then
		export PGUSER="${DB_USER}"
	fi
fi

if [ ! "${DBT2TSDIR}" = "" ]; then
	BUILDARGS="${BUILDARGS} -t"
fi

if [ "${ROWS_PER_COMMIT}" -gt 0 ]; then
	BUILDARGS="${BUILDARGS} --rows-per-commit=${ROWS_PER_COMMIT}"
fi

eval "dbt2 ${DBMS}-build-db -w ${WAREHOUSES} ${BUILDARGS}"
