#!/bin/sh
exec_option=$1
serv_option=$2
time_option=$3

if [ -f /usr/local/data/configuration/Network/NTP.cfg ]
then
        . /usr/local/data/configuration/Network/NTP.cfg
        . /usr/local/data/configuration/Network/Wan.cfg
        if [ X"$exec_option" = X"start" ]
        then
            pid=`pidof ntpclient`
            if [ X"$pid" != X"" ]
            then
            	kill -9 $pid
            fi
			x_tmp=$(echo $ENABLE | tr [A-Z] [a-z])
      if [[ "$x_tmp" = "yes" ]]
			then
				if [ X"$serv_option" != X"primary" ]
				then
					if [ X"$time_option" != X"7FFFFFFF" ]
					then
					        if [ X"$NETWORKTYPE" = X"2" ]
                                                then
						    ntpclient -6 -s -c 525600 -i ${INTERVAL} -h ${NTPSERVER2} -o ${time_option} &
						else
						    ntpclient -4 -s -c 525600 -i ${INTERVAL} -h ${NTPSERVER2} -o ${time_option} &
						fi
					else
						if [ X"$NETWORKTYPE" = X"2" ]
						then
						    ntpclient -6 -s -c 525600 -i ${INTERVAL} -h ${NTPSERVER2} -o ${TIMEOFFSET} &
						else
						    ntpclient -4 -s -c 525600 -i ${INTERVAL} -h ${NTPSERVER2} -o ${TIMEOFFSET} &
						fi
					fi
					if [ "$?" != "0" ]
					then
						exit 1
					fi
				else
					if [ X"$time_option" != X"7FFFFFFF" ]
					then
						if [ X"$NETWORKTYPE" = X"2" ]
                                                then
						    ntpclient -6 -s -c 525600 -i ${INTERVAL} -h ${NTPSERVER} -o ${time_option} &
                                                else
                                                    ntpclient -4 -s -c 525600 -i ${INTERVAL} -h ${NTPSERVER} -o ${time_option} &
						fi
					else
					        if [ X"$NETWORKTYPE" = X"2" ]
                                                then
						    ntpclient -6 -s -c 525600 -i ${INTERVAL} -h ${NTPSERVER} -o ${TIMEOFFSET} &
					        else
						    ntpclient -4 -s -c 525600 -i ${INTERVAL} -h ${NTPSERVER} -o ${TIMEOFFSET} &
						fi
					fi
          if [ "$?" != "0" ]
          then
            exit 1
          fi
				fi
			fi
        elif [[ X"$exec_option" = X"stop" ]]
        then
                pid=`pidof ntpclient`
				if [ X"$pid" != X"" ]
                then
                        kill -9 $pid
                fi
        else
                echo 'usage ntpc start|stop'
        fi
else
        echo 'Hi, guy, i need config file'
fi
