#!/bin/bash

# This script generate NUM_USERS in OpenSIPS and Asterisk. It also creates 
# a CSV file containing these users to be used by SIPp.
#
# For Asterisk, it writes the users in /etc/asterisk/sipp_users.conf.
# You must add the following lines to /etc/asterisk/sip.conf:
#    ---------------------------
#    [sipp](!)
#    type=friend
#    host=dynamic
#    context=desde-sipp
#    #include sipp_users.conf
#    ---------------------------


DOMAIN="XXXXXXX"
NUM_USERS=5000
CSV_FILE="./csv_file"

# Users file for Asterisk
AST_SIPP_CONF="/etc/asterisk/sipp_users.conf"

# DB for OpenSIPS
OSIPS_DB_HOST=localhost
OSIPS_DB_NAME=opensips
OSIPS_DB_USER=XXXXXX
OSIPS_DB_PASSWD=XXXXXX


# Clean the files if they already exist
> $CSV_FILE
echo "SEQUENTIAL" > $CSV_FILE
> $AST_SIPP_CONF


### OpenSIPS
# Delete current users with domain = DOMAIN
mysql -u $OSIPS_DB_USER -p$OSIPS_DB_PASSWD -h $OSIPS_DB_HOST $OSIPS_DB_NAME -e "DELETE FROM subscriber WHERE domain='$DOMAIN'"

for ((i=1; i<=$NUM_USERS; i++)) ; do

	USER=user$i
	PASSWD=$(($RANDOM%1000000))
	
	### SIPp
	# Write in CSV_FILE
	echo "$USER;$DOMAIN;[authentication username=$USER password=$PASSWD]" >> $CSV_FILE
	
	### Asterisk
	# Write in AST_SIPP_CONF
	echo -e "[$USER](sipp)\nsecret=$PASSWD\n" >> $AST_SIPP_CONF
	
	### OpenSIPs
	# Add the user via console
	opensipsctl add ${USER}@${DOMAIN} $PASSWD
	
done


echo "Done, now you can test OpenSIPS and Asterisk with SIPp scenario"
echo "Remember reloading Asterisk and OpenSIPS"