#!/bin/sh

# Script to setup environment correctly and run test suites
#

# setting environment variables for testing
DBNAME=v3db
VERSION=3.4.3
ARCHLIB=lib
PKGROOT=/opt/lsb/test/libstdcpp_$VERSION
EXECROOT=/opt/lsb/test/$ARCHLIB/libstdcpp_$VERSION
QMTEST_CLASS_PATH=$PKGROOT/qm-classes/qmtc:$PKGROOT/qm-classes/qmtest_gcc
PATH=/opt/lsb/bin:/opt/lsb/appbat/bin:$PATH

# Set env variables for testing
export PKGROOT EXECROOT QMTEST_CLASS_PATH PATH

# Config param
config_arg()
{
  DEFAULT=$3

  printf "$2 ("
  if [ -n "$DEFAULT" ] ; then
    printf "$DEFAULT"
  fi

  printf ")? "
  read cmd

  if [ -z "$cmd" ] ; then
    cmd=$DEFAULT
  fi

  export "$1=$cmd"
}

# Create the test database (and directory, if needed)
create_tdb()
{
  config_arg DBROOT "Where do you want to create the test database" \
    "$HOME/qmtest_libstdcpp_$VERSION"
  echo $DBROOT | grep "^/" > /dev/null  2>&1 
  if [ $? -ne 0 ] ; then
    export DBROOT=$PWD/$DBROOT
  fi

  #XXX make sure the directory is accessible

  if [ -e $DBROOT -a ! -d $DBROOT ] ; then
    echo "$DBROOT already exists but is not a directory"
    exit 1
  fi

  if [ -d $DBROOT -a ! -w $DBROOT ] ; then
    echo "$DBROOT already exists but is not writable"
    exit 1
  fi

  if [ ! -d $DBROOT ] ; then
    mkdir $DBROOT
    if [ $? -ne 0 ]; then
      exit 1
    fi
  fi

  qmtest -D $DBROOT/$DBNAME create-tdb \
    -c v3_database.V3Database -a srcdir=$PKGROOT/testsuite
}

# Do run time configuration
config_run()
{
  DEFAULT_TRIPLET=`grep "^DejaGNUTest.target=" $PKGROOT/context | cut -d= -f2` 
  config_arg TRIPLET "What is the GNU triplet for your operating system" \
    "$DEFAULT_TRIPLET"

  DEFAULT_OPER=`grep "^VSX_OPER=" $PKGROOT/context | cut -d= -f2` 
  config_arg VSX_OPER "Name of person running tests" "$DEFAULT_OPER"

  DEFAULT_ORG=`grep "^VSX_ORG=" $PKGROOT/context | cut -d= -f2` 
  config_arg VSX_ORG "Organisation" "$DEFAULT_ORG"

  DEFAULT_SYS=`grep "^VSX_SYS=" $PKGROOT/context | cut -d= -f2` 
  config_arg VSX_SYS "Test System" "$DEFAULT_SYS"

}

# Check whether the necessary locales have been setup up
# (it_IT en_US de_DE en_HK de_DE@euro es_MX fr_FR en_PH fr_FR@euro)
check_locales()
{
  for Locale in it_IT en_US en_HK es_MX fr_FR en_PH fr_FR@euro 
  do
    TZ=UTC LC_ALL=$Locale date -f $PKGROOT/locale/designdate > \
      $DBROOT/.date.$Locale

    ret=`diff $DBROOT/.date.$Locale $PKGROOT/locale/date.$Locale`
    if [ $? -ne 0 ] ; then
      echo "Warning: the $Locale locale may not be set up correctly on your system:"
      printf "$ret\n"
      # Make it a warning instead of a failure for now
      #exit 1
      rm -f $DBROOT/.date.$Locale 
    fi
    rm -f $DBROOT/.date.$Locale 
  done

  # special-case the de_DE* locales, locale date format has changed 
  # but this does not affect the C++ tests (bug 1422)
  for Locale in de_DE de_DE@euro
  do
    TZ=UTC LC_ALL=$Locale date -f $PKGROOT/locale/designdate > \
      $DBROOT/.date.$Locale

    ret=`diff $DBROOT/.date.$Locale $PKGROOT/locale/date.$Locale`
    if [ $? -eq 0 ] ; then
      rm -f $DBROOT/.date.$Locale 
      continue
    fi

    # try alternate format
    ret2=`diff $DBROOT/.date.$Locale $PKGROOT/locale/date2.$Locale`
    if [ $? -eq 0 ] ; then
      rm -f $DBROOT/.date.$Locale 
      continue
    fi

    echo "Warning: the $Locale locale may not be set up correctly on your system:"
    printf "$ret\n"
    # Make it a warning instead of a failure for now
    #exit 1
    rm -f $DBROOT/.date.$Locale 
  done
}

#----------------------------------------------------------------------
# Main bit

create_tdb
check_locales
config_run

echo "----------------------------------------------------------------------"
echo "Executing the test suites"
echo

cd $DBROOT/$DBNAME
REPORT_NAME=journal.`date  +"%Y%m%d%H%M"` 

# If the user provides triplet, then use it
qmtest run -C $PKGROOT/context \
  -c DejaGNUTest.target="$TRIPLET" \
  -c VSX_OPER="$VSX_OPER" \
  -c VSX_ORG="$VSX_ORG" \
  -c VSX_SYS="$VSX_SYS" \
  --result-stream="tet_stream.TETStream(filename='$REPORT_NAME')"

echo ""

echo "Finished."
echo "A journal file has been created as: " $DBROOT/$DBNAME/$REPORT_NAME
echo

