#!/bin/sh

functions()
	{
		SELFTMP=$0
		ORIGPWD=`pwd`
		REALPWD=`dirname $0`
		cd "$REALPWD"
		while [ -L "$SELFTMP" ]
			do
				SELFTMP=`readlink "$SELFTMP"`
				cd `dirname "$SELFTMP"`
				REALPWD=`pwd -P`
		done

		cd "$ORIGPWD"

		if [ "$INITIALIZED" != "1" ]
			then
				source $REALPWD/inc/functions.sh
		fi
	}

functions

createtables()
	{
		SERVICETYPE="$1"
		SERVICENAME="$2"
		good ""
		hilite "Set up connection for $SERVICENAME:"
		good ""
		
		DBNAME="merak"
		DBUSER="merak"

		DBHOST="`grep "^socket" /etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf 2>/dev/null |cut -d'=' -f2 | cut -d' ' -f2| tail -n 1 `"
		

		getparam "Database name [${DBNAME}]:"
		[[ $PARAM != "" ]] && DBNAME="$PARAM"
		getparam "Database username [${DBUSER}]:"
		[[ $PARAM != "" ]] && DBUSER="$PARAM"
		getsparam "Database password:"
		DBUSERPW="$SPARAM"
		
		getparam "Database server (socket path or host or leave blank) [${DBHOST}]:"
		[[ $PARAM != "" ]] && DBHOST="$PARAM"		
		
		getconfirm "${GOOD}OK${HILITE}?${NORMAL}" || return 0

		DBNEWUSER="$DBUSER"
		DBNEWNAME="$DBNAME"
		DBNEWUSERPW="$DBUSERPW"
		DBNEWHOST="$DBHOST"

		if ! ( mysqlcall )
			then
				getconfirm "MySQL login failed using supplied credentials, create new database for this user?" || break 1
				
				DBUSER="root"
				DBUSERPW=""
				unset DBNAME
				
				getparam "MySQL administrator username [${DBUSER}]:"
				[[ $PARAM != "" ]] && DBUSER="$PARAM"
				getsparam "MySQL administrator password:"
				DBUSERPW="$SPARAM"
				getparam "MySQL server (socket path or host or leave blank) [${DBHOST}]:"
				[[ $PARAM != "" ]] && DBHOST="$PARAM"
				good ""
				
				if ( getconfirm "Generate a secure password for the new user (uuencode needed)?" )
					then
						DBNEWUSERPW="`head -c 10 /dev/random | uuencode -m - |cut -d'=' -f'1' |tail -n 2 | head -n 1 2>/dev/null`"
						good "Password is: ${DBNEWUSERPW}"
				fi
				
				getconfirm "${GOOD}OK${HILITE}?${NORMAL}" || return 0
				good ""
		
				SQLQUERY="CREATE DATABASE $DBNEWNAME" mysqlcall && good "Database created" || bad "Database creation problem!"
				SQLQUERY="GRANT ALL ON $DBNEWNAME.* TO ${DBNEWUSER}@'localhost' IDENTIFIED BY '$DBNEWUSERPW'" mysqlcall && good "Privileges granted (${DBNEWUSER}@'localhost')" || bad "Unable to grant privileges!"
				SQLQUERY="GRANT ALL ON $DBNEWNAME.* TO ${DBNEWUSER}@'%' IDENTIFIED BY '$DBNEWUSERPW'" mysqlcall && good "Privileges granted (${DBNEWUSER}@'%')" || bad "Unable to grant privileges!"
				SQLQUERY="GRANT ALL ON $DBNEWNAME.* TO ${DBNEWUSER}@'127.0.0.1' IDENTIFIED BY '$DBNEWUSERPW'" mysqlcall && good "Privileges granted (${DBNEWUSER}@'127.0.0.1')" || bad "Unable to grant privileges!"
				SQLQUERY="FLUSH PRIVILEGES" mysqlcall

				getconfirm "Continue with table creation?" || exit 0
		fi

		tool CREATE TABLES $SERVICETYPE "$DBNEWNAME;$DBNEWUSER;$DBNEWUSERPW;$DBNEWHOST;3" && good "Tables created" || bad "Tables already exist or save problem"
		return 0

	}


case `basename $0` in
        createtables-accounts)
                createtables 0 "Accounts storage"
		exit 0
		;;
        createtables-logging)
		createtables 1 "Log storage"
		exit 0
		;;
        createtables-groupware)
		isdaemonalive cal || verybad "GroupWare daemon must be running!"
		createtables 2 "GroupWare storage"
		exit 0
		;;
        createtables-challengeresponse)
		createtables 3 "Challenge-response storage"
		exit 0
		;;
	createmysqldb)
		
		createdbmysql $*
		exit 0
		;;
esac
