#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0-only

# Keep track of the number of builds and errors
nb_warnings=0
warnings_list=""
nb_errors=0
errors_list=""
test_errors_list=""
nb_defconfigs=0
nb_tests=0
nb_tests_failed=0
exitcode=0
logdir="log"

time_start=$(date +%s)

filename=$(basename $0)

#-----------------------------------------------------------------------

usage() {
	echo "Usage: ${filename} [OPTION]..."
	echo "Barebox MAKEALL tools."
	echo ""
	echo "it's allow you to compile specific defconfig or ARCH or all"
	echo "as"
	echo ""
	echo "CROSS_COMPILE=arm-linux- ARCH=arm ./MAKEALL at91sam9263ek_defconfig"
	echo "CROSS_COMPILE=arm-linux- ARCH=arm ./MAKEALL"
	echo ""
	echo "The cross-compiler can be specify via"
	echo "    CROSS_COMPILE               default"
	echo "    CROSS_COMPILE_<arch>        arch default"
	echo "    CROSS_COMPILE_<defconfig>   defconfig specific"
	echo ""
	echo "it will be evaluated in the invert order"
	echo ""
	echo "or via config"
	echo ""
	echo "you can specify it via env CONFIG or option -c (overwrite env)"
	echo ""
	echo "CONFIG=./MAKEALL.cfg ARCH=arm ./MAKEALL at91sam9263ek_defconfig"
	echo "CONFIG=./MAKEALL.cfg ARCH=arm ./MAKEALL"
	echo ""
	echo "and for all"
	echo ""
	echo "CONFIG=./MAKEALL.cfg ./MAKEALL"
	echo ""
	echo "you can specify via env or option"
	echo "env         option"
	echo "ARCH        -a      arch"
	echo "CONFIG      -c      config"
	echo "JOBS        -j      jobs"
	echo "BUILDDIR    -O      build dir"
	echo "LOGDIR      -l      log dir"
	echo "REGEX       -e      regex"
	echo "KCONFIG_ADD -k      kconfig fragment or option"
	echo "TARGET      -t      Makefile target"
	echo "V           -v      verbosity"
	echo "INCREMENTAL -i"
	echo ""
}

stats() {
	echo ""
	echo "--------------------- SUMMARY ----------------------------"
	echo "defconfigs compiled: ${nb_defconfigs}"
	if [ ${nb_errors} -gt 0 ] ; then
		echo -e "\tdefconfigs with errors:   ${nb_errors} (${errors_list} )"
	fi
	if [ ${nb_warnings} -gt 0 ] ; then
		echo -e "\tdefconfigs with warnings: ${nb_warnings} (${warnings_list} )"
	fi
	if [ ${nb_tests} -gt 0 ]; then
		echo "defconfigs tested:   ${nb_tests}"
		if [ "${nb_tests_failed}" -gt 0 ]; then
			echo -e "\tdefconfigs with errors:   ${nb_tests_failed} (${test_errors_list} )"
		fi
	fi
	time_stop=$(date +%s)
	time_diff=$((${time_stop} - ${time_start}))
	printf "Total time spent: %4is\n" ${time_diff}
	echo "----------------------------------------------------------"

	exit ${exitcode}
}

check_pipe_status() {
	for i in "${PIPESTATUS[@]}"
	do
		[ $i -gt 0 ] && return 1
	done
	return 0
}

with_logs_collected() {
	local log_report="${logdir}/${defconfig}/report.log"
	local log_err="${logdir}/${defconfig}/errors.log"

	if [ -n "${logdir}" ]; then
		"$@" 2>&1 > "${log_report}" | tee "${log_err}"
		check_pipe_status
	else
		"$@"
	fi

	return $?
}

report() {
	local log_report="${logdir}/${defconfig}/report.log"

	if [ -n "${logdir}" ]; then
		printf "$@" | tee -a "${log_report}"
	else
		printf "$@"
	fi
}

merge_config() {
	with_logs_collected ./scripts/kconfig/merge_config.sh -m -O \
		${BUILDDIR} "$@"
}

