#!/bin/bash
+
+#configuration
 TODAY=`date +%Y%m%d`
 YESTERDAY=`date -d yesterday +%Y%m%d`
 TMPDIRNAME=$HOME/__${TODAY}_crontab
 SHELLTIME2CENTSPHP=scripts/shell_time2cents.php
 SHELLADDERPHP=scripts/shell_adder.php
 COMMONPHP=scripts/public_html/common.php
-
-
+MYSQL="mysql -u helm -h mowgli.cs.unibo.it matita"
+SQLQMARK="select distinct mark from bench where mark like '"
+SQLQORD="' order by mark;"
+SQLQTIME="select SUM(timeuser) from bench where mark = \""
+SQLQGRMARK="\" group by mark;"
+SQLQFAIL="select count(distinct test) from bench where mark = \""
+SQLQFAIL1="select distinct test from bench where mark = \""
+SQLQFAIL2="\" and result = 'fail';"
+URL="http://mowgli.cs.unibo.it/~tassi/bench.php"
+
+#initialization
 OLD=$PWD
 mkdir -p $TMPDIRNAME
 rm -rf $TMPDIRNAMEOLD
 cd $TMPDIRNAME
 rm -rf helm
 svn co ${SVNROOT}helm/software/matita/scripts/ > LOG.svn 2>&1
+
+#run tests
 scripts/profile_svn.sh 2> LOG
 
-MARK=`echo "select distinct mark from bench where mark like '$TODAY%' order by mark" | mysql -u helm matita | tail -n 1`
-LASTMARK=`echo "select distinct mark from bench where mark like '$YESTERDAY%' order by mark" | mysql -u helm matita | tail -n 1`
+MARK=`echo $SQLQMARK$TODAY%$SQLQORD | $MYSQL | tail -n 1`
+LASTMARK=`echo $SQLQMARK$YESTERDAY%$SQLQORD | $MYSQL | tail -n 1`
 
 if [ -z "$MARK" ]; then
   echo "No benchmark records for $TODAY"
   exit 1
 fi
 
-CUR_TIME=`/usr/bin/php4 -c /etc/php4/apache/php.ini -f $SHELLADDERPHP -- $COMMONPHP "select SEC_TO_TIME(SUM(TIME_TO_SEC(time))) from bench where mark = \"$MARK\" group by mark;"`
-OLD_TIME=`/usr/bin/php4 -c /etc/php4/apache/php.ini -f $SHELLADDERPHP -- $COMMONPHP  "select SEC_TO_TIME(SUM(TIME_TO_SEC(time))) from bench where mark = \"$LASTMARK\" group by mark;"`
-
-CUR_CENTS=`/usr/bin/php4 -c /etc/php4/apache/php.ini -f $SHELLTIME2CENTSPHP -- $COMMONPHP $CUR_TIME`
-OLD_CENTS=`/usr/bin/php4 -c /etc/php4/apache/php.ini -f $SHELLTIME2CENTSPHP -- $COMMONPHP $OLD_TIME`
+#check for slowdown
+CUR_TIME=`echo $SQLQTIME$MARK$SQLQGRMARK | $MYSQL`
+OLD_TIME=`echo $SQLQTIME$LASTMARK$SQLQGRMARK | $MYSQL`
 
 ((DELTA=$CUR_CENTS-$OLD_CENTS))
 if [ $DELTA -lt 0 ]; then
   PERC=0
 else
-  ((PERC=100 * $DELTA))
-  ((PERC=$PERC / $OLD_CENTS))
+  PREC=`scripts/functions.lua proportion $DELTA x $OLD_CENTS 100`
 fi
 if [ $PERC -ge 5 ]; then
   cat <<EOT
-  REPORT FOR `date`
-  http://mowgli.cs.unibo.it/~tassi/bench.php
-
+  
   PERFORMANCE LOSS DETECTED (MARK $MARK vs MARK $LASTMARK)
-  is $CUR_TIME sec 
-  was $OLD_TIME sec
-
+  
+  Is `scripts/functions.lua t2s $CUR_TIME` 
+  
+  Was `scripts/functions.lua t2s $OLD_TIME`
+  
+  For details: $URL
 EOT
 fi
 
-CUR_FAIL=`/usr/bin/php4 -c /etc/php4/apache/php.ini -f $SHELLADDERPHP -- $COMMONPHP "select count(distinct test) from bench where mark = \"$MARK\" and result = 'fail';"`
-OLD_FAIL=`/usr/bin/php4 -c /etc/php4/apache/php.ini -f $SHELLADDERPHP -- $COMMONPHP "select count(distinct test) from bench where mark = \"$LASTMARK\" and result = 'fail';"`
+#check for more broken tests
+CUR_FAIL=`echo $SQLQFAIL$MARK$SQLQFAIL2 | $MYSQL`
+OLD_FAIL=`echo $SQLQFAIL$LASTMARK$SQLQFAIL2 | $MYSQL`
 
 if [ $CUR_FAIL -gt $OLD_FAIL ]; then
   cat <<EOT
-  REPORT FOR `date`
-  http://mowgli.cs.unibo.it/~tassi/bench.php
 
   MORE BROKEN TESTS DETECTED (MARK $MARK vs MARK $LASTMARK)
