#!/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/X86_64/
if [ -d $SRCHOME ]; then
	rm -rf $SRCHOME
fi
SRCHOME1=/opt/oracle/oltest/olts/.srchome/X86_64-2.1.0-2/
if [ -d $SRCHOME1 ]; then
	rm -rf $SRCHOME1
fi
SRCHOME2=/opt/oracle/oltest/.srchome/ora-11.1.0.6-X86_64/oradb/
if [ -d $SRCHOME2 ]; then
	rm -rf $SRCHOME2
fi

# fix /olt-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 /olt-storage ]; then
	mkdir /olt-storage
fi

if ! grep -q olt-storage /proc/mounts ; then
mount -t ext3 /dev/sda1 /olt-storage
sts=$?
if [ $sts -ne 0 ]; then
	echo "mount -t ext3 /dev/sda1 /olt-storage error = $sts"
	exit 11
fi
fi

chown oracle.g502 /olt-storage
sts=$?
if [ $sts -ne 0 ]; then
	echo "chown oracle.g502 /olt-storage error = $sts"
	exit 12
fi

# copy the /olt-storage-skeleton tree to /olt-storage
##su - oracle cp -far /olt-storage-skeleton/* /olt-storage

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

# allocate hugepages for any/all hugepage tests
###echo 800 > /proc/sys/vm/nr_hugepages
echo 2500 > /proc/sys/vm/nr_hugepages

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:
tst_resm TINFO oltest results:
su - oracle -c "/usr/bin/olt-schedule -f $CMDDIR/validate.cmd -s"

tst_resm TINFO fini
exit 0