do_build_defconfig() {
	local arch=$1
	local defconfig=$2
	local step_time_start=$(date +%s)
	local log_err="${logdir}/${defconfig}/errors.log"
	local err=0

	[ "$INCREMENTAL" != "1" ] && rm -rf "${BUILDDIR}"
	[ -n "$logdir" ] && mkdir -p "${logdir}/${defconfig}"

	MAKE="make -j${JOBS} ARCH=${arch} O=${BUILDDIR}"
	${MAKE} ${defconfig} &>/dev/null

	if [ ${arch} = "arm" ]; then
		grep -q "CONFIG_ARM64=y" ${BUILDDIR}/.config
		if [ $? = 0 ]; then
			arch=arm64
		fi
	fi

	tmp=$(echo "${defconfig}" | tr - _)

	cross_compile=$(eval echo '$CROSS_COMPILE_'${tmp})
	cross_compile_set=$(eval echo '${CROSS_COMPILE_'${tmp}'+set}')
	if [ "${cross_compile_set}" = "" ]
	then
		cross_compile=$(eval echo '$CROSS_COMPILE_'${arch})
		cross_compile_set=$(eval echo '${CROSS_COMPILE_'${arch}'+set}')
		if [ "${cross_compile_set}" = "" ]
		then
			cross_compile=${CROSS_COMPILE}
		fi
	fi

	[ -z "$V" ] && silent_flag=-s

	report "Building ${arch} ${defconfig} \n" >&2
	MAKE="${MAKE} $silent_flag CROSS_COMPILE=${cross_compile}"
	with_logs_collected ${MAKE} ${defconfig}
	for i in ${KCONFIG_ADD}; do
		if [[ $i =~ ^CONFIG_.*=.* ]]; then
			merge_config ${BUILDDIR}/.config <(echo $i)
		else
			merge_config ${BUILDDIR}/.config $i
		fi
	done
	with_logs_collected ${MAKE} $silent_flag olddefconfig

	configure_result="$?"

	report "Configure: "

	if [ "$configure_result" = "0" ]; then
		report "OK     \n"

		with_logs_collected ${MAKE} $silent_flag ${TARGET}
		compile_result="$?"

		report "Compile: " ${defconfig}

		if [ "$compile_result" = "0" ]; then
			report "OK     \n"
		else
			report "FAILED \n"
			nb_errors=$((nb_errors + 1))
			errors_list="${errors_list} ${defconfig}"
			err=1
			exitcode=1
		fi
	else
		report "FAILED \n"
		report "Compile: ------ \n"
		err=1
		exitcode=1
	fi

	if [ -n "$logdir" ]; then
		if [ -s "${log_err}" ] ; then
			nb_warnings=$((nb_warnings + 1))
			warnings_list="${warnings_list} ${defconfig}"
		else
			rm "${log_err}"
		fi
	fi

	nb_defconfigs=$((nb_defconfigs + 1))

	step_time_stop=$(date +%s)
	step_time_diff=$((${step_time_stop} - ${step_time_start}))
	report "Compiled in %4is\n" ${step_time_diff}

	return $err
}

if command -v labgrid-pytest >/dev/null; then
	pytest=labgrid-pytest
else
	pytest=pytest
fi

do_test_defconfig() {
	local yaml=$1
	local defconfig=$2
	shift 2
	local step_time_start=$(date +%s)
	local err=0

	LG_BUILDDIR=$BUILDDIR with_logs_collected $pytest --lg-env $yaml "$@"
	compile_result="$?"

	report "Test: " ${yaml}

	if [ "$compile_result" = "0" ]; then
		report "OK     \n"
	else
		report "FAILED \n"
		nb_tests_failed=$((nb_tests_failed + 1))
		test_errors_list="${test_errors_list} ${yaml}"
		exitcode=1
		err=1
	fi

	nb_tests=$((nb_tests + 1))

	step_time_stop=$(date +%s)
	step_time_diff=$((${step_time_stop} - ${step_time_start}))
	report "Tested in %4is\n" ${step_time_diff}

	return $err
}

do_build() {
	local arch=$1
	local regex=$2

	configs=$(find arch/${arch}/configs -name "${regex}_defconfig" | sort)
	for i in ${configs}; do
		local defconfig=$(basename $i)

		do_build_defconfig ${arch} ${defconfig}
	done
}

