#!/bin/bash
# Configuration script that creates the basic site configuration file
#
# Copyright (C) 2006, 2007 Greger Viken Teigre
# This file is part of ser, a free SIP server.
# ser is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version
#  
# ser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# This configuration script copies all the templates from the templates/site
# directory into the local directory. Either to the 'default' sub-directory or
# to a named sub-directory. Specifying a name to the configure script will
# make it use that name as the sub-directory name below $local_prefix (see 
# below).  If you manage several servers, you will have one such sub-directory
# per server.
#
# You can always generate a new config.m4. If local.m4 exists in the target directory,
# local.m4 and any other site/server configs will be left untouched.
# 
# The generated output from this interactive script goes into config.m4
# Thus, the most basic switches are turned on and off there.

# Can be changed to generate local sub-dirs somewhere else...
# Use either absolute or relative path to dir with configure in.
local_prefix="./local"

if [ ! -d $local_prefix ]; then
    mkdir $local_prefix
fi

# Set output file
if [ -z "$1" ]; then
    out="$local_prefix/default/config.m4"
    dir="$local_prefix/default"
    #Name to be used as a define
    name="default"
else
    out="$local_prefix/$1/config.m4"
    dir="$local_prefix/$1"
    name="$1"
fi

echo "This configure script will generate a new local configuration to $out."
echo "======================"
if [ -f "$out" ]; then
  if [ ! "$2" = "new" ]; then
    echo "$out already exists. To confirm overwriting this config, please run: $0 [default|yourconfigname] new"
    exit
  fi
fi

# Now read all the parameters
echo " "

EXTERNAL_IP="`grep 'IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 | awk '{split($1,subs,"=");print(subs[2]);}'`"

if [ "$EXTERNAL_IP" = "" ]; then
    echo "WARNING!!! eth0 does not have a configured IP address in /etc/systconfig/network-scripts/ifcfg-eth0."
    echo "           This may indicate that you don't have a static IP address or are using another interface (or you have a distrobution not using that file...)."
    echo "           Using SER with a DHCP-assigned (non-static) IP address is NOT recommended."
fi
echo "Press ENTER to accept default values."
echo " "
echo "SER Server IP address:"
echo -n "  IP address to listen on ($EXTERNAL_IP): "
read ip
if [ -z "$ip" ]; then
    ip="$EXTERNAL_IP"
fi
echo -n "  Port to listen on (5060): "
read port
if [ -z "$port" ]; then
    port="5060"
fi
echo -n "  Public IP address of server (ENTER if same as listen IP): "
read publicip
echo " "
echo -n "Domain name for this SER installation (i.e. mydomain.com, def. mytestser.org) : "
read seralias
if [ -z "$seralias" ]; then
    seralias="mytestser.org"
fi
echo " "
echo -n "SER installation path (/usr/local, i.e. modules in /usr/local/lib/ser/modules): "
read path
if [ -z "$path" ]; then
    path="/usr/local"
fi

echo " "
echo    "Usage:       dbtype://readwriteuser:password@host/databasename"
echo -n "Database URL (mysql://ser:heslo@localhost/ser): "
read dburl

# Set default usrloc mode to 1 (write-through)
usrlocdbmode="1"
if [ -z "$dburl" ]; then
    dburl="mysql://ser:heslo@localhost/ser"
fi

echo " "
echo "----------------------------------------"
echo "   NOTE!!! Answer 'yes' or 'no' to the questions below, ENTER chooses recommended defaults"
echo "    (i.e. includes the most common features)."
echo "----------------------------------------"

echo " "
echo "You may choose to turn on logging as found in the SER - Getting Started guide."
echo "This logging can be turned on and off in a live environment, and we recommend"
echo "everybody to include the logging except in high-load environments."
echo "(You can also later disable it from your config.m4)"
echo -n "  Turn on logging (yes): "
read logging
if [ -z "$logging" ]; then
    logging="defined"
    echo -n "  How much log possibilities to include (low/medium/high, default high): "
    read level
    if [ -z "$level" ]; then
	lognotice="yes"
	loginfo="yes"
	logdebug="yes"
    else
	if [ "$level" = "low" ]; then
	    lognotice="yes"
	else
	    if [ "$level" = "medium" ]; then
		lognotice="yes"
		loginfo="yes"
	    else
		lognotice="yes"
		loginfo="yes"
		logdebug="yes"
	    fi
	fi

    fi
