#!/usr/bin/env bash
#
# MT, run specific part of the test suite
# Copyright (C) 2018 Xavier Delaruelle
#
# 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 3 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, see <http://www.gnu.org/licenses/>.

##########################################################################

set -u

# print message on stderr then exit
echo_error() {
   echo -e "ERROR: $1" >&2
   exit 1
}

if [ ! -e modulecmd.tcl.in ]; then
   echo_error "Not in correct directory"
fi

# make bin, should use GNU make
command -v gmake >/dev/null
if [ $? -eq 0 ]; then
   make=gmake
else
   make=make
fi

# make target
target=test
if [ $# -gt 0 ]; then
   if [ "$1" = "cov" ]; then
      target=testcoverage
      shift
   elif [ "$1" = "install" ]; then
      target=testinstall
      shift
   fi
fi

if [ $# -gt 0 ]; then
   chmod 000 testsuite/modules.*
   chmod 755 testsuite/modules.00-init
   chmod 000 testsuite/modules.00-init/*.exp
   chmod 644 testsuite/modules.00-init/{005,006,010,050,080}-*.exp
   lasti=0
   for i in ${@}; do
      j=${i##*/}
      i=${i%/*}
      chmod 755 testsuite/modules.${i}*
      if [ "$j" != "$i" ]; then
         if [ "$i" != "00" -a "$lasti" != "$i" ]; then
            chmod 000 testsuite/modules.${i}*/*.exp
         fi
         for f in testsuite/modules.${i}*/{010,999,$j}*.exp; do
            if [ -e $f ]; then
               chmod 644 $f
            fi
         done
      elif [ "$i" == "00" ]; then
         chmod 644 testsuite/modules.00-init/*.exp
      fi
      lasti=$i
   done
fi

$make contrib/mtreview
$make icdiff
export RUNTESTFLAGS='-v -v >/dev/null 2>&1'
$make $target
ret=$?

# highlight failed tests
if [ "$target" = 'testinstall' ]; then
   contrib/mtreview install.log
else
   if [ -e compat/modules.log ]; then
      contrib/mtreview compat/modules.log
   fi
   contrib/mtreview modules.log
fi

chmod 755 testsuite/modules.*
chmod 644 testsuite/modules.*/*.exp

exit $ret
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent:
