#! /bin/sh

check_oem_exist()
{
	if [ -f "/oem/.userdata" ]; then
		if [ ! -f "/oem/.upgrade" ]; then
			echo "keep config upgrade"
			/etc/scripts/merge_conf.sh
			touch /oem/.upgrade
			sleep 1
			rm -rf /userdata/config
			sync
			/sbin/reboot
			exit 0
		fi
	else
		[ -f /userdata/updatetime.txt ] && updatetime="`sysconf /userdata/updatetime.txt get upgrade date`"
		TARGET_DIR="/oem"
		find "$TARGET_DIR" -mindepth 1 ! -path "$TARGET_DIR/.flag" ! -path "$TARGET_DIR/.flag/*" ! -path "$TARGET_DIR/.onvif_upgrade" ! -path "$TARGET_DIR/.onvif_upgrade/*" ! -path "$TARGET_DIR/.language" ! -path "$TARGET_DIR/.language/*" -exec rm -rf {} +
		
		tar xvf /oem_backup.tar -C /oem/
		
		[ ! -z "${updatetime}" ] && sysconf /etc/speaker.conf set upgrade date ${updatetime}
		[ -f /oem/.flag/.language ] && language="`sysconf /oem/.flag/.language get system language`" && [ ! -z ${language} ] && sysconf /etc/speaker.conf set system language ${language}
		sleep 1
		touch /oem/.userdata
		rm -rf /userdata/config
		sync
		/sbin/reboot
		exit 0
	fi
}

start() {
	printf "Starting input-event-daemon: "
	printf "Check oem: "
	check_oem_exist
}


stop() {
	printf "Stopping input-event-daemon: "
	echo "done"
}

restart() {
	stop
	start
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	restart
	;;
  *)
	echo "Usage: $0 {start|stop|reload|restart}"
	exit 1
esac

exit $?
