#!/bin/bash

# example: bash /data1/paul/kmax/scripts/krepair_evaluation/randconfig_test.sh patchconditions build_targets.json inputs/linux0 7585-400086d7c113 test_randtest/7585-400086d7c113 x86_64

set -x

if [ "$#" -gte 6 ]; then
  echo "Illegal number of parameters"
  exit -1
fi

# set path for libisl.so.22, needed to build the kernel
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/.local/lib

# wget https://github.com/appleseedlab/plocalizer/blob/master/assets/final_patch_conditions_v512_v513.tar.gz
# mkdir patchconditions; tar -C patchconditions -xvf final_patch_conditions_v512_v513.tar.gz
# folder structure: archname/patchname.cond
patchconds=$1

# mapping of compilation units to their kbuild patch
# passed to validate script for every patch
# https://github.com/appleseedlab/plocalizer/blob/master/assets/build_targets.json
build_targets=$2

# this is an already-cloned linux source directory that only this run
# of the script will use
linuxsrclone=$3

# the patchname, i.e., "PATCHNUMBER-COMMIT"
patchname=$4

# this is the directory where output and intermediate files go
# providing a name of a nonexistant directory (since it's created)
# structure: validation_wrapper.sh's output, plus patchname/config_count
outdir=$5

# the architecture to run on
arch=$6

# (optional) number of randconfigs to test
numconfigs=${7:-10}

# # make -j number of threads
# j=4

# It will create a directory ./ploc_approx_allno_results_<arch>/
# Within the directory, it contains:
# - ./(<patchname>/: stores the output generated by plocalizer for given patch
# - ./logs/: stores the stderr and stdout of running plocalizer for each patch
# - ./results/: stores the log of evaluation on plocalizer outputs for patch
# - ./gen_logs/: stores the log of gen_patch_files for each patch
# - ./cond_logs/: stores the log of loc_patch_condtitions for each patch
# - ./conf_logs/: stores the log of loc_patch_configs for each patch

# used to find other scripts called
script_dir=$(dirname $(realpath $0))

if [ -d $outdir ]; then
  echo "ERROR: output directory already exists"
  exit 1
else
  mkdir -p $outdir
fi

outdir=$(realpath $outdir)

commit="$(echo $patchname | cut -d - -f 2)"
patchconditionfile="${patchconds}/${arch}/${patchname}.cond"

if [ ! -f "${patchconditionfile}" ]; then
	echo "ERROR: expected patch condition: ${patchconditionfile}"
	exit 1
fi

# checkout to the patch's associated commit
(cd ${linuxsrclone}; git checkout -f $commit)

do_testrand () {
    arch=$1
    outdir=$2

    echo "${patchname} testing randconfig"
    # koverage takes a single config, but we may have multiple ones
    # $configs handle multiple configs dir for repaired configs

    for randnum in $(seq 1 ${numconfigs}); do
	randtestoutdir=${outdir}/${randnum}
	mkdir ${randtestoutdir}
	timefile=${randtestoutdir}/time
 	outfile=${randtestoutdir}/out
	config=${randtestoutdir}/config
	(cd ${linuxsrclone}; git clean -dfx)
	(cd ${linuxsrclone}; make ARCH=${arch} randconfig)
	cp ${linuxsrclone}/.config ${config}

	scratchdir=${randtestoutdir}/scratch
	/usr/bin/time -f %e -o ${timefile} koverage --config ${config} --arch ${arch} --linux-ksrc ${linuxsrclone} --check-covreq $patchconditionfile --build-targets $build_targets --scratch-dir $scratchdir -o $outfile
    done
}

do_testrand ${arch} ${outdir}
