#!/bin/sh
arch=Olinux-x86_64
arch32=Olinux-x86_32
LIST="$arch-gmp\
 $arch\
 $arch-none\
 $arch32-gmp\
 $arch-noinline\
 $arch-para"

cov_init () {
  for d in $LIST; do rm -rf gcov-$d; done
  $conf --builddir=gcov-$arch-gmp
  $conf --without-gmp --builddir=gcov-$arch
  $conf --kernel=none --builddir=gcov-$arch-none
  env CFLAGS=-DDISABLE_INLINE $conf --kernel=none --builddir=gcov-$arch-noinline
  env CFLAGS=-m32 linux32 $conf --builddir=gcov-$arch32-gmp
  $conf --builddir=gcov-$arch-para --mt=pthread
}

cov_build() {
  for d in $LIST; do
    (cd gcov-$d && make -j4 gp)
  done
}

cov_test() {
  for d in $LIST; do
    (cd gcov-$d && make -j4 gp && make lcov-reset && make statest-all) 
  done
}
cov_test_nohup() {
  for d in $LIST; do
    (cd gcov-$d && make -j4 gp && make lcov-reset && make statest-all) &
  done
}
cov_test2() {
  (cd $main && make test-ploth test-io test-parallel test-install test-env test-time)
  (cd $main && echo "1+1"|./gp-sta --quiet --fast)
  (cd $main && echo "1+1"|./gp-sta -D lines=1 -D timer=1)
  (cd $main && ./gp-sta --version)
  (cd $main && ./gp-sta --version-short)
  (cd $main && ./gp-sta --default parisize=1000)
  (cd $main && ./gp-sta --dont-exist)
  (cd gcov-$arch-para && make test-parallel)
}

cov_write() {
  FLAGS=`echo $LIST | sed -e 's/O/-d ..\/gcov-O/g'`
  cd $main
  prolog=/tmp/prolog.html
  (cd /home/www/wml && wml lcov-prolog.wml > $prolog)
  make LCOV_REPORT=/home/www/lcov-report$1 \
    GENHTML_FLAGS="--html-prolog $prolog" \
    LCOV_FLAGS="$FLAGS" lcov-report
}

# to test another branch than 'master'; for the time being, hardcode henri-mf
cov_extra() {
  $conf --builddir=$main
  (cd $main && make -j4 gp-sta)
  (cd $main && make lcov-reset && make test-mf)
  LIST=$arch-gmp
  cov_write "-extra"
}

main=gcov-$arch-gmp
conf="./Configure -gcov"
case $1 in 
  -i|--init)  cov_init;;
  -b|--build) cov_build;;
  -t2|--test2)cov_test2;;
  -t|--test)  cov_test; cov_test2;;
  -T|--test-nohup) cov_test2; cov_test_nohup ;;
  -A|--all-nohup)cov_init; cov_build; cov_test2; cov_test_nohup;;
  -a|--all) cov_init; cov_build; cov_test; cov_test2; cov_write ;;
  -e|--extra) cov_extra;;
  *) cov_write;;
esac