do_build_all() {
	local nbuilds=0

	for i in arch/*
	do
		local arch=$(basename $i)

		if [ -d $i ]
		then
			do_build ${arch} "*"
			nbuilds=$((nbuilds + 1))
		fi
	done

	return $nbuilds
}

while getopts "hc:j:O:l:a:e:k:t:v:i" Option
do
case $Option in
	a )
		ARCH=${OPTARG}
		;;
	c )
		CONFIG=${OPTARG}
		;;
	j )
		JOBS=${OPTARG}
		;;
	l )
		LOGDIR=${OPTARG}
		;;
	O )
		BUILDDIR=${OPTARG}
		;;
	e )
		REGEX=${OPTARG}
		;;
	k )
		KCONFIG_ADD="${KCONFIG_ADD} ${OPTARG}"
		;;
	t )
		TARGET="${TARGET} ${OPTARG}"
		;;
	v )
		export V=${OPTARG}
		;;
	i )
		INCREMENTAL=1
		;;
	h )
		usage
		exit 0
		;;
esac
done

shift $((OPTIND - 1))

if [ ! "${JOBS}" ] ; then
	#linux, BSD, MacOS
	nb_cpu=`getconf _NPROCESSORS_ONLN`

	if [ $? -gt 0 ]
	then
		nb_cpu=1
	fi

	JOBS=$((${nb_cpu} * 2))
fi

if [ -v LOGDIR ];
then
	logdir="$LOGDIR"
fi

if [ ! "${BUILDDIR}" ]
then
	BUILDDIR="makeall_builddir"
fi

if [ "${CONFIG}" ]
then
	basedir=$(dirname ${CONFIG})

	if [ ! "${basedir}" ] || [ "${basedir}" = "." ]
	then
		CONFIG="./${CONFIG}"
	fi

	. "${CONFIG}"
fi

if [ -n "$logdir" ] && [ ! -d "${logdir}" ]
then
	mkdir "${logdir}" || exit 1
fi

if [ ! "${REGEX}" ]
then
	REGEX="*"
fi

if [ $# -eq 0 ]
then
	if [ ! "${ARCH}" ] || [ ! -d arch/${ARCH} ]
	then
		do_build_all
		if [ $? -eq 0 ]
		then
			echo "You need to specify the ARCH or CROSS_COMPILE_<arch> or CROSS_COMPILE_<defconfig> in your config file"
			usage
			exit 1
		fi
		exit 0
	fi

	do_build ${ARCH} "${REGEX}"
else
	declare -a configs=()
	declare -a pytest_opts=()
	for i in $*; do
		if [[ "$i" = "-"* ]] || [ ${#pytest_opts[@]} -gt 0 ]; then
			pytest_opts+=($i)
			continue;
		fi

		skip=0

		# test/*/ may contain local symlinks, so resolve them
		for j in "${configs[@]}"; do
			if [ "$i" = "$j" ]; then
				skip=1
				break
			fi

			# drop duplicates, e.g. via globbing in directory with symlinks
			if [[ $i =~ / && $j =~ / &&
			   "$(readlink -f $i)" == "$(readlink -f $j)" ]]; then
				skip=1
				break
			fi
		done

		if [ $skip = 1 ]; then
			continue;
		fi

		configs+=($i)
	done
	for i in "${configs[@]}"; do
		config=$i
		if [[ $i =~ ^.*/([^/]+)/([^@]*@|)([^.]+).yaml$ ]]; then
			arch=${BASH_REMATCH[1]}
			defconfig=${BASH_REMATCH[3]}

			if grep -q "^\s\+kconfig_add:" $i; then
				if command -v yq 2>&1 >/dev/null; then
					KCONFIG_ADD="${KCONFIG_ADD} $(yq -M -r \
					'.targets.main.runner.kconfig_add |
					keys[] as $k | .[$k]' < $i)"
				else
					echo "WARNING: yq command not found in path" >&2
					echo "WARNING: ignoring kconfig_add in $i" >&2
					echo
				fi
			fi

			do_build_defconfig $arch $defconfig
			if [ $? -eq 0 ]; then
				do_test_defconfig $config $defconfig "${pytest_opts[@]}"
			else
				echo "Skipping test due to failed build"
			fi
		else
			do_build_defconfig ${ARCH} $config
		fi
	done
fi

stats

exit ${nb_errors}
