#!/bin/bash

################################################################################
# Unlock script for the Zyxel Prestige 2303R VoIP adapter
# Copyright 2010 Brett T. Warden
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

HOST="$1"

if [ "${HOST}" == "" ]; then
				echo "Must specify hostname (or IP address) of P2302R"
				exit 1
fi

NEWPASS="5678"

echo "Fetching password from ${HOST}"
SYSPASS="`ncftpget -EVc "ftp://admin:1234@${HOST}/rom-t" | grep 'System Password =' | awk 'BEGIN {FS=" *= *"} {print $3}'`"

if [ $? != 0 ]; then
				echo "Failed to fetch System Password from ${HOST}"
				exit 1
fi

MESSAGE="
980600001 = Auto Provision Active <0(No) |1(Yes)> = 0
230000000 = System Password = ${NEWPASS}
981301027 = Challenge SIP Invite <0(No) | 1(Yes)> = 0
981301028 = Challenge SIP Notify <0(No) | 1(Yes)> = 0
"

echo "Setting new password for ${HOST} and disabling auto-provisioning"
echo "${MESSAGE}" | ncftpput -EVc -u VW_Admin -p "${SYSPASS}" "${HOST}" rom-t

if [ $? != 0 ]; then
				echo "Failed to set new options on ${HOST}"
				exit 1
fi

echo "Waiting for ${HOST} to reboot"
sleep 15
ping -w 90 -c 2 -q "${HOST}"

echo "Now login to ${HOST} as VW_Admin with password '${NEWPASS}'"
echo
