#!/bin/bash

# check if testbox has enough memory for bp_memmap

source $LKP_SRC/lib/unit.sh

if [ $boot_params_bp_memmap ]; then
	reserve_memory=$(echo $boot_params_bp_memmap | awk -F '!' '{print $1}' | awk -F 'G' '{print $1}')
	start_memory=$(echo $boot_params_bp_memmap | awk -F '!' '{print $2}' | awk -F 'G' '{print $1}')
	boot_need_memory=$(( reserve_memory + start_memory ))
fi

# boot_params_bp1_memmap boot_params_bp2_memmap-boot_params_bp3_memmap boot_params_bp4_memmap
boot_need_memory=${boot_need_memory:-0}
for i in $(seq 1 8);
do
	bp_memmap_var="echo \${boot_params_bp${i}_memmap}"
	bp_memmap=$(eval $bp_memmap_var)
	[ -z "$bp_memmap" ] && continue

	reserve_memory=$(echo $bp_memmap| awk -F '!' '{print $1}' | awk -F 'G' '{print $1}')
	start_memory=$(echo $bp_memmap | awk -F '!' '{print $2}' | awk -F 'G' '{print $1}')
	boot_need_memory_var=$(( reserve_memory + start_memory ))
	[ $boot_need_memory_var -gt $boot_need_memory ] && boot_need_memory=$boot_need_memory_var
done

memory_gb=$(to_gb $memory)

if (( boot_need_memory > memory_gb )); then
	echo "rm $job_file due to not enough memory"
	echo "need: $boot_need_memory G, has: $memory_gb G"
	exit 1
fi

exit 0
