#!/bin/sh

# === usb-script-common.inc ===
#!/bin/sh
# $Revision$

touch $0.disabled || exit
exec >$0.log 2>&1

if ! [ -x /opt/kerio/winroute/winroute ]; then
	echo "Kerio Control is not installed."
	echo "Kerio Control is not installed." > /dev/console
	exit 1
fi

set -x
# ================

USERDB=${USERDB:-"/opt/kerio/winroute/UserDB.cfg"}

userDB_clear_Password() {
	xml2 < $USERDB | awk -F "/" '

$3 !~ /list/          { chlist(); listname = ""; print; next }
$4 ~ /@name=/         { chlist(); listname = substr($0, index($0, "=")+1); print; next }
!$4                   { chlist(); listname = ""; print; next }
listname != "LocalUsers" { print; next }

/@name=/              { variable = substr($0, index($0, "=")+1); listitem[variable] = ""; next }
/=/                   { listitem[variable] = substr($0, index($0, "=")+1); next }
! $5                  { chlist() }

END                   { chlist() }

function chlist() {
	if (!length(listitem["Name"])) return;
	if (listitem["Name"] == "Admin") {
		listitem["Password"] = ""
		print listitem["UUID"] > "/tmp/admin-uuid.txt"
	}
	print "/config/list/listitem"
	for(variable in listitem) {
		print "/config/list/listitem/variable"
		print "/config/list/listitem/variable/@name=" variable
		print "/config/list/listitem/variable=" listitem[variable]
	}
	delete listitem
}	' | 2xml | tidy -xml -quiet -indent -wrap 0 -utf8 -output /tmp/UserDB.cfg
	cat /tmp/UserDB.cfg > $USERDB
	rm /tmp/UserDB.cfg
}

userDB_set_AccStatus() {
	xml2 < $USERDB | awk -F "/" '

$3 !~ /list/          { chlist(); listname = ""; print; next }
$4 ~ /@name=/         { chlist(); listname = substr($0, index($0, "=")+1); print; next }
!$4                   { chlist(); listname = ""; print; next }
listname != "UsersData" { print; next }

/@name=/              { variable = substr($0, index($0, "=")+1); listitem[variable] = ""; next }
/=/                   { listitem[variable] = substr($0, index($0, "=")+1); next }
! $5                  { chlist() }

END                   { chlist() }

function chlist() {
	if (!length(listitem["UUID"])) return;
	if (listitem["UUID"] == ENVIRON["ADMIN_UUID"]) listitem["AccStatus"] = "2"
	print "/config/list/listitem"
	for(variable in listitem) {
		print "/config/list/listitem/variable"
		print "/config/list/listitem/variable/@name=" variable
		print "/config/list/listitem/variable=" listitem[variable]
	}
	delete listitem
}	' | 2xml | tidy -xml -quiet -indent -wrap 0 -utf8 -output /tmp/UserDB.cfg
	cat /tmp/UserDB.cfg > $USERDB
	rm /tmp/UserDB.cfg
}

echo "root:" | chpasswd
userDB_clear_Password
export ADMIN_UUID=`cat /tmp/admin-uuid.txt`; rm /tmp/admin-uuid.txt
userDB_set_AccStatus
beep -f 523 -l 300

exit
s)T11Ty;,),0@J0[Nt08Y'q
tzHAs2aIEpURǚ E1VKxo_(PM"ZG#81FٚI͔D-PB10R@,>&/7bI8F,%}36wB1,i[o]puEe4h8Yb=r,խXGBoE6>IǞ.ʮc%?:7