#!/bin/sh
#
# $Id$
#
# script for adding users to database
#

# local domain
DOMAIN=iptel.org

# SQL config
SQL_DB=csps107
SQL_HOST=dbhost
SQL_USER=csps

# binaries
GENHA1='gen_ha1'
MYSQL='mysql'

# SQL names
USER_COLUMN=user_id
REALM_COLUMN=realm
HA1_COLUMN=HA1
HA1B_COLUMN=HA1B
TABLE=subscriber
PASSWORD_COLUMN=password

if [ $# -ne 3 ] ; then
	echo "usage: $0 <name> <realm> <password>"
	exit 1
fi

HA1=`$GENHA1 $1 $2 $3`
if [ $? -ne 0 ] ; then
	echo "HA1 calculation failed"
	exit 1
fi
HA1B=`$GENHA1 "$1@$DOMAIN" $2 $3`
if [ $? -ne 0 ] ; then
	echo "HA1B calculation failed"
	exit 1
fi
$MYSQL -h $SQL_HOST -u $SQL_USER -p \
	-e "use $SQL_DB; insert into $TABLE ($USER_COLUMN,$REALM_COLUMN,$HA1_COLUMN,$HA1B_COLUMN,$PASSWORD_COLUMN) values ('$1','$2','$HA1','$HA1B','$3');";
if [ $? -ne 0 ] ; then
	echo "introducing a new user to the database failed"
else
	echo "new user added"
fi