PATH=$PATH:

# config vars

# name of the database to be used by Kamailio
DBNAME=${DBNAME:-kamailio}

# address of database server
DBHOST=${DBHOST:-localhost}

# user with full privileges over DBNAME database
DBRWUSER=${DBRWUSER:-kamailio}

# password user with full privileges over DBNAME database
DBRWPW=${DBRWPW:-kamailiorw}

# read-only user
DBROUSER=${DBROUSER:-kamailioro}

# password for read-only user
DBROPW=${DBROPW:-kamailioro}

# user name column
USERCOL=${USERCOL:-username}

# Describe what additional tables to install. Valid values for the variables
# below are yes/no/ask. With ask it will interactively ask the user for the
# answer, while yes/no allow for automated, unassisted installs.

INSTALL_EXTRA_TABLES=${INSTALL_EXTRA_TABLES:-ask}
INSTALL_PRESENCE_TABLES=${INSTALL_PRESENCE_TABLES:-ask}
INSTALL_DBUID_TABLES=${INSTALL_DBUID_TABLES:-ask}

# Used by dbtext and db_berkeley to define tables to be created, used by
# postgres to do the grants
STANDARD_TABLES=${STANDARD_TABLES:-'version acc dbaliases domain domain_attrs
		grp uri speed_dial lcr_gw lcr_rule lcr_rule_target pdt subscriber
		location location_attrs re_grp trusted address missed_calls
		usr_preferences aliases silo dialog dialog_vars dispatcher dialplan
		acc_cdrs topos_d topos_t'}
EXTRA_TABLES=${EXTRA_TABLES:-'imc_members imc_rooms cpl sip_trace domainpolicy
		carrierroute carrier_name domain_name carrierfailureroute userblocklist
		globalblocklist htable purplemap uacreg pl_pipes mtree mtrees
		sca_subscriptions mohqcalls mohqueues rtpproxy rtpengine
		dr_gateways dr_rules dr_gw_lists dr_groups secfilter'}
PRESENCE_TABLES=${PRESENCE_TABLES:-'presentity active_watchers watchers xcap
		pua rls_presentity rls_watchers'}
DBUID_TABLES=${UID_TABLES:-'uid_credentials uid_domain uid_domain_attrs
		uid_global_attrs uid_uri uid_uri_attrs uid_user_attrs'}

# SQL definitions
# If you change this definitions here, then you must change them
# in ../../lib/srdb1/schema/entities.xml too. They are used in this
# script and needed to be the same as in the database definitions.
# FIXME

FOREVER=${FOREVER:-2030-05-28 21:32:15}

# default location for config files
DEFAULT_CFG_DIR=/etc/kamailio/

# default location for data files
DEFAULT_DATA_DIR=/usr/local/share/kamailio/

# Needed programs
MD5=${MD5:-md5sum}
AWK=${AWK:-awk}
GREP=${GREP:-grep}
SED=${SED:-sed}

# define what modules should be installed
STANDARD_MODULES=${STANDARD_MODULES:-'standard acc lcr domain group
		permissions registrar usrloc msilo alias_db uri_db speeddial
		avpops auth_db pdt dialog dispatcher dialplan topos'}

PRESENCE_MODULES=${PRESENCE_MODULES:-'presence rls'}

EXTRA_MODULES=${EXTRA_MODULES:-'imc cpl siptrace domainpolicy carrierroute
		drouting userblocklist htable purple uac pipelimit mtree sca mohqueue
		rtpproxy rtpengine secfilter'}

DBUID_MODULES=${UID_MODULES:-'uid_auth_db uid_avp_db uid_domain uid_gflags
		uid_uri_db'}

############################################################

# common functions

usage() {
COMMAND=`basename $0`
cat <<EOF
$0 $VERSION

usage: $COMMAND create <db name or db_path, optional> ...(creates a new database)
       $COMMAND drop <db name or db_path, optional> .....(!entirely deletes tables!)
       $COMMAND reinit <db name or db_path, optional> ...(!entirely deletes and than re-creates tables!)
       $COMMAND backup <file> ...........................(dumps current database to file)
       $COMMAND restore <file> ..........................(restores tables from a file)
       $COMMAND copy <new_db> ...........................(creates a new db from an existing one)
       $COMMAND presence ................................(adds the presence related tables)
       $COMMAND extra ...................................(adds the extra tables)
       $COMMAND dbuid ...................................(adds the uid tables)
       $COMMAND dbonly ..................................(creates empty database)
       $COMMAND grant ...................................(grant privileges to database)
       $COMMAND revoke ..................................(revoke privileges to database)
       $COMMAND add-tables <gid> ........................(creates only tables groupped in gid)

       if you want to manipulate database as other database user than
       root, want to change database name from default value "$DBNAME",
       or want to use other values for users and password, edit the
       "config vars" section of the command $COMMAND.

       $COMMAND pframework create .......................(creates a sample provisioning framework file)
EOF
} #usage




# read realm
prompt_realm()
{
	printf "Domain (realm) for the default user 'admin': "
	read SIP_DOMAIN
	echo
}


# calculate credentials for admin
credentials()
{
	HA1=`echo -n "admin:$SIP_DOMAIN:$DBRWPW" | $MD5 | $AWK '{ print $1 }'`
	if [ $? -ne 0 ] ; then
		merr "HA1 calculation failed"
		exit 1
	fi
	HA1B=`echo -n "admin@$SIP_DOMAIN:$SIP_DOMAIN:$DBRWPW" | $MD5 | $AWK '{ print $1 }'`
	if [ $? -ne 0 ] ; then
		merr "HA1B calculation failed"
		exit 1
	fi

	#PHPLIB_ID of users should be difficulty to guess for security reasons
	NOW=`date`;
	PHPLIB_ID=`echo -n "$RANDOM:$NOW:$SIP_DOMAIN" | $MD5 | $AWK '{ print $1 }'`
	if [ $? -ne 0 ] ; then
		merr "PHPLIB_ID calculation failed"
		exit 1
	fi
}

# FIXME use the definition from kamctl.base

mdbg() {
	if [ "0$VERBOSE" -ne 0 ] ; then
		if [ -t 1 -a -z "$NOHLPRINT" ] ; then
			echo -e "\033[1m$1\033[0m"
		else
			echo "$1"
		fi
	fi
}

mwarn() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e '\033[37;32m'"\033[1mWARNING: $1\033[0m"
	else
		echo "** WARNING: $1"
	fi
}

minfo() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e '\033[37;33m'"\033[1mINFO: $1\033[0m"
	else
		echo "** INFO: $1"
	fi
}

mecho() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e "\033[1m$1\033[0m"
	else
		echo "$1"
	fi
}

merr() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e '\033[37;31m'"\033[1mERROR: $1\033[0m"
	else
		echo "** ERROR: $1"
	fi
}

# Get a y/n value from a variable. If the variable contains the keyword
# `ask', then interactively ask the user for an answer.
#
# Arguments:
#  $1 - variable holding yes/no/ask
#  $2 - question to print if value of variable is ask
#
# Return:
#  On return $ANSWER will be available with y/n
#
get_answer ()
{
	value=$1
	question=$2
	if [ "${value}" = "ask" ]; then
		echo -n "$question"
		read value
	fi
	ANSWER=$(echo ${value} | grep -o -e '[YyNn]' | sed -e 's/Y/y/' -e 's/N/n/')
}
