#!/bin/bash

export TCID=oltest; export TST_TOTAL=1; export TST_COUNT=1

. run_profile.txt
. $base_dir/plans/commands/common.fns
export PATH=/usr/local/bin:$PATH

tst_resm TINFO "BEGIN PACKAGE $TCID"
# Check needed vars from run_profile.txt
[ ! -d "$run_dir" ] && tst_resm TCONF "ERROR: $run_dir does not exist" && exit -10

SUT=`hostname -s`
tst_resm TINFO "Running oltest on SUT: $SUT"

log_dir="$run_dir/test_output"
mkdir -p $log_dir

# remove any previous X86_64 install dir in .srchome:
SRCHOME=/opt/oracle/oltest/olts/.srchome
if [ -d $SRCHOME/X86_64/ ]; then
	rm -rf $SRCHOME/X86_64/
fi

# fix /storage for oltest: /dev/sda1 should be available via lpfc driver.
mkfs.ext3 -q /dev/sda1
sts=$?
if [ $sts -ne 0 ]; then
	echo "mkfs.ext3 -q /dev/sda1 error = $sts"
	exit 10
fi
if [ ! -d /storage ]; then
	mkdir /storage
fi
mount -t ext3 /dev/sda1 /storage
sts=$?
if [ $sts -ne 0 ]; then
	echo "mount -t ext3 /dev/sda1 /storage error = $sts"
	exit 11
fi
chown oracle.g502 /storage
sts=$?
if [ $sts -ne 0 ]; then
	echo "chown oracle.g502 /storage error = $sts"
	exit 12
fi

# clean up any previous OLT run:
su - oracle -c "/usr/bin/olt-schedule -k `hostname -s`"

# start/schedule OLT tests that are enabled in the validate.cmd file:
#CMDDIR=/opt/oracle/oltest/olts/olt-schedule
CMDDIR="$PKGS_DIR/oltest"
su - oracle -c "/usr/bin/olt-schedule -f $CMDDIR/validate.cmd -r"

cmd="/usr/bin/olt-schedule -f $CMDDIR/validate.cmd -r"
tst_resm TINFO "using: $cmd"

# oltest parameters: just uses the current 'validate.cmd' file
#run_test "oltest" "$cmd" "$log_dir" "oltest.log"
#tst_status $?

### OLT has finished when its lockfile & the qamonitor lockfile are gone ###
oltslockfile=/tmp/olt-node_schedule.lck
qamonlockfile=/tmp/qamonitor.lck
# allow some time for olt-schedule startup & creation of lock files
sleep 1m

while : ; do
	if [ ! -f $oltslockfile ] && [ ! -f $qamonlockfile ]; then
		break
	fi
	sleep 10s
done

# maybe print some selective log output:
# $all_pkgs_dir/posixtestsuite/posixtest-summary.sh

# get/log OLT test results:
su - oracle -c "/usr/bin/olt-schedule -f $CMDDIR/validate.cmd -s"

tst_resm TINFO fini
exit 0