-  now broken:
-  `echo "select distinct test from bench where mark = \"$MARK\" and result = 'fail';" | mysql -u helm -h mowgli.cs.unibo.it matita`
-  were broken:
-  `echo "select distinct test from bench where mark = \"$LASTMARK\" and result = 'fail';" | mysql -u helm -h mowgli.cs.unibo.it matita`
   
+  Now broken:
+`echo $SQLQFAIL1$MARK$SQLQFAIL2 | $MYSQL`
+
+  Were broken:
+`echo $SQLQFAIL1$LASTMARK$SQLQFAIL2 | $MYSQL`
+  
+  For details: $URL
 EOT
 
 fi
 
        mark_re .. sep .. opt_re 
 
 -- the string to cents of seconds function
-function convert(s)
+function s2t(s)
        local _,_,min,sec,cents = string.find(s,"(%d+)m(%d+)%.(%d+)s")
        return min * 6000 + sec * 100 + cents
 end
+function t2s(s)
+  s = tonumber(s)
+  local min, sec, cents
+  cents = s % 100
+  min = math.floor(s / 6000)
+  sec = math.floor((s - min * 6000 - cents) / 100)
+  return string.format("%dm%02d.%02ds",min,sec,cents)
+end
 
 -- logfile to sql conversion
 function log2sql(file)
        if opt ~= "gc-on" and opt ~= "gc-off" then
                error("unknown option "..opt)
        end
-       real = convert(real)
-       user = convert(user)
+       real = s2t(real)
+       user = s2t(user)
 
        -- print the sql statement
        table.insert(t,string.format(fmt,rc,tool,test,real,user,mark,opt))
 return table.concat(t)
 end
 
+-- proportions
+function proportion(x,y,a,b)
+  local rc
+  if x == "x" then
+    rc = y * a / b
+  elseif y == "x" then
+    rc = x * b / a
+  elseif a == "x" then
+    rc = x * b / y
+  elseif b == "x" then
+    rc = y * a / x
+  end
+  return string.format("%d",rc)
+end
+
+-- conversion from the old db to the new one
+function convertsql(file)
+  local f = io.open(file)
+  return string.gsub(f:read("*a"),time_re,
+    function(s)
+      return s2t(s)
+    end)
+end
+
 -- call the desired function and print the result
 fun = arg[1]
 table.remove(arg,1)
 
 #!/bin/bash
+
+#configuration
 MARK=`date +%Y%m%d%H%M`
 TMPDIRNAME=__${MARK}_compilation
 SVNROOT="svn+ssh://mowgli.cs.unibo.it/local/svn/helm/trunk/"
+MYSQL="mysql -u helm -h mowgli.cs.unibo.it matita"
+SVNLOG=$TMPDIRNAME/LOG.svn
 
+#helpers
 function testit {
   LOGTOOPT=/dev/null
   LOGTOBYTE=/dev/null
-  export DO_TESTS_EXTRA="$MARK\t$@"
-  make tests DO_TESTS_OPTS="-no-color -twice -keep-logs"
-  make tests.opt DO_TESTS_OPTS="-no-color -twice -keep-logs"
+  export BENCH_EXTRA_TEXT="$MARK\t$@"
+  make tests 
+  make tests.opt 
 }
 
 function compile {
   LOCALOLD=$PWD
   cd $1
   ./matitaclean all
-  mkdir .matita
-  export OCAMLRUNPARAM='o=1000000'
+  export OCAMLRUNPARAM='o=100000'
   testit "gc-off"
   export OCAMLRUNPARAM=''
   testit "gc-on"
   cd $LOCALOLD
 }
 
+#initialization
 OLD=$PWD
 rm -rf $TMPDIRNAME
 mkdir $TMPDIRNAME
 mkdir $TMPDIRNAME.HOME
 cd $TMPDIRNAME
-SVNLOG=`pwd`/LOG.svn
 
-#svn
+#svn checkout
 svn co -N $SVNROOT > $SVNLOG 2>&1
 cd trunk 
 svn update -N helm >> $SVNLOG 2>&1
 #run
 run_tests $PWD/helm/software/matita > LOG 2>LOG.run_tests.err
 
-cat LOG | grep "\(OK\|FAIL\)" | grep "\(gc-on\|gc-off\)" | awk -f $PWD/helm/software/matita/scripts/insert.awk > INSERT.sql
-cat INSERT.sql | mysql -u helm -h mowgli.cs.unibo.it matita
-SVNREVISION=`cat $SVNLOG | grep revision | tail -n 1 | sed "s/.*revision \(\w\+\)./\1/"`
-echo "INSERT INTO bench_svn VALUES ('$MARK','$SVNREVISION')" | mysql -u helm -h mowgli.cs.unibo.it matita
+#insert the db
+cat LOG | grep "\(OK\|FAIL\)" | grep "\(gc-on\|gc-off\)" | \
+  $PWD/helm/software/matita/scripts/functions.lua log2sql - > INSERT.sql
+cat INSERT.sql | $MYSQL
+
+#save the revision
+SVNREVISION=`svn info $PWD/helm/software/ | grep "^Revision:" | cut -d : -f 2`
+echo "INSERT INTO bench_svn VALUES ('$MARK','$SVNREVISION')" | $MYSQL
 cd $OLD
 #rm -rf $TMPDIRNAME
+
+#eof