#!/bin/bash

CONFIGDIR="/oem/.record"
SYSTEMCONF="/etc/speaker.conf"
FIRMWARECONF="${CONFIGDIR}/firmware_url"

action="$1"
address="$2"
type="$3"

[ -z "${action}" ] && echo "Usage:$0 dhcp|static" && exit 1

[ ! -d ${CONFIGDIR} ] && mkdir -p ${CONFIGDIR}
if [ ! -f ${FIRMWARECONF} ];then
    touch ${FIRMWARECONF}
    cat << END > ${FIRMWARECONF}
[firmware]
url=test
END
fi

[ ! -d ${CONFIGDIR} ] && mkdir -p ${CONFIGDIR}

get_mac() {
    real_model="`/etc/scripts/getmodel.sh`"
    if [ "foo${real_model}" = "fooX10" -o "foo${real_model}" = "fooX10_V2" ];then
        interface="br0"
    else
        interface="eth0"
    fi
    echo "`ifconfig ${interface} | grep HWaddr | awk '{print $5}' | tr -d ':' | tr -d ' '`"
}

handle_conf() {
    /etc/scripts/conf_handle.sh export_${format}
    res="`/etc/scripts/import_compare.sh ${CONFIGDIR}/new.json /tmp/conf`"
    if [ ! -z "${res}" ];then
        cp ${CONFIGDIR}/new.json /tmp/conf
        res="`/etc/scripts/conf_handle.sh import`"
        if [ -z "${res}" ];then
            rm -rf ${CONFIGDIR}/new.json
            echo "Auto privisioning Successfully"
            if [ "foopnp" != "foo${type}" ];then
                /etc/scripts/shell_action.sh showReboot
                sleep 1
                /sbin/reboot
            fi
        else
            echo "Import configuration failed"
            rm -rf ${CONFIGDIR}/new.json
            exit 1
        fi
    else
        rm -rf ${CONFIGDIR}/new.json
        echo "Configuration same as local, dont need apply"
        exit 0
    fi
}

mac="`get_mac`"
model="`sysconf ${SYSTEMCONF} get system oem_model`"
[ -z "${model}" ] && model="`sysconf ${SYSTEMCONF} get system model`"
softwareVersion="`sysconf ${SYSTEMCONF} get system firmware`"
[ -z "${mac}" ] && echo "Please check network interface" && exit 1
format="`sysconf ${SYSTEMCONF} get auto_privisioning format`"
[ -z "${format}" ] && echo "Please check auto privisioning format" && exit 1
if [ "foo${action}" = "foodhcp" ];then
    [ -z "${address}" ] && echo "Not found http url or tftp server" && exit 1
    result=$(echo ${address} | grep "://")
    if [ -z ${result} ];then
        mode="tftp"
    else
        mode="http"
        final_char="${address: -1}"
        if [ "foo${final_char}" = "foo/" ];then
            address="${address%?}"
        fi
    fi
else
    mode="`sysconf ${SYSTEMCONF} get auto_privisioning mode`"
    address="`sysconf ${SYSTEMCONF} get auto_privisioning address`"
    [ -z "${address}" -o -z "${mode}" ] && echo "Need http url and mode" && exit 1
    final_char="${address: -1}"
    if [ "foo${final_char}" = "foo/" ];then
        address="${address%?}"
    fi
fi

if [ "foo${mode}" = "footftp" ];then
    #get config file from tftp server
    tftp -g -l ${CONFIGDIR}/new.json -r ${mac}.${format} ${address}
    retcode="$?"
    [ "foo${retcode}" = "foo1" ] && echo "Failed download file form tftp server" && exit 1
    handle_conf
elif [ "foo${mode}" = "foohttp" ];then
    #get config file from http server
    #"Zycoo-X10 v_s1.2.5 68692E290C5D"
    user_agent="Zycoo-${model} ${softwareVersion} ${mac}"
    if [ "foopnp" = "foo${type}" ];then
        retcode="`/usr/bin/curl -o ${CONFIGDIR}/new.json -k --user-agent "${user_agent}" --connect-timeout 3 -w %{http_code} ${address}`"
    else
        retcode="`/usr/bin/curl -o ${CONFIGDIR}/new.json -k --user-agent "${user_agent}" --connect-timeout 3 -w %{http_code} ${address}/${mac}.${format}`"
    fi
    if [ "foo${retcode}" = "foo200" ];then
        handle_conf
    else
        rm -rf ${CONFIGDIR}/new.json
        exit 1
    fi
fi

exit 0