#!/bin/bash

# example: bash build_repair.sh ~/tmp/test_output/14522-210f563b0979 ~/src/linux 14522-210f563b0979 ~/tmp/build_repair/14522-210f563b0979 x86_64
# example: bash /data1/paul/kmax/scripts/krepair_evaluation/build_patch.sh aggregated/repair/7585-400086d7c113 inputs/linux0 7585-400086d7 c113 test_build_repair/7585-400086d7c113 x86_64

# example: experiment@church:/data1/test_experiment$ # for i in {0..3}; do bash /data1/paul/kmax/scripts/krepair_evaluation/build_client.sh localhost 45678 /data2/test_experiment/aggregated build_patch inputs/linux$i x86_64 > build_patch$i 2>&1 & sleep 1; done

set -x

if [ "$#" -ne 5 ]; 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

# this is the output of krepair
repairout=$1

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

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

# 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=$4

# # we are running on x86_64 only right now
# arch=x86_64
arch=$5

# # 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)"

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

do_build () {
    configs=$1
    arch=$2
    buildoutdir=$3
    makeflags=$4

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

    find $configs | egrep "\.config$|configtorepair$" | while IFS= read -r config; do
	configfilename=$(basename $config)
	timefile=$buildoutdir/$configfilename.time
	outfile=$buildoutdir/$configfilename.out
	(cd ${linuxsrclone}; git clean -dfx)
	cp $config ${linuxsrclone}/.config
	(cd ${linuxsrclone}; make olddefconfig |& tee ${outfile})
	(cd ${linuxsrclone}; /usr/bin/time -f %e -o $timefile make ARCH=${arch} |& tee ${outfile})
    done
}

# do_build ${repairout}/allnoconfig/configs ${arch} $outdir/build_results_allnoconfig_after_makej8 "-j 8"
do_build ${repairout}/defconfig/configs ${arch} $outdir/build_results_defconfig_after_makej8 "-j 8"
do_build ${repairout}/allyesconfig/configtorepair ${arch} $outdir/build_results_allyesconfig_before_makej8 "-j 8"

# do_build ${repairout}/allnoconfig/configs ${arch} $outdir/build_results_allnoconfig_after
# do_build ${repairout}/defconfig/configs ${arch} $outdir/build_results_defconfig_after
# do_build ${repairout}/allyesconfig/configtorepair ${arch} $outdir/build_results_allyesconfig_before
