#!/bin/sh

. /usr/syscfg/api_log.sh

DDNS_username=$1
DDNS_password=$2 
DDNS_domainname=$3
DDNS_interface=$4
DDNS_PATH=$5
DDNS_ipaddr=$6
DDNS_provider=$7

if [ "${DDNS_provider}" == "oray.com" ];then
    ddns_cmd="/usr/bin/curl --interface ${DDNS_interface} -u ${DDNS_username}:${DDNS_password} http://ddns.oray.com/ph/update?hostname=${DDNS_domainname}&myip=${DDNS_ipaddr}"
elif [ "${DDNS_provider}" == "tzo" ];then
    ddns_cmd="/usr/bin/curl --interface ${DDNS_interface} http://rh.tzo.com/webclient/tzoperl.html?tzoname=${DDNS_domainname}&email=${DDNS_username}&tzokey=${DDNS_password}&ipaddress=${DDNS_ipaddr}&system=tzodns&info=1"
elif [ "${DDNS_provider}" == "dyndns" ];then
    ddns_cmd="/usr/bin/curl --interface ${DDNS_interface} -u ${DDNS_username}:${DDNS_password} http://dyndns.org/nic/update?hostname=${DDNS_domainname}&myip=${DDNS_ipaddr}&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
elif [ "${DDNS_provider}" == "noip" ];then
    ddns_cmd="/usr/bin/curl --interface ${DDNS_interface} -u ${DDNS_username}:${DDNS_password} http://dynupdate.no-ip.com/nic/update?hostname=${DDNS_domainname}&myip=${DDNS_ipaddr}"
fi

TGP_Log ${TGP_LOG_WARNING} "${ddns_cmd}"

statu=`${ddns_cmd}`

TGP_Log ${TGP_LOG_WARNING} "statu=${statu}"

temp=`echo "${statu}" | cut -d ' ' -f 1`

if [ "${temp}" = "Unconfigure" ];then
    cfgcmd sset ${DDNS_PATH}.DDNSState Unsynchronized
elif [ "${temp}" = "good" -o "${temp}" = "nochg" ]; then
    cfgcmd sset ${DDNS_PATH}.DDNSState Synchronized
elif [ "${temp}" = "badauth" ]; then
    cfgcmd sset ${DDNS_PATH}.DDNSState Badauthication
elif [ "${temp}" = "abuse" ]; then
    return
else
    cfgcmd sset ${DDNS_PATH}.DDNSState Unsynchronized
fi