#!/bin/bash

. $LKP_SRC/lib/email.sh
. $LKP_SRC/lib/log.sh
. $LKP_SRC/lib/upload.sh

source /etc/profile.d/compass.sh
do-local-pack

group_id=selftest
tmpdir=$(mktemp -d)
log_file="$tmpdir"/submit_cmd

cci_pkg_deps=(	"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=reaim"
		"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=pixz"
		"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=iozone"
		"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=hackbench"
		"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=netpipe"
		"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=pft"
		"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=ku-latency"
		"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=redis-server"
		"testbox=vm-2p16g cci-makepkg.yaml cci-makepkg.benchmark=hackben-git"
		"testbox=dc-8g    cci-depends.yaml cci-depends.benchmark=sshd"
		"testbox=vm-2p8g  cci-depends.yaml cci-depends.benchmark=pft"
		"testbox=vm-2p8g  cci-depends.yaml cci-depends.benchmark=reaim"
		"testbox=vm-2p8g  cci-depends.yaml cci-depends.benchmark=pixz"
		"testbox=vm-2p8g  cci-depends.yaml cci-depends.benchmark=netpipe"
		"testbox=vm-2p8g  cci-depends.yaml cci-depends.benchmark=numactl"
		"testbox=vm-2p8g  cci-depends.yaml cci-depends.benchmark=fs"
)

cases=( "testbox=dc-8g   borrow-1h.yaml     runtime=10s"
        "testbox=vm-2p8g borrow-1h.yaml     runtime=10s"
        "testbox=vm-2p8g reaim.yaml         reaim.test=aim9 runtime=10s"
        "testbox=vm-2p8g pixz.yaml          nr_threads=100% runtime=10s"
        "testbox=vm-2p8g iozone-bs.yaml     block_size=64k  runtime=10s"
        "testbox=vm-2p8g hackbench-git.yaml nr_threads=1    hackbench-git.mode=process hackbench-git.ipc=pipe runtime=10s"
        "testbox=vm-2p8g host-info.yaml     runtime=10s"
        "testbox=vm-2p8g kunit.yaml         runtime=10s"
        "testbox=vm-2p8g netpipe.yaml       runtime=10s"
        "testbox=vm-2p8g redis.yaml         runtime=10s"
        "testbox=vm-2p8g ku-latency.yaml    runtime=10s"
        "testbox=vm-2p8g pft.yaml           nr_task=50%     runtime=10s"
)

check_submit_result()
{
	local cmd=$1
	submit_result=$(eval "$cmd")
	id=$(echo "$submit_result" | grep -o "${lab}\.[0-9]*" | uniq)
	[ -n "$id" ] || {
		eval echo "$cmd" >> "$log_file"
		log_error "$submit_result"
	}
}

submit_pkg_deps()
{
	for args in "${cci_pkg_deps[@]}"
	do
		(
			cmd='submit -m $args group_id=$group_id --no-pack'
			check_submit_result "$cmd"
		) &
	done
}

submit_cases()
{
	for args in "${cases[@]}"
	do
		(
			cmd='submit -m $args group_id=$group_id --no-pack'
			check_submit_result "$cmd"
		) &
	done
}

submit_all()
{
	submit_pkg_deps
	wait
	submit_cases
	wait
	touch "$tmpdir"/job_finished
}

get_es_results()
{
	local job_field=$1
	local skip_result=$2

	es-jobs group_id="$group_id" | grep "$job_field" | grep "raw.id" | grep -v "$skip_result" > "$tmpdir"/"$job_field"
	[ -s "$tmpdir"/"$job_field" ] && {
		while read es_result
		do
		        echo "$es_result" | grep -o "raw.id.\[[^\[\]*\]:"
			ids=$(echo "$es_result" | grep -o "$lab.[0-9]*")
			for id in $ids
			do
				echo /srv"$result_root"/"$id"
		        done
		        echo -e "\n"
		done < "$tmpdir"/"$job_field"
	}
}

get_report_content()
{
	get_es_results job_health success
	get_es_results job_stage finish

	[ -s "$log_file" ] && {
		sed -i '1i\submit failed:' "$log_file"
		cat "$log_file"
	}
}

get_commit_info()
{
	cd /c/compass-ci || exit

	commit_id=$(git log -1 --format="%H")
	commit_subject=$(git log -1 --format="%s")

	author_name=$(git log -1 --format="%an")
	author_email=$(git log -1 --format="%ae")

	committer=$(git log -1 --format="%cn")
	committer_email=$(git log -1 --format="%ce")

	recipient_email="$author_email;$committer_email"
}

upload_failed_result()
{
	lab=$(awk '/^lab:\s/ {print $2; exit}' /etc/compass-ci/defaults/*.yaml)
	job_ids=$(find /srv/result -name "$lab.*"| awk -F'/' '{print $NF}')
	for job_id in $job_ids
	do
		result_root=/srv$(es-find id=$job_id | grep result_root | cut -d'"' -f4)
		es-find id=$job_id| grep job_health| grep -q "success" || upload_files -t $job_id $result_root/*
	done
}

monitor_jobs()
{
	for times in {1..8}
	do
		[ -f "$tmpdir/job_finished" ] && return
		sleep 15m
	done
}

submit_all &
monitor_jobs
get_commit_info
report_content=$(get_report_content)
send_email selftest
upload_failed_result
rm -rf "$tmpdir"
