#!/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
#

if [ "${DBT2DATADIR}" = "" ]; then
	echo "ERROR: DBT2DATADIR must be set"
	exit 1
fi

LOGFILE="dbt2.log"
OUTDIR="."
HOSTNAME=$(hostname)

while getopts "fo:p:" OPT; do
	case ${OPT} in
	f)
		if [ -f "${DBT2DATADIR}/${HOSTNAME}.pid" ]; then
			rm "${DBT2DATADIR}/${HOSTNAME}.pid"
		fi
		;;
	o)
		OUTDIR=$OPTARG
		;;
	p)
		DEFAULTS_FILE=${OPTARG}
		DEFAULTS_FILE_ARG="--defaults-file=${DEFAULTS_FILE}"
		;;
	\?)
		exit 1
		;;
	esac
done

if [ -f "${DBT2DATADIR}/${HOSTNAME}.pid" ]; then
	echo "MySQL pid file '${DBT2DATADIR}/${HOSTNAME}.pid' already exists."
	exit 1
fi
# shellcheck disable=SC2086 # DEFAULTS_FILE_ARG is intentionally word split
nohup mysqld_safe ${DEFAULTS_FILE_ARG} \
		--log-error="${OUTDIR}/${LOGFILE}" > /dev/null 2>&1 &
sleep 10

exit 0
