#!/bin/sh -e

. _inq-config-global
. $SHARE_DIR/functions
. $SHARE_DIR/communication
export PATH
if [ -r $SHARE_DIR/init ] && ! [ -r /proc/cpuinfo ]; then
	. $SHARE_DIR/init
fi

# ===========================================================================
# Detects
# ===========================================================================

echo 'Starting detects'
inq-detect
echo 'Detects finished'

# ===========================================================================
# Self-identification
# ===========================================================================

echo 'Starting self-identification'
COMPUTER_ID=`inq-self-id`
[ -n "$COMPUTER_ID" ] || fatal_failure 'Self-identification'
export COMPUTER_ID

# Reporting IP-address to the server
publish_my_ip || fatal_failure 'IP-address update'

# ===========================================================================
# Start logging
# ===========================================================================

hostname c$COMPUTER_ID
service_start syslog-ng || :

# ===========================================================================
# Set time
# ===========================================================================

echo -n 'Trying set local time '
if [ -n "$NTP_SERVER" ]; then
	if /usr/sbin/ntpdate $NTP_SERVER $1>/dev/null && /sbin/hwclock --systohc; then
		echo_success
	else
		echo_failure
	fi
else
	echo -n '(ntp server does not set)'
	echo_skipped
fi

# ===========================================================================
# Start watchdog, if available
# ===========================================================================

watchdog_start

# ===========================================================================
# Submit configuration by REST
# ===========================================================================

submit_components $HOME/components.xml

# ===========================================================================
# Starting monitorings
# ===========================================================================

echo 'Starting monitorings'
inq-monitoring
echo 'Monitorings were started'

# ===========================================================================
# Starting software detects
# ===========================================================================

echo 'Starting software detects'
inq-software-detect
echo 'Software detects finished'

# ===========================================================================
# Run all tests
# ===========================================================================

plan_test_script
. $HOME/test_script

# We completed all tests
print_green_message "================"
print_green_message "TESTING FINISHED"
print_green_message "================"

testing_finished