else 
  # Did not choose default
  logging=""
fi

echo " "
# ADD your new feature package below
all_packages="gettingstarted sems"

echo "You can base your configuration file on one of the feature packages available."
# ADD new feature packages using the directory name of the feature package in the below echo
echo "Currently, the supported packages are ($all_packages)"
echo "   gettingstarted  : A set of basic configurations found in most setups"
echo "   sems            : A configuration file that enables SER as a front-end for SEMS"
# ADD a one-line description of the new feature package
echo " "
echo "You can also choose to include specific components within a feature package (components)."
echo "And finally, you can choose to include no prepared features, just use your own code in $local_prefix."
echo " "
echo "Your choice of feature, choose only one, "
echo -n "   default is gettingstarted (none or $all_packages) ? " 
read features

if [ -z "$features" ]; then
	features="gettingstarted"
fi

if [ "$features" = "sems" ]; then
    echo " "
    echo "sems feature packages is not yet implemented."
    echo "Exiting..."
    exit
fi

if [ "$features" = "gettingstarted" ]; then
	echo " "
	echo "You have chosen the SER - Getting Started feature package.  (See SER - Getting Started document)"
	echo " "
	echo "You now have to choose which features to include (press ENTER for defaults)."
	echo " "
	echo -n "-- basic configuration (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_helloworld="yes"
	        # if helloworld is chosen, we don't want to use a db and the usrloc db mode is 0 (don't use db)
	        usrlocdbmode="0"
	else 
		echo "WARNING!!!! not including the basic configuration is for experts only and should be used to extract"
		echo "            specific features or just an empty config shell."
		read warning
	fi
	feat=""
	echo -n "-- SIP Registrar functions (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_registrar="yes"
	fi
	feat=""
	echo -n "-- authentication using database (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_auth="yes"
		# Write-through db mode is default
		usrlocdbmode="1"
	fi
	feat=""
	echo -n "-- load domain attributes and global flags from database on start-up  (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_global_load="yes"
	fi
	feat=""
	echo -n "-- NAT traversal handling (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_nat="yes"
		echo " "
		echo "You have chosen NAT traversal, do you want to use rtpproxy (iptel.org) or mediaproxy (ag-projects)?"
		echo -n "Your choice (default rtpproxy, mediaproxy): "
		read proxychoice
		if [ -z "$proxychoice" ]; then
			proxychoice="rtpproxy"
		fi
	fi
	feat=""	
	echo -n "-- Basic call features like call forwarding (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_callfwd="yes"
	fi
	feat=""	
	if [ "$gs_auth" != "yes" ]; then
		echo " -- Functionality for PSTN integration is NOT included because authentication was not chosen."
	else
		echo -n "-- Functionality for PSTN integration (ENTER to include) "
		read feat
		if [ -z "$feat" ]; then
			gs_pstn="yes"
		fi
		if [ "$feat" == "yes" ]; then
			if [ "$gs_global_load" == "yes" ]; then
				echo "       NOTE!!! gw_ip in the db table global_attrs must be set to IP address of"
				echo "               the gateway. Use ser_db (ser_ctl) and use type=2, and flags=32"
			else
				echo "       NOTE!!! You have not select loading of global attributes/flags at start-up."
				echo "               Please add '$gw_ip=\"a.b.c.d\";' to your local/default/mainroute.m4"
				echo "               where a.b.c.d is the IP address to your PSTN gateway."
			fi
		fi
	fi
	feat=""	
	echo -n "-- XMLRPC management interface (default no, yes to include) "
	read feat
	if [ "$feat" == "yes" ]; then
		gs_xmlrpc="yes"
	fi
	feat=""
	echo -n "-- Accounting (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_acc="yes"
		echo " "
		echo "You have chosen accounting, do you want to do accounting to database or syslog?"
		echo -n "Your choice (default database, syslog): "
		read accchoice
		if [ -z "$accchoice" ]; then
			accchoice="database"
		fi
	fi
	feat=""	
	echo -n "-- Handling of MESSAGE, including offline storage (default no, yes to include) "
	read feat
	if [ "$feat" == "yes" ]; then
		gs_message="yes"
	fi
	feat=""	
	echo -n "-- Handling of OPTIONS (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_options="yes"
	fi
	feat=""	
	echo -n "-- Outbound calls to the Internet (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_outbound="yes"
	fi
	feat=""	
	echo -n "-- Inbound calls from the Internet (ENTER to include) "
	read feat
	if [ -z "$feat" ]; then
		gs_inbound="yes"
	fi
	feat=""	

fi

# Now all options have been answered, create the template local.m4 file.
if [ ! -f "$dir/local.m4" ]; then
    if [ ! -d "$dir" ]; then
      echo "Creating server-specific directory $dir..."
      mkdir $dir
    fi
    echo "Copying site templates to $dir..."
    cp templates/site/config/* $dir/ > /dev/null 2>&1
else
    echo "$dir/local.m4 already exists. Keeping site configurations..."
    echo "    (only $dir/config.m4 will be created in $dir)"
fi
if [ ! -d "$local_prefix/common" ]; then
    echo "Creating common config directory..."
    mkdir $local_prefix/common
    echo "Copying common configs to $local_prefix/common..."
    cp templates/site/common/* $local_prefix/common/
else
    echo "$local_prefix/common already exists, skipping common config templates..."
fi

def()
{
  echo "define(\`$1', \`$2')dnl"
}

include()
{
  echo "include(\`$1')"
}

cat > $out  << EOF
dnl  Copyright (C) 2006, 2007 Greger Viken Teigre
dnl  This file is part of ser, a free SIP server.
dnl ser is free software; you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation; either version 2 of the License, or
dnl (at your option) any later version
dnl  
dnl ser is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl GNU General Public License for more details.
dnl
dnl You should have received a copy of the GNU General Public License
dnl along with this program; if not, write to the Free Software
dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
dnl
dnl -------------------------------------
dnl This file is the basic configuration for a specific SER server.
dnl Run configure to generate the file automatically.
dnl -------------------------------------
dnl
dnl *******************************
dnl ******* Admin defines *********
dnl *******************************
dnl The below define can be used throughout the whole build system if you
dnl want to keep most of your configuration the same across many servers,
dnl but still want to have differences based on which config is generated.
dnl How to use the define in your site-specific m4 files in local/:
dnl   ifdef(`echo '\`'`CFG_$name',`echo '\`'`
dnl     code_to_include_if_$name
dnl   ')
`def CFG_$name true`
dnl CFGNAME is used by the template system to include the correct site-specific files
`def CFGNAME $name`
dnl The local prefix where local common and local sever configs are found
`def CFGPREFIX $local_prefix`
dnl
dnl Remove 'dnl ' in front of this define to set DEVELOPMENT
dnl to tell that this configuration should include DEVELOPMENT ser.cfg code
dnl You can use this define to turn on and off inclusion of test code in the various
dnl .m4 files like this:
dnl ifdef(\`DEVELOPMENT',\`
dnl   # Code included for testing purposes
dnl ')dnl
dnl define(\`DEVELOPMENT',\`defined')dnl
dnl
dnl If this define is not defined, the separators showing start and stop of the various included m4 files
dnl will not be included in the final ser.cfg
`def "INCLUDE_SEPARATORS" defined`
dnl
dnl *******************************
dnl ******* Global defines ********
dnl *******************************
dnl The public IP address of your SER server
dnl If you use a private address, that is okay if only user agents on your local network
dnl will contact your SER server.
dnl If you map statically a public IP address to your SER server, please define
dnl SER_PUBLIC_IP below to the public IP address statically mapped to SER_LISTEN_IP 
dnl NOTE! If your SER server has two interfaces, one public and one private, you
dnl       must use mhome=yes in your configuration file (add to local-directives.m4)
dnl       and you should add a new listen= directive too.
`def "SER_LISTEN_IP" $ip`
`def "SER_LISTEN_PORT" $port`
dnl Remove 'dnl ' from the below define to allow the private IP address of your SER server
dnl to be accessed through a public IP addressed mapped to your private address on your NAT/firewall
`if [ -z "$publicip" ]; then echo "dnl "; fi``def "SER_PUBLIC_IP" $publicip`
dnl
dnl The alias is used to identity messages for this server. If a user agent
dnl dials (or registers as) sip:user@mytestser.org, that is a local uri.
`def "SER_ALIAS" $seralias`
dnl
`def "SER_INSTALL_PREFIX" $path`
`def "SER_DB_URL" $dburl`
dnl
dnl *******************************
dnl ******* Log defines   *********
dnl *******************************
dnl Major "switches", put dnl in front of a switch to turn off the functionality
`if [ -z "$logging" ]; then echo "dnl "; fi``def "INCLUDE_GETTING_STARTED_LOGGING" yes`
`if [ -z "$lognotice" ]; then echo "dnl "; fi``def "INCLUDE_NOTICE_LOGGING" yes`
`if [ -z "$loginfo" ]; then echo "dnl "; fi``def "INCLUDE_INFO_LOGGING" yes`
`if [ -z "$logdebug" ]; then echo "dnl "; fi``def "INCLUDE_DEBUG_LOGGING" yes`
dnl
dnl *********************************************
dnl ******* Package and feature defines *********
dnl *********************************************
dnl
dnl Each of the features you want to be included must be defined with the two-letter package name prefix, example:
dnl `def "GS_AUTHENTICATION" yes`
dnl See the documentation for each feature package for available features to include. 
dnl
dnl Below each configuration feature within a feature package are included
`if [ -z "$gs_helloworld" ]; then echo "dnl "; fi``def "GS_HELLOWORLD" yes` 
`if [ -z "$gs_auth" ]; then echo "dnl "; fi``def "GS_AUTH" yes` 
`if [ -z "$gs_global_load" ]; then echo "dnl "; fi``def "GS_GLOBAL_LOAD" yes` 
`if [ -z "$gs_nat" ]; then echo "dnl "; fi``def "GS_NAT" yes` 
`if [ -z "$gs_pstn" ]; then echo "dnl "; fi``def "GS_PSTN" yes` 
`if [ -z "$gs_callfwd" ]; then echo "dnl "; fi``def "GS_CALLFWD" yes` 
`if [ -z "$gs_registrar" ]; then echo "dnl "; fi``def "GS_REGISTRAR" yes` 
`if [ -z "$gs_options" ]; then echo "dnl "; fi``def "GS_OPTIONS" yes` 
`if [ -z "$gs_message" ]; then echo "dnl "; fi``def "GS_MESSAGE" yes` 
`if [ -z "$gs_xmlrpc" ]; then echo "dnl "; fi``def "GS_XMLRPC" yes` 
`if [ -z "$gs_acc" ]; then echo "dnl "; fi``def "GS_ACC" yes` 
`if [ -z "$gs_outbound" ]; then echo "dnl "; fi``def "GS_OUTBOUND" yes` 
`if [ -z "$gs_inbound" ]; then echo "dnl "; fi``def "GS_INBOUND" yes` 
dnl
dnl Which accounting to include, DON'T define both at the same time
`if [ "$accchoice" != "database" ]; then echo "dnl "; fi``def "GS_ACC_DB" yes`
`if [ "$accchoice" != "syslog" ]; then echo "dnl "; fi``def "GS_ACC_SYSLOG" yes`
dnl
dnl Which NAT handling system to include, DON'T define both at the same time
`if [ "$proxychoice" != "rtpproxy" ]; then echo "dnl "; fi``def "GS_NAT_RTPPROXY" yes`
`if [ "$proxychoice" != "mediaproxy" ]; then echo "dnl "; fi``def "GS_NAT_MEDIAPROXY" yes`
dnl
dnl ******************************************
dnl ******* Module-related defines   *********
dnl ******************************************
dnl
`def "USRLOC_DB_MODE" $usrlocdbmode`
dnl
dnl Include the local configuration file.
dnl Copy local.m4 from local/common/ to this directory if you want 
dnl to control parameters in local.m4 per server. 
dnl local/common/local.m4 will then be ignored for this server.
`include "local.m4"`
EOF
