#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright 2020,2024 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
#
# init a git tree full of just the changelog entries for that specific kernel release
#
# horrible hack of a shell script, use the .c version instead, it's faster by HOURS

HOME="/home/gregkh/linux"
TMPDIR="/home/gregkh/tmp"
PREFIX="fixes_search_"

REAL_SCRIPT=$(realpath -e ${BASH_SOURCE[0]})
SCRIPT_TOP="${SCRIPT_TOP:-$(dirname ${REAL_SCRIPT})}"

KERNEL_DIR="/home/gregkh/linux/stable/linux-stable"

source ${SCRIPT_TOP}/../lib/common


usage()
{
	echo "version numbers not found"
	echo "usage:"
	echo "	$0 OLD_KERNEL NEW_KERNEL"
	exit 1
}

OLD_VERSION=$1
NEW_VERSION=$2


if [ "${OLD_VERSION}" == "" ] ; then
	usage
fi

if [ "${NEW_VERSION}" == "" ] ; then
	usage
fi

pwd=$(pwd)

DB=${SCRIPT_TOP}/commits.db

# Build the DB!
sqlite3 ${DB} "CREATE TABLE if not EXISTS commits (id TEXT PRIMARY KEY, release TEXT NOT NULL, mainline_id TEXT);"

#logn "${txtgrn}Creating stats for data from version ${txtylw}${OLD_VERSION}${txtrst} to ${txtylw}${NEW_VERSION}${txtrst}	"
cd "${KERNEL_DIR}"

# is this a minor version?  If so, skip the ids list as we don't care about them
REL_ARRAY=(${NEW_VERSION//./ })
MINOR=${REL_ARRAY[2]}
MAJOR=${REL_ARRAY[0]}
if [[ "${MINOR}" == "" || "${MAJOR}" == "2" ]] ; then
	# test if version is already created
#	if [ -d "${pwd}/changes/${NEW_VERSION}/" ] ; then
#		#log "${txtred}files already created${txtrst}"
#		exit 1
#	fi
	present=$(sqlite3 ${DB} "SELECT id from commits where release='${NEW_VERSION}';")
	if [[ "${present}" != "" ]] ; then
		log "${txtred}${NEW_VERSION}${txtrst} already in database"
		exit 1
	fi

	log "${txtgrn}Creating stats for data from version ${txtylw}${OLD_VERSION}${txtrst} to ${txtylw}${NEW_VERSION}${txtrst}	"
#	mkdir "${pwd}/changes/${NEW_VERSION}"

	# not a minor version, a "Major" one, so generate the ids and the changes for it
	#git rev-list --reverse --no-merges v${OLD_VERSION}..v${NEW_VERSION} > "${pwd}/ids/${NEW_VERSION}"

	for commit in $(git rev-list --reverse --no-merges v${OLD_VERSION}..v${NEW_VERSION}); do
		#log "${txtgrn}${commit}${txtrst}	"
		#spin
		#echo "sqlite3 commits.db \"INSERT INTO commits (id, release) VALUES ('${commit}', '${NEW_VERSION}');\""
		sqlite3 ${DB} "INSERT INTO commits (id, release) VALUES ('${commit}', '${NEW_VERSION}');"
#		git log -1 --format='%B' "${commit}" > "${pwd}/changes/"/"${NEW_VERSION}"/${commit}
	done
else
	present=$(sqlite3 ${DB} "SELECT id from commits where release='${NEW_VERSION}';")
	if [[ "${present}" != "" ]] ; then
		log "${txtred}${NEW_VERSION}${txtrst} already in database"
		exit 1
	fi
	log "${txtgrn}Creating stats for data from version ${txtylw}${OLD_VERSION}${txtrst} to ${txtylw}${NEW_VERSION}${txtrst}	"
	for commit in $(git rev-list --reverse --no-merges v${OLD_VERSION}..v${NEW_VERSION}); do
		#log "${txtgrn}${commit}${txtrst}	"
		#spin
		mainlinesha=$(git --no-pager log -n1 ${commit} | grep -i upstream | grep -oE "[a-f0-9]{40,}")
		#echo "sqlite3 commits.db \"INSERT INTO commits (id, release) VALUES ('${commit}', '${NEW_VERSION}');\""
		sqlite3 ${SCRIPT_TOP}/commits.db "INSERT INTO commits (id, release, mainline_id) VALUES ('${commit}', '${NEW_VERSION}', '${mainlinesha}');" 
#		git log -1 --format='%B' "${commit}" > "${pwd}/changes/"/"${NEW_VERSION}"/${commit}
	done
#	# it is a minor verison, so only create a log which will let us "catch" what commits were backported to what version
#	git log v${OLD_VERSION}..v${NEW_VERSION} > "${pwd}/releases/${NEW_VERSION}"
fi
log "${txtpur}files created!${txtrst}"

cd "${pwd}"

