#! /bin/sh

tmp=/tmp/$$
#tmp=`pwd`/TMP.$$
here=`pwd`
status=1
prog=`basename $0`
trap "rm -f $tmp.*; exit \$status" 0 1 2 15
#trap "exit \$status" 0 1 2 15

mod=
ism=
git=
out=
LOD=xfs-linux-melb
ALTLOD=xfs-linux

_usage()
{
    cat << EOF
Usage: $prog mod workarea git-tree output

mod       Last mod that went into GIT tree in last push
workarea  ptools workarea
git-tree  GIT tree
output    Output file with list of mods to be merged. 
          Format of output file is: mod:order
          Order is 0 if not in git, or the index at which it
	  was checked into the git tree
EOF
    exit 1
}

if false
then
    while getopts "P:" c
    do
	case $c in
	    P) PLROOT=$OPTARG;;
	    \?) _usage
		exit 1
		break;;
	esac
    done

    shift `expr $OPTIND - 1`
fi

if [ $# -ne 4 ]
then
    _usage
    exit 1
fi

mod=$1
ism=$2
git=$3
out=$4

if [ ! -d $ism ]
then
    echo "$prog: Unable to open $ism"
    exit 1
fi

if [ ! -d $git ]
then
    echo "$prog: Unable to open $git"
    exit 1
fi

cd $ism
export WORKAREA=`pwd`

if p_modinfo $mod > $tmp.modinfo
then
    :
else
    echo "$prog: Unable to find mod $mod"
    exit 1
fi   

modlod=`echo $mod | cut -d':' -f1`
modism=`echo $mod | cut -d':' -f2`
modid=`echo $mod | cut -d':' -f3`

ts=`cat $tmp.modinfo | grep on | head -1 | awk '{print $5}'`

echo ""
echo "$mod was submitted on $ts"
echo "Collecting all mods from that date:"

if p_modinfo -t"$ts" -P $modism/$modlod | grep $modlod > $tmp.modlist
then
    :
else
    echo "$prog: Unable to get mod list from $modism/$modlod"
    exit 1
fi

if [ ! -s $tmp.modlist ]
then
    echo "$prog: Empty mod list!"
    exit 1
fi

cat $tmp.modlist \
| cut -d':' -f3 \
| awk '{ print $1, NR }' \
> $tmp.modlistcut

# output to the stdout for feedback
cat $tmp.modlistcut \
| cut -d' ' -f1

echo ""
echo "Looking for [XFS] commits which are missing SGI-Modid:"

cd $git
git log \
| awk -v modid=$modid '
/\[XFS\]/	{ commit_xfs = $0; next }
/^Author/	{ author = $2 " " $3; next }
/SGI-Modid/	{
		    commit_xfs = 0
		    if ($2 ~ modid) {
			exit
		    }
		    next
		}
/^commit/	{ 
		    if (commit_xfs != 0 && author !~ /Torvalds/) {
			printf("Missing modid: Author: %s, Summary: %s\n", author, commit_xfs)
		    }	
		    commit_xfs = 0; author = 0
		    next
		}'

echo ""
echo "Collecting all mods from git tree:"

# NOTE: this relies on us always using
# SGI-Modid in our commit descriptions
# Need to look at ones where we didn't do this
cd $git
if git log | grep SGI-Modid > $tmp.gitlist
then
    :
else
    echo "$prog: git log failed on $git"
    exit 1
fi

if [ ! -s $tmp.gitlist ]
then
    echo "$prog: Empty git mod list!"
    exit 1
fi

# Only look at the mods up to and not including
# the mod that we were given, <modid>
# which is supposed to be the last mod that was checked
# in a major commit
#
# We extract modid and then extract out the number
# SGI-Modid: xfs-linux-melb:xfs-kern:26293a
# => xfs-linux-melb:xfs-kern:26293a
# => 26293a
# => 26293a 85        where 85 is a sequence number (rec#)
# then reverse numeric by their initial order (rec#) 
# then numeric sort by the modid 
#
# This is trying to attach sequence numbers to the
# git mods but must reverse them because it wants the
# lower number to be older number and "git log"
# outputs the newest first and older ones later
# So the output will be in mod# order but will have appended
# to it the git sequence where smaller is older
#
cat $tmp.gitlist \
| awk -v modid=$modid '
		{ print $2 }
$0 ~ modid 	{ exit }' \
| cut -d':' -f3 \
| awk '{ print $0, NR }' \
| sort -n -r -k2 \
| awk '{ print $1, NR }' \
| sort -t' ' -n -k1 \
> $tmp.gitlistcut

# output to the stdout for feedback
cat $tmp.gitlistcut \
| cut -d' ' -f1


echo ""
echo "Generating list of mods to be merged:"

#
# Print out lines which occur in both or just in modlist (ptools)
# For ones which are just in modlist, the 2.2 field is blank
join -1 1 -2 1 -a 1 -o 1.1,2.2 $tmp.modlistcut $tmp.gitlistcut \
> $tmp.merged

#
# Only print unpairable lines from tmp.gitlistcut
# => mods in gitlist but not in ptools
# Print out the whole entry for gitlist's mod and rec#
join -1 1 -2 1 -v 2 -o 2.1,2.2 $tmp.modlistcut $tmp.gitlistcut \
> $tmp.unknown

if [ -s $tmp.unknown ]
then
    echo "$prog: Unknown mods in git tree:"
    cat $tmp.unknown
fi

if [ ! -s $tmp.merged ]
then
    echo "$prog: No mods to merge!"
    exit 1
fi

cd $here
cat $tmp.merged \
| tr ' ' ':' \
| awk -F':' -v modlod=$modlod -v modism=$modism '
{ if ($2 == "") $2 = "0"; 
  printf("%s:%s:%s:%s\n", modlod, modism, $1, $2); }' \
> $out

cat $out

status=0
