#!/bin/bash

# (C) -2002,2002-2007 Jose Calhariz
# License: GPL-2

LINUXDIR=linux-2.6.18

function erro () {
echo Erro Inesperado 
exit 1
}

function usage () { 
    echo "Tests to see if the machine is stable.  This tests are mainly on"
    echo "memory, CPU and disks subsystems."
    echo "Run this script on a prepared dir with:"
    echo "  t.tar.bz2 - Big tar file to test consecutive extract and diffs, better if you use linux-2.6.9.tar.bz2 or linux-2.6.18.tar.bz2"
    echo "  linux-cpubuild - Directory with a configured linux kernel sources, better if you use vanilla 2.6.9 or 2.6.18 with a good .config file"
    echo "    It's better if you edit Makefile to remove the CC flag -Wall"
}


cpubuildsingle () {
run=0
end=$1
while [ $run -lt $end ] ;  do
    echo "==>>Compilação $[ run = run + 1 ] $end $2"
    touch ~/log/tick_stamp
    make dep > /dev/null || erro
    make clean > /dev/null || erro
    make bzImage > /dev/null || erro 
done

}

cpubuild () {
run=0
end=$1
while [ $run -lt $end ] ;  do
    echo "==>>Compilação $[ run = run + 1 ] $end $2"
    touch ~/log/tick_stamp
    make dep > /dev/null || erro
    make clean > /dev/null || erro
    make -j $3 bzImage > /dev/null || erro 
done

}

test_fs () {
run=0
rm -rf te1 te2 
while [ $run -lt $1 ] ; do
    echo "==>Comparação $[ run = run + 1 ] $2"
    touch ~/log/diff_${2}_${run}_stamp
    bzip2 -dc t.tar.bz2 | tar xf - || erro
    mv ${LINUXDIR} te1 || erro
    bzip2 -dc t.tar.bz2 | tar xf - || erro
    mv ${LINUXDIR} te2 || erro
    diff -r --brief --speed-large-files te1 te2 || erro
    rm -rf te1 te2 || erro
done
}

usage
echo Testing Level 1 `date`
touch zstart_level1
test_fs 1 Level_1

echo Testing Level 2 `date`
touch zstart_level2
pushd linux-cpubuild
cpubuildsingle 1 Level_2 
popd

echo Testing Level 3 `date`
touch zstart_level3
test_fs 10 Level_3

echo Testing Level 4 `date`
touch zstart_level4
pushd linux-cpubuild
cpubuild 1 Level_4 5
popd
echo Done

