#!/bin/sh -e

. /etc/inquisitor/global
. $SHARE_DIR/functions
. $SHARE_DIR/communication
if [ -r $SHARE_DIR/init ] && ! [ -r /proc/cpuinfo ]; then
	. $SHARE_DIR/init
fi

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

# Save detection logs
dmidecode > $HOME/dmi.log
$SHARE_DIR/decode-dimms.pl > $HOME/spd.log
ipmitool fru 2>/dev/null > $HOME/fru.log || true
lspci > $HOME/lspci.log
lshw 2>/dev/null > $HOME/lshw.log

echo 'Starting detects'
for I in $SHARE_DIR/detect/*; do
	echo -n "Detecting `basename $I`"
	. $I
	echo_success
done
echo 'Detects finished'

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

echo 'Starting self-identification'
# Try various methods for self-id, stop after successful one
COMPUTER_ID=
for I in $SHARE_DIR/self-id/*; do
	echo -n "Trying self-id using $I"
	. $I
	if [ -n "$COMPUTER_ID" ]; then
		echo_success
		break
	else
		echo_failure
	fi
done
[ -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
/sbin/service syslog-ng start || :

# ===========================================================================
# 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

# ===========================================================================
# 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

bash
