#!/bin/sh

# Demonstrates an issue with Asterisk not saving entries upon shutdown:

KEY="TESTING123" # a unique key not already in the database
asterisk=asterisk
#asterisk=./live/asterisk

# I don't want to make this part of the standard script:
if [ "$1" = 'clean' ]; then
	$asterisk -rx "database deltree $KEY"
	exit 0
fi

ast_num_entries() {
	asterisk -rx "database show $KEY" | head -n -1 | wc -l
}

sqlite_num_entries() {
	echo "select key, value from astdb where key like '/$KEY%';" | sqlite3 /var/lib/asterisk/astdb.sqlite3 | wc -l
}

echo "Entries before running (If it's 30, this test is meaningless. use '$0 clean'):"
ast_num_entries
for i in `seq 30`; do
	asterisk -rx "database put $KEY ent$i val$i"
done
echo "Done writing."
echo "Entries Asterisk knows of:"
ast_num_entries
echo "Entries in SQLITE:"
sqlite_num_entries
#/etc/init.d/asterisk restart
# With this, it works:
#sleep 5

$asterisk -rx 'core stop now'
echo "Stopped asterisk. Entries in sqlite now:"
sqlite_num_entries
$asterisk -F -U asterisk
sleep 1
echo "Started asterisk. Entries Asterisk knows of now:"
ast_num_entries
