#!/bin/sh
#

# Create a new (empty, no tables) Sherpath Groupware database
# Script created: Xavier Roche, 12/2001

# script path
cd "${0%/*}"

# params
LOCALSRV="localhost"
DBACCESS="localhost"
USER=

## database access
#printf "Enter database server username (root):"
#read DBUSER
#printf "Enter database server password (none):"
#read DBPASS
#if ! test -n "$DBUSER"; then
#	DBUSER="root"
#fi
#if ! test -n "$DBPASS"; then
#	DBPASS=""
#fi
if /usr/bin/mysql </dev/null; then
	#printf "Enter webserver database name (Sherpath):"
	#read DBN
	#printf "Enter webserver database username (sherpath):"
	#read USER
	USER="sherpath"

	MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	LENGTH="8"
	while [ "${n:=1}" -le "$LENGTH" ]; do
	        PASSWD="$PASSWD${MATRIX:$(($RANDOM%${#MATRIX})):1}"
	        let n+=1
	done
	PASS=$PASSWD
	echo $PASS > ~/.sherpath.pw
	/bin/chmod 0400 ~/.sherpath.pw

	#printf "Enter webserver database password (foobar):"
	#read PASS
	if ! test -n "$DBN"; then
		DBN="Sherpath"
	fi
	if ! test -n "$USER"; then
		USER="sherpath"
	fi
	if ! test -n "$PASS"; then
		PASS="foobar"
	fi

	echo -n "Removing/Creating database '$DBN'..."
	#read OK
	OK=y
	if ! test "$OK" = "y"; then
		exit
	fi

	# drop and recreate the database
	echo "drop database $DBN;" | /usr/bin/mysql

	if echo "create database $DBN;" | /usr/bin/mysql; then
		# webserver access & webserver tables access
		if (	echo "delete from user where User='$USER' and Host='$DBACCESS';"; \
			echo "insert into user (User,Password,Host) values ('$USER',password('$PASS'),'$DBACCESS');"; \
			echo "update user set Select_priv='Y',Insert_priv='Y',Update_priv='Y',Delete_priv='Y' where User='$USER';"; \
			echo "update db set Update_priv='Y' where User='$USER';"; \
			echo "update db set Insert_priv='Y' where User='$USER';"; \
			echo "update db set Create_priv='Y' where User='$USER';"; \
			echo "update db set Select_priv='Y' where User='$USER';"; \
			echo "update db set Index_priv='Y' where User='$USER';"; \
			echo "update db set Alter_priv='Y' where User='$USER';"; \
			echo "update db set Drop_priv='Y' where User='$USER';"; \
			echo "flush privileges;"; \
		)	| /usr/bin/mysql mysql; then
			echo "done"
		fi
	fi
	
	CRETABLES=
	if test -f "create_database"; then
		echo -n "Creating all tables and ACLs..."
		#read OK
		OK=y
		if test "$OK" = "y"; then
			CRETABLES=1
		fi
	fi

	#########
	/bin/sed -e "s/__PASSWORD__/$PASS/" ../../config/setup/general.config.php3 > /tmp/sherpath-general.config.php3
	/bin/sed -e "s/__PASSWORD__/$PASS/" ../../config/database/common.config.php3 > /tmp/sherpath-common.config.php3
	/bin/sed -e "s/__PASSWORD__/$PASS/" ../../config/database/common.default.config.php3 > /tmp/sherpath-common.default.config.php3
	/bin/cp -f /tmp/sherpath-general.config.php3 ../../config/setup/general.config.php3
	/bin/cp -f /tmp/sherpath-common.config.php3 ../../config/database/common.config.php3
	/bin/cp -f /tmp/sherpath-common.default.config.php3 ../../config/database/common.default.config.php3
	/bin/rm -f /tmp/sherpath-general.config.php3
	/bin/rm -f /tmp/sherpath-common.config.php3
	/bin/rm -f /tmp/sherpath-common.default.config.php3
	#########

	if test -n "$CRETABLES"; then
		if cat create_database | /usr/bin/mysql $DBN; then
			echo "done"
			echo " "
			echo "MySQL   DB=$DBN   DBUSER=$USER   Password in ~/.sherpath.pw"
		fi
	else
		echo "Go now to the login page to create automatically all database tables!"
	fi
else
	echo "unable to access to the database server.."
fi
