#!/bin/sh
# simple shell script wrapper for lsb-runtime-test
# Stew Benedict <sbenedict@mandriva.com>
# 01/31/2005 - 2nd version 0.2
# To run from cron use:
# openvt -w -- <path_to_this_script> cron
# Note: Some versions of openvt are broken and don't wait
# 2/16/2005 - combine common functions

RPMPKG0=lsb-tet3-lite
RPMPKG1=lsb-runtime-test

# common functions
. /usr/share/lsb/common-functions

install_rpm lsb-core
install_rpm sudo
install_rpm openssh-server

if ! `service sshd status | grep -q "is running"`; then
	service sshd start
fi

install_rpm openssh-clients

retrieve_rpm $RPMPKG0
retrieve_rpm $RPMPKG1

# (sb) we have to expose the password of vsx0
# also we can't drive the last bits via script 
# due to su refusing the root passwd from a file
# we get around that by aliasing su to sudo for vsx0

echo $VSX0_PASS | passwd --stdin vsx0 > /dev/null
echo $VSX1_PASS | passwd --stdin vsx1 > /dev/null
echo $VSX2_PASS | passwd --stdin vsx2 > /dev/null

# (sb) not defined under cron
TMP=$HOME/tmp
HOME=$TET_HOME
rm -f $TET_HOME/.configured
rm -f $TET_HOME/TESTROOT/tetexec.cfg.old

# answer runtime setup questions
TMPFILE=`mktemp $TMP/runtime.XXXXXX` || exit 1
cat << EOF > $TMPFILE
$TEST_USER
$TEST_ORG
$TEST_SYS
$BLK_DEV
y
y
$KERNEL
n
y
n
n
$VSX0_USER
$VSX0_PASS
$VSX1_PASS
$VSX2_PASS
$VSX0_PASS
y
$VSX0_PASS
y
$VSX0_PASS
$VSX0_PASS
$VSX0_PASS
$VSX0_PASS
$VSX0_PASS
$VSX0_PASS
EOF

chmod 0644 $TMPFILE
mv $TMPFILE $TET_HOME/tmp
TMPFILE=`echo $TMPFILE | sed 's|/root|/home/tet/test_sets|'`

# add vsx0 to sudoers
if `grep -q vsx0 /etc/sudoers`; then
	echo "vsx0 in sudoers..." > /dev/null
else
	echo "vsx0    ALL=(ALL) PASSWD: /bin/su root -c*" >> /etc/sudoers
fi

# su will not accept input from stdin 
mkdir /home/tet/bin
chown vsx0 /home/tet/bin
cat << EOF > $TET_HOME/../bin/su
#!/bin/sh
sudo -S /bin/su "\$@"
EOF
chmod +x $TET_HOME/../bin/su

# make keys for ssh login
if [ ! -f $TET_HOME/.ssh/id_dsa ]; then 
	su vsx0 -c "ssh-keygen -P '' -t dsa -f $TET_HOME/.ssh/id_dsa"
	su vsx0 -c "cp $TET_HOME/.ssh/id_dsa.pub $TET_HOME/.ssh/authorized_keys"
fi

# change the profile for vsx0
cp $PROFILE $PROFILE.lsb
# tty returns "not a tty" because of redirected stdin
# grab the current tty (running via ssh)
echo "cat << EOF > ../bin/tty" >> $PROFILE
echo '#!/bin/sh' >> $PROFILE
echo "echo \$SSH_TTY" >> $PROFILE
echo "EOF" >> $PROFILE
echo "chmod +x ../bin/tty" >> $PROFILE

# alias tcc so "run_tests" exits before the actual test run
ln -s /bin/false $TET_HOME/../bin/tcc

# fudge the path
echo "TET_ROOT=/opt/lsb-tet3-lite" >> $PROFILE
echo "TET_SUITE_ROOT=/home/tet" >> $PROFILE
echo 'TET_EXECUTE=$TET_SUITE_ROOT/test_sets/TESTROOT' >> $PROFILE
echo "export TET_SUITE_ROOT" >> $PROFILE
echo 'PATH=/home/tet/bin:$TET_ROOT/bin:$HOME/BIN:$TET_EXECUTE/BIN:$PATH' >> $PROFILE
echo "export TET_EXECUTE TET_ROOT PATH" >> $PROFILE
# setup the test run 
echo "run_tests < $TMPFILE" >> $PROFILE
# only need the tty/tcc aliases long enough to setup the tests
echo "rm -f ../bin/tty" >> $PROFILE
echo "rm -f ../bin/tcc" >> $PROFILE

# now run the tests, can't do it from "run_tests" with input redirected
echo "tcc -p -e -s $HOME/scen.exec test_sets" >> $PROFILE

# exit ssh after the test run
echo "exit" >> $PROFILE

# setup/run the tests via .profile 
su vsx0 -c "ssh $HOSTNAME"

# get results
JOURNAL=`find $TET_HOME/results -name journal -anewer $PROFILE`

#clean up profile
mv $PROFILE.lsb $PROFILE

#clean up keys
rm -f /home/tet/test_sets/.ssh/id_dsa*
rm -f /home/tet/test_sets/.ssh/authorized_keys

# cleanup
clean_file $TMPFILE
rm -fr $TET_HOME/../bin

if [ -f /etc/sudoers ]; then
	sed -i 's|vsx0\s*ALL=(ALL) PASSWD: /bin/su root -c\*||' /etc/sudoers	
fi

# if we're running from cron, we won't get any results back
# even if there are failures

if [ "$1" = "cron" ]; then
	report_results $JOURNAL cron
else
	report_results $JOURNAL
fi

