PATH=$PATH:/usr/local/sbin

# config vars

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

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

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

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

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

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

# 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}

# 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 lcr domain grp
		uri speed_dial gw pdt subscriber location re_grp trusted
		address missed_calls usr_preferences aliases silo dialog
		dispatcher dialplan}
EXTRA_TABLES=${EXTRA_TABLES:-imc_members imc_rooms cpl sip_trace domainpolicy carrierroute
		carrier_name domain_name carrierfailureroute userblacklist globalblacklist htable purplemap}
PRESENCE_TABLES=${PRESENCE_TABLES:-presentity active_watchers watchers xcap 
		pua rls_presentity rls_watchers}

# SQL definitions
# If you change this definitions here, then you must change them
# in db/schema/entities.xml too.
# FIXME

FOREVER=${FOREVER:-2020-05-28 21:32:15}
DEFAULT_ALIASES_EXPIRES=${DEFAULT_ALIASES_EXPIRES:-${FOREVER}}
DEFAULT_Q=${DEFAULT_Q:-1.0}
DEFAULT_CALLID=${DEFAULT_CALLID:-Default-Call-ID}
DEFAULT_CSEQ=${DEFAULT_CSEQ:-13}
DEFAULT_LOCATION_EXPIRES=${DEFAULT_LOCATION_EXPIRES:-${FOREVER}}


# 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}
EXTRA_MODULES=${EXTRA_MODULES:-imc cpl siptrace domainpolicy carrierroute userblacklist htable purple}

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

# 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 migrate <old_db> <new_db> .....................(migrates DB from 1.2 to 1.3, not implemented yet!)
       $COMMAND presence ......................................(adds the presence related tables)
       $COMMAND extra .........................................(adds the extra tables)

       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.

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 '\E[37;32m'"\033[1mWARNING: $1\033[0m"
	else
		echo "** WARNING: $1"
	fi
}

minfo() {
	if [ -t 1 -a -z "$NOHLPRINT" ] ; then
		echo -e '\E[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 '\E[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 ANSWER
    else
        ANSWER=${value}
    fi
    ANSWER=${ANSWER:0:1}
    ANSWER=${ANSWER/Y/y}
    ANSWER=${ANSWER/N/n}
}