#!/bin/sh
#
#	$NetBSD: rtconfig,v 1.1.1.1 2002/09/19 21:40:45 atatat Exp $
#

nonemessage="Enter \"none\" for an empty reponse or hit enter for the default."
query()
{
	_var="$1"
	_default="$2"
	_value=""

	[ -n "$nonemessage" ] && echo "$nonemessage"
	nonemessage=""
	while [ -z "$_value" ]; do
		echo -n "Enter $_var [$_default]: "
		read _value
		if [ -z "$_value" ]; then
			_value="$_default"
			break
		elif [ "$_value" = "none" ]; then
			_value=""
			break
		fi
	done
	
	eval $_var="$_value"
}

PERL='/usr/pkg/bin/perl'
RT_PATH='/usr/pkg/rt'
RT_ETC_PATH="${RT_PATH}/etc"
RT_LOG_PATH='/var/pkg/rt/log'
RT_DATA_PATH='/var/pkg/rt/data'
DB_TYPE='mysql'
DB_HOME='/usr/pkg'
query DB_HOST 'localhost'
DB_PORT=''
DB_RT_USER='rt'
DB_RT_PASS='changemeplease'
DB_DATABASE='rt2'
query DB_DBA 'root'
query DB_DBA_PASSWORD ''

if [ ! -f "${RT_DATA_PATH}/installed" ]; then
	upgrading=false
else
	ODB_TYPE=`cat "${RT_DATA_PATH}/installed"`
	if [ "$DB_TYPE" = "$ODB_TYPE" ]; then
		upgrading=true
	else
		cat <<EOF
WARNING: You cannot upgrade an RT installation from $ODB_TYPE to
WARNING: $DB_TYPE simply by installing a new pkg.  Your data will
WARNING: need to be manually dumped and reloading from the database.
EOF
		exit 1
	fi
fi

query WEB_USER 'nobody'
query WEB_GROUP 'nobody'

MASON_DATA_PATH='/var/pkg/rt/data'
MASON_SESSION_PATH='/var/pkg/rt/sessiondata'

# install -> initialize.${DB_TYPE} -> createdb
createdb()
{
	$upgrading && return 0

        ${PERL} ${RT_PATH}/etc/initdb "${DB_TYPE}" "${DB_HOME}" \
		"${DB_HOST}" "${DB_PORT}" \
		"${DB_DBA}" "${DB_DATABASE}" \
		create
}

# install -> initialize.${DB_TYPE} -> acls
acls()
{
	$upgrading && return 0

	echo "Note: make sure this is the same as listed in your config.pm"
	query DB_RT_PASS "${DB_RT_PASS}"
	sed < "${RT_ETC_PATH}/acl.${DB_TYPE}" > "${RT_ETC_PATH}/acl" \
		-e "s/identified by [^ ]*$/identified by ${DB_RT_PASS}/" \
		-e "s/with password '[^']*'/with password '${DB_RT_PASS}'/" \
		-e "s/IDENTIFIED BY '[^']*'/IDENTIFIED BY '${DB_RT_PASS}'/"
	${RT_ETC_PATH}/initacls.${DB_TYPE} "${DB_HOME}" \
		"${DB_HOST}" "${DB_PORT}" "${DB_DBA}" \
		"${DB_DBA_PASSWORD}" "${DB_DATABASE}" \
		"${RT_ETC_PATH}/acl"
	rm "${RT_ETC_PATH}/acl"
}

# install -> initialize.mysql -> initdb.rtuser
initdb_rtuser()
{
	$upgrading && return 0

	${PERL} ${RT_ETC_PATH}/initdb "${DB_TYPE}" "${DB_HOME}" \
		"${DB_HOST}" "${DB_PORT}" \
		"${DB_RT_USER}" "${DB_DATABASE}" \
		insert
}

# install -> initialize.Pg -> initdb.dba
initdb_dba()
{
	$upgrading && return 0

	${PERL} ${RT_ETC_PATH}/initdb "${DB_TYPE}" "${DB_HOME}" \
		"${DB_HOST}" "${DB_PORT}" \
		"${DB_DBA}" "${DB_DATABASE}" \
		insert
}

# install -> upgrade -> upgrade-noclobber -> nondestruct -> fixperms
fixperms()
{
	chmod 770		${RT_LOG_PATH} \
				${MASON_DATA_PATH} \
				${MASON_SESSION_PATH}
        chown -R ${WEB_USER}	${RT_LOG_PATH} \
				${MASON_DATA_PATH} \
				${MASON_SESSION_PATH}
        chgrp -R ${WEB_GROUP}	${RT_LOG_PATH} \
				${MASON_DATA_PATH} \
				${MASON_SESSION_PATH}
}

# install -> insert
insert()
{
	$upgrading && return 0

	${PERL} ${RT_ETC_PATH}/insertdata
}

# rt upgrade "instructions"
rtupgrade()
{
	$upgrading || return 0

	cat <<EOF

Congratulations. RT has been upgraded. You should now check-over
${RT_ETC_PATH}/config.pm for any necessary site customization.
EOF
}

# 
rtinstall()
{
	$upgrading && return 0

	cat <<EOF

Congratulations. RT has been installed.  From here on in, you
should refer to the users guide (aka README.rt).
EOF

	echo "$DB_TYPE" > "${RT_DATA_PATH}/installed"
}

cd "${RT_PATH}"

case "${DB_TYPE}" in
mysql)
	createdb					# not on upgrade
	acls						# not on upgrade
	initdb_rtuser					# not on upgrade
	fixperms
	insert						# not on upgrade
	;;

Pg)
	createdb					# not on upgrade
	initdb_dba					# not on upgrade
	acls						# not on upgrade
	fixperms
	insert						# not on upgrade
	;;

*)
	echo "what database type is ${DB_TYPE}?" >&2
	exit 1
	;;
esac

rtupgrade
rtinstall
