#!/bin/bash

# My standard work directory. Results of runs are in sub directories.
BATCH_WORKDIR="storage"
mkdir $BATCH_WORKDIR

# My standard var directory on tmpfs. Ongoing RQG runs are in sub directories.
BATCH_VARDIR="/dev/shm/vardir"
mkdir $BATCH_VARDIR


# Go with heavy load
PARALLEL=`nproc`
PARALLEL=$(($PARALLEL + $PARALLEL / 2))

TRIALS=$(($PARALLEL * 4))
TRIALS=64
# TRIALS=200

# TRIALS=1
# PARALLEL=1

# Various settings for the server+InnoDB etc.
# including settings for avoiding open bugs.
CONFIG=uni.cfg
CONFIG=$1

if [ ! -e "$CONFIG" ]
then
   echo "The config file '$CONFIG' does not exist."
   echo "Please adjust the setting in $0"
   exit
fi

CASE=`basename $CONFIG .cfg`
PROT="$CASE"".prt"

# One 'God' only
# --------------
# in order to avoid "ill" runs where
# - current rqg_batch run ---- other ongoing rqg_batch run
# - current rqg_batch run ---- ongoing MTR run
# clash on the same resources (vardir, ports -> MTR_BUILD_THREAD) or
# suffer from tmpfs full etc.
killall -9 perl ; killall -9 mysqld
rm -rf $BATCH_VARDIR/* rm -rf /dev/shm/var_*

# Path to MariaDB binaries
BASEDIR1="$2"
if [ "$BASEDIR1" = "" ]
then
   echo "You need to assign a basedir == path to MariaDB binaries like '/work_m/bb-10.2-marko/bld_asan'"
   echo "Example:         ./NewInnoDB.sh /work_m/bb-10.2-marko/bld_asan"
   exit
fi
if [ ! -d "$BASEDIR1" ]
then
   echo "BASEDIR1 '$BASEDIR1' does not exist."
   exit
fi
BASEDIR2="$BASEDIR1"

set -o pipefail
# --run-all-combinations-once                                     \
# --grammar=$GRAMMAR                                               \
# --threads=1                                                    \
# --dryrun                                                       \
# --run-all-combinations-once                                     \
# --discard_logs Remove the logs of RQG runs achieving STATUS_OK/verdict 'ignore'
# --force    Do not abort if hitting Perl errors or STATUS_ENVIRONMENT_FAILURE. Unclear if good.
# --no-mask  Unclear if ./rqg_batch.pl and rqg.pl will mask at all.
# --type=RQG_Simplifier                                             \
# --dryrun=ignore                                                \
# --dryrun=replay                                                \
# --stop_on_replay                                                \
# --trials=$TRIALS                                                \
# --script_debug=_all_                                            \
# --basedir2=$BASEDIR2                                                   \
# --grammar=$GRAMMAR                                              \
  GRAMMAR=MDEV-20090.yy
  vi $GRAMMAR
# perl -w -d:ptkdb ./rqg_batch.pl                                          \
nohup perl -w ./rqg_batch.pl                                           \
--workdir=$BATCH_WORKDIR                                               \
--vardir=$BATCH_VARDIR                                                 \
--parallel=$PARALLEL                                                   \
--basedir1=$BASEDIR1                                                   \
--config=$CONFIG                                                       \
  --stop_on_replay                                                \
  --grammar=$GRAMMAR                                              \
  --trials=$TRIALS                                                     \
  --discard_logs                                                       \
  --threads=3                                                    \
--type=RQG_Simplifier                                                  \
--no-mask                                                              \
> $PROT 2>&1 &
tail -f $PROT


