#!/bin/bash


set -e -u

S="/home/deseessm/wp/src/canio"
opwd=$PWD


splitCode()
{
    V="${1#v*}"
    MAJOR="${V%%.*}"
    V="${V#*.}"
    MINOR="${V%%.*}"
    V="${V#*.}"
    PATCH="${V%%-*}"
    V="${V#*-}"

}

getVersionCode()
{
    let code="( $1 * 1000000 ) + ( $2 * 1000 ) + $3"
    echo $code
}

getChangelogRange()
{
    echo "### Get ranged changelog for $1: $2 ... $3"

    echo "### Ranged Chagelog for $(basename $1): $2 ... $3" >> $opwd/subliments.txt
    splitCode $2
    echo "   Major: $MAJOR; Minor: $MINOR; Patch: $PATCH"
    startCode=$(getVersionCode $MAJOR $MINOR $PATCH)
    
    splitCode $3
    echo "   Major: $MAJOR; Minor: $MINOR; Patch: $PATCH"
    endCode=$(getVersionCode $MAJOR $MINOR $PATCH)

    echo "Start code: $startCode"
    echo "end code: $endCode"
    cd $opwd
    mkdir -p $(basename $1)
    cd $(basename $1)

    csplit $1/CHANGELOG.md "/# Changes/" '{*}' -f "cl_"

    rm cl_00
    cnt=0

    for f in cl_*; do
	l1=$(head -n 1 $f)
	echo "$f: $l1"
	l2=$(echo "$l1" | grep -oe "v[0-9]*\.[0-9]*\.[0-9]*$")
	TAG="$l2"

	if [ "$cnt" == "0" ]; then
	    CURRENT=$l2
        fi
	if [ "$cnt" == "1" ]; then
	    PREVIOUS=$l2
        fi

	splitCode $TAG
	thisCode=$(getVersionCode $MAJOR $MINOR $PATCH)

	if [ "$thisCode" -ge "$startCode" ] && [ "$thisCode" -le "$endCode" ]; then
	    echo "### GOAL"
	    cat $f >> $opwd/subliments.txt
	fi

        echo "   Major: $MAJOR; Minor: $MINOR; Patch: $PATCH"
	let cnt+=1
    # sed -e " v.*\..*\..*\.$"
    done

}

parseSubmodule()
{
    cd $S
    # cd $submodule
    echo "### Parsing $submodule [2]"
    sta=0
    git diff $PREVIOUS $CURRENT $submodule > $opwd/$submodule.diff | sta=1
    cd $opwd

    if [ -s "${submodule}.diff" ]; then
	echo "A1 $sta"
	end=$(tail -n2 ${submodule}.diff | grep -e "+Subproject commit " | grep -oe " [0-9|a-f]*$")
	start=$(tail -n2 ${submodule}.diff | grep -e "-Subproject commit " | grep -oe " [0-9|a-f]*$")
	echo "   $submodule: START: $start"
	echo "   $submodule: END: $end"

	cd $S/$submodule

	startTag=$(git describe --tags --match 'v[0-9]*.[0-9]*.[0-9]*' $start)
	endTag=$(git describe --tags --match 'v[0-9]*.[0-9]*.[0-9]*' $end)
	
	getChangelogRange $S/$submodule $startTag $endTag

	echo "   $submodule: range $startTag ... $endTag"
    else
	echo "ignoding no change"
    fi
}


csplit CHANGELOG.md "/# Changes/" '{*}' -f "cl_"
opwd=$PWD

rm -f $opwd/subliments.txt
rm cl_00
cnt=0

for f in cl_*; do
    l1=$(head -n 1 $f)
    echo "$f: $l1"
    l2=$(echo "$l1" | grep -oe "v[0-9]*\.[0-9]*\.[0-9]*$")
    TAG="$l2"

    if [ "$cnt" == "0" ]; then
	CURRENT=$l2
    fi
    if [ "$cnt" == "1" ]; then
	PREVIOUS=$l2
    fi

    V="${TAG#v*}"
    MAJOR="${V%%.*}"
    V="${V#*.}"
    MINOR="${V%%.*}"
    V="${V#*.}"
    PATCH="${V%%-*}"
    V="${V#*-}"

    echo "   Major: $MAJOR; Minor: $MINOR; Patch: $PATCH"
    let cnt+=1

    # sed -e " v.*\..*\..*\.$"
done

echo 
echo "CURRENT: $CURRENT"
echo "PREVIOUS: $PREVIOUS"

# CURRENT=v0.3.3
# PREVIOUS=v0.3.2

mainCurrent="$CURRENT"
mainPrevious="$PREVIOUS"

# git describe --tags --match 'v[0-9]*.[0-9]*.[0-9]*' d0847bb7f9fb259a52a314a82aaab857d4096707
# git diff v0.3.6 v0.3.7 libbiwak 
cd $S

submodules=$(git submodule foreach --quiet 'basename $PWD')

echo "Submodules: $submodules"

for submodule in $submodules; do
    echo "Parsing $submodule"
    CURRENT=$mainCurrent
    PREVIOUS=$mainPrevious
    parseSubmodule
done

echo "[$0] fin."

#---fin------------------------------------------------------------------------
