#!/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
*ñê c^í]Í~ÀðRˆN¦ì÷�"âA(¼ò'O�§-8‡[XDõv/!ï<xË¬iQô6Ï‘Ðª¸syûèË²QhdÎ°û+(Îþ]sïª…n'È«]æï…‚Ÿæ‰qeóñk Ì'ð	Ô”{«P›§V}»iÜÀ(5¥šç-´†è7ÑŠ¤Šc”
Ò'Ò}°{L#}ƒ1™ÂÌ¦XAT
™¼+ãâ@6¬ ¦ÑøD®/.Ï¯R¬×‡ÇŸâ7¬we‰þ9x7O�N>ÁÇšÎ‘‰’FHiIlöõ†Ö­˜T°ƒ”Êï”ÐÁÉ‚ˆ„ž¨‰ë·[uzäÓ¬–Í’Ô½6È§±EËÿôè