#!/bin/sh                                                                                                              

set -e

                                                                                                 
DNSSENSE=/usr/sbin/dnssense                                                                                            
HTTP=/usr/sbin/dnssensehttp                                                                                            
                                                                                        
                                                                                                                       
DNSSENSEVERSION=$($DNSSENSE -v|grep "Roksit"|sed 's/Roksit version //g')                                               
HTTPVERSION=$($HTTP -v)                                                                                                


CURL=$(which uclient-fetch)                                                                                                                       
                                                                                                                       
UPDATED=n                                                                                                              

download_file_else_exit(){
        echo "downloading $1 to $2"
        $CURL -q -O $2 $1
        if [ $? -eq 0 ]
        then
        echo "success request"
        else
        echo "failed request"
        exit 1
        fi
}

DNSSENSEUPDATEURL=http://ports.dnssense.com/download/mikrotik_hapaclite    

echo "checking dnssenseupdate.sh"
download_file_else_exit $DNSSENSEUPDATEURL/file/dnssenseupdate.sh /tmp/dnssenseupdate.sh 
mv /tmp/dnssenseupdate.sh /usr/sbin/dnssenseupdate.sh
chmod 755 /usr/sbin/dnssenseupdate.sh                                                                                                                
echo "checking dnssense"

download_file_else_exit $DNSSENSEUPDATEURL/version/latest.dnssense.txt  /tmp/dnssense.txt  
                                                                                                                       
tmp=$(cat /tmp/dnssense.txt)                                                                                           
                                                                                                                       
if [ $tmp != $DNSSENSEVERSION ];then                                                                                   
        echo "updating dnssense"                                                                                       
        download_file_else_exit $DNSSENSEUPDATEURL/file/dnssense /tmp/dnssense                                                         
        mv /tmp/dnssense /usr/sbin/dnssense
        chmod 755 /usr/sbin/dnssense                                                                            
        echo "updated dnssense"                                                                                        
        UPDATED=y                                                                                                      
fi

echo "checking dnssensehttp"
download_file_else_exit $DNSSENSEUPDATEURL/version/latest.dnssensehttp.txt  /tmp/dnssensehttp.txt                                               
                                                                                                                       
tmp=$(cat /tmp/dnssensehttp.txt)                                                                                           
                                                                                                                       
if [ $tmp != $HTTPVERSION ];then                                                                                   
        echo "updating dnssensehttp"                                                                                       
        download_file_else_exit $DNSSENSEUPDATEURL/file/dnssensehttp /tmp/dnssensehttp                                                        
        mv /tmp/dnssensehttp /usr/sbin/dnssensehttp
        chmod 755 /usr/sbin/dnssensehttp                                                                            
        echo "updated dnssensehttp"                                                                                        
        UPDATED=y                                                                                                      
fi


if [ $UPDATED = y ];then
    
    /etc/init.d/dnssense stop
    /etc/init.d/dnssense start
    
fi




