#!/bin/bash

ret=0
TASK_AND_BUG_ID_LIST=""
SEVERITY_LEVEL="Low"
CC_STABLE=""
K2CI_ARCH="K2CI-Arch: All"

function do_exit()
{
	if [ $ret == 0 ];then
		echo -e "\033[32mSucessed.\033[0m"
	else
		echo -e "\033[34mSomething wrong, please check it.\033[0m"
	fi
	exit $1
}

function usage()
{
	echo -e "`basename $0` [OPTION]... upstream-comit-id"
	echo ""
	echo -e "  -h, --help\t\t\tshow this help info"
	echo -e "  -i, --insert-head-only\treword commit body only, without edit patch"
	echo -e "  -c, --cve\t\t\trequire cve id, insert to commit body"
	echo -e "  -b, --bug\t\t\trequire bug id, insert to commit body"
	echo -e "  -t, --task\t\t\trequire task id, insertt to commit body"
	echo -e "  -s, --stable\t\t\tlet us CC kernel stable team, need backport to stable tree"
	echo -e "  -a, --k2ci-arch\t\tuse for K2CI-Arch, None/Amd64/Arm64/Loongarch..."
	echo ""
	echo "Examples:"
	echo "  sync-stable -c CVE-2022-23456 -b 10243 -t 6009 -a \"Arm64|Amd64\" upstream-commit-id"
}

if [ $# -lt 1 ]; then
	usage
        exit -1
fi

ARGS=`getopt -o hib:t:c:sa: -a --long help,insert-head-only,cve:,bug:,task:,stable,k2ci-arch -- "$@"`
if [ $? != 0 ]; then
        echo "Terminating..."
        usage && exit 1
fi

eval set -- "${ARGS}"

while true
do
	case $1 in
		-h|--help)
			usage
			exit 0;;
		-c|--cve)
			CVEID="CVE: $2"
			SEVERITY_LEVEL="Important"
			CC_STABLE="%nCc: kernel-stable-team # STABLE-TREE-NEED"
			shift 2;;
		-i|--insert-head-only)
			IHO="true";
			shift 1;;
		-b|--bug)
			TASK_AND_BUG_ID_LIST="${TASK_AND_BUG_ID_LIST}bug: $2%n"
			SEVERITY_LEVEL="Moderate"
			shift 2;;
		-t|--task)
			TASK_AND_BUG_ID_LIST="${TASK_AND_BUG_ID_LIST}task: $2%n"
			SEVERITY_LEVEL="Moderate"
			shift 2;;
		-s|--stable)
			SEVERITY_LEVEL="Important"
			CC_STABLE="%nCc: kernel-stable-team # STABLE-TREE-NEED"
			shift 1;;
		-a|--k2ci-arch)
			K2CI_ARCH="K2CI-Arch: $2"
			shift 2;;
		--)
			shift
			break;;
	esac
done

comment=$(git show --stat $1)
if [ $? != 0 ]; then
	echo "Not found commit"
	ret=-2
	do_exit -1;
fi

# test in tree ?
$(dirname $0)/stable-tools/stable commit-in-tree $1
if [ $? == 1 -a x"$IHO" != x"true" ]; then
	echo -e "\033[32mbackported before, not need.\033[0m"
	exit 0
fi

# Commit from stable tree ?
string=$(git show --stat $1 | grep -i upstream)
if [ $? == 0 ]; then
	result=$(echo $string | grep "\[ Upstream")
	if [[ "$result" != "" ]]
	then
		# [ Upstream commit d518691cbd3be3dae218e05cca3f3fc9b2f1aa77 ]
		mainline_commit=$(echo $string | awk '{print $4}')
	else
		# commit 4bf584a03eec674975ee9fe36c8583d9d470dab1 upstream.
		for sha in $(echo $string | awk '{print $2}')
		do
			if [ 40 == `echo $sha | wc -L` ]; then
				mainline_commit=$sha
			fi
		done

		if [ x"$mainline_commit" == x"" ]; then
			mainline_commit=$1
			echo "find fake upstream commit, use your commit."
		fi
	fi
else
	mainline_commit=$1
fi

mainline_commit=$(git rev-parse $mainline_commit)

# Now get the mainline commit
tag=$(git tag --sort=taggerdate --contains $mainline_commit | grep "^v" | head -n 1)
if [ x"$tag" == x"" ];then
	echo -e "\033[31mNot found tag, please note.\033[0m"
	ret=-2
fi
if [ x"$IHO" != x"true" ]; then
	git cherry-pick $1
	if [ $? != 0 ]; then
		ret=-1
		COMMIT_ID=$1
	else
		COMMIT_ID=`git rev-parse HEAD`
	fi
else
	COMMIT_ID=$1
fi
if [ $ret == 0 ] || [ $ret == -2 ]; then
	msg=$(git log -1 --format="%s%n\
%nMainline: $mainline_commit%n\
From: $tag%n\
Severity: ${SEVERITY_LEVEL}%n\
${CVEID}%n\
%n${TASK_AND_BUG_ID_LIST}%n\
%n%b\
(backported from commit $(git rev-parse $1))%n\
%n${K2CI_ARCH}%n\
${CC_STABLE}" ${COMMIT_ID})
	git commit -s --amend -m "$msg"
fi

do_exit
