#!/usr/bin/perl
# 
# Automatic execution of contest <http://contest.kolivas.net/>
# against kernel images listed in /etc/lilo.conf
#
# autocontest.pl v0.04a Run it @ your own risk.
#
# Copyright 2002, Robinson Maureira, all rights reserved.
#
# Author:  Robinson Maureira Castillo <rmaureira@alumno.inacap.cl>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# The script takes care of boot each kernel image and run
# contest a couple of times with each one.
#
# Following assumptions are made:
# 
# - You're using LILO, GRUB version will follow if this one probes useful
# - Your system is compatible with redhat or debian (It was tested on those)
# With "compatible" I mean:
# It has /etc/inittab, /etc/rc*.d, and a SysV way of "init"
# - You have PERL installed, if not, it won't run, believe me 8)
# - You have a directory /usr/src/linux-2.4.19 for make the compiles
# - Options used to boot are vga=normal and apm=off
# 
# I can't say it will work on your system, "It works for me(tm)"
# Feedback is of course welcome, rants about PERL Monkey Style goes to /dev/null
#
# Changelog:
#
# v0.04a - User definable variables
# v0.03b - Some code commenting
# v0.02  - uid(0) check, autoselection of runlevel number
# v0.01  - insane first version
#

# Customize at your taste, defaults should be ok
# nload = how many times noload will run
# pload = the same for process load
# mload = memory load
# ioload = io load

$nload = 1;
$pload = 1;
$mload = 3;
$ioload = 5;


# If you're not root, you can't run autocontest.pl
die "Yay! you're not root, su and try again" if ($< ne 0);

# Let's see if we can create a new dummy runlevel directory
# If we can't, we die.
for ($rl = 7; $rl <= 9; $rl++) {
	next if ((-d "/etc/rc$rl.d/") || (-l "/etc/rc$rl.d/"));
	$new = $rl;
	last; 
}
die "There's no space to create a new runlevel, this is needed for autocontest" if !$new;

# Now we parse the configuration file for lilo
# After this, we'll have a list of images (labels)
# and the total number of those.
open FD, "/etc/lilo.conf" or die "$!\n";
while(<FD>){
	if(grep /label/,$_) {
		chop;
		push @labels,$_;
	}
}
close FD;

# Let's erase any previous version of /.labels
system("/bin/rm","-f","/.labels");

foreach $line (@labels) {
	($x,$label) = split /=/, $line;
	push @lb,$label;
	$com = "/bin/echo \"$label\" >> /.labels";
	system($com);
}
$times = scalar(@lb);
$kernel = $lb[$times - 1];


# OK then, let's create the dummy dir
# backup inittab and make some changes
# after that, let's put the number of reboots in the /.num file
system("/bin/mkdir","-p","/etc/rc$new.d/");
system("/bin/cp","/etc/inittab","/etc/inittab.contest");

$rc = `/usr/bin/find /etc/ -name rc 2> /dev/null | head -n 1`;
chomp $rc;

$com = "/bin/echo \"l$new:$new:wait:$rc $new\" >> /etc/inittab";
system($com);
$com = "/bin/echo \"c:$new:once:/bin/bash --init-file /.contest\" >> /etc/inittab";
system($com);
$com = "/bin/echo $times > /.num";
system($com);

# Now we create the initfile for each reboot
open FD, "> /.contest" or die "$!\n";
print FD<<EOF;
#!/bin/sh
#
RUNLEVEL=$new
TIMES=`cat /.num`
NAME=`head -n \$TIMES /.labels | tail -n 1`
cd /usr/src/linux-2.4.19
contest -k \$NAME -l noload -n $nload
contest -k \$NAME -l process_load -n $pload
contest -k \$NAME -l mem_load -n $mload
contest -k \$NAME -l io_load -n $ioload -r
TIMES=`perl -e "print (\$TIMES - 1)"`
NAME=`head -n \$TIMES /.labels | tail -n 1`
if [ -z \$NAME ] ;
then
        cp -f /etc/inittab.contest /etc/inittab
        rm -fr /etc/rc\$RUNLEVEL.d/
	rm -fr /.num
	rm -fr /.labels
        lilo
        reboot
	exit 0
fi
echo \$TIMES > /.num
lilo -R \$NAME \$RUNLEVEL vga=normal apm=off
reboot
EOF
close FD;

# Finally, set lilo for the first reboot, and reboot.
system("lilo","-R",$kernel,$new,"vga=normal","apm=off");
system("reboot");

# We should never get here 8)
exit 0;

