#!/bin/sh /etc/rc.common
# Copyright (C) 2013-2014 OpenWrt.org

START=03
STOP=00

boot() {
	local curtime="$(date +%s)"
	local maxtime="$(find /etc -type f -exec date -r {} +%s \; | sort -nr | head -n1)"
	if [ -f /data/dni/ntp/save_time ];then
		save_time=`cat /data/dni/ntp/save_time`
	fi
	if [ "x$save_time" != "x" ];then
		local save_time_s="$(date +%s -d $save_time)"
		[ $maxtime -gt $curtime ] && [ $maxtime -gt $save_time_s ] && date -s @$maxtime
		[ $save_time_s -gt $curtime ] && [ $save_time_s -gt $maxtime ] && date $save_time
	else
		[ $curtime -lt $maxtime ] && date -s @$maxtime
	fi
}

start()
{
	boot
}

stop()
{
	#Save ntp synced time for Armor action in next time booting
	[ ! -d /data/dni/ntp ] && mkdir -p /data/dni/ntp
	date +%Y.%m.%d-%H:%M:%S -u > /data/dni/ntp/save_time
}
