#!/bin/sh

usage()
{
    echo "This script generates LO git commit summary"
    echo
    echo "Usage: ${0##*/} [--help] [--piece=<piece>] bootstrap-dir summary.log [git_log_param...]"
    echo
    echo "Options:"
    echo
    echo "	--help		print this help"
    echo "	--piece=<piece>	summarize just chnages from the given piece"
    echo "      bootstrap-dir   directory with the libreoffice/bootstrap clone; the other piece repos"
    echo "                      must be cloned in the bootstrap-dir/clone/<piece> subdirectories"
    echo "      summary.log     output file"
    echo "      git_log_param   extra parameters passed to the git log command to define"
    echo "                      the area of interest , e.g. --after=\"2010-09-27\" or"
    echo "                      TAG..HEAD"
}

only_piece=
bootstrap_dir=
summary_log=
export git_log_params=
while test -n "$1" ; do
    case "$1" in
	--help)
	    usage
	    exit 0;
	    ;;
	--piece=*)
	    only_piece=`echo $1 | sed "s|--piece=||"`
	    ;;
	*)
	    if test -z "$bootstrap_dir" ; then
	        bootstrap_dir="$1"
	    elif test -z "$summary_log" ; then
	        summary_log="$1";
	    else
	        git_log_params="$git_log_params $1"
	    fi
    esac
    shift
done

if test -z "$bootstrap_dir" ; then
    echo "Error: please, define directory with the cloned libreoffice/bootstrap repo"
    exit 1;
fi

if test -z "$summary_log" ; then
    echo "Error: please, define the output file"
    exit 1;
fi

if test ! -d "$bootstrap_dir/.git" -o ! -f "$bootstrap_dir/configure.in" -o ! -d "$bootstrap_dir/clone" ; then
    echo "Error: invalid bootstrap dir: \"$bootstrap_dir\""
    echo "       it must point to a clone of libreoffice/bootstrap git repo"
    exit 1;
fi



get_git_log()
{
    git_dir="$1"
    temp_dir="$2"
    repo="$3"

    echo "Getting log from the repo: $repo"
    cd $git_dir
#    git log --pretty='format:%an: %s%n' $git_log_params >$temp_dir/$repo.log
    git log --pretty='format:    + %s [%an]' $git_log_params | sort -u >$temp_dir/$repo.log
    cd - >/dev/null 2>&1
}

add_to_paterns()
{
    sed -e "s|\#|\\\#|" \
	-e "s|\[|\\\[|" \
	-e "s|\]|\\\]|" $1 >>"$2"
}

temp_dir=`mktemp -d /tmp/lo-git-commit-summary-XXXXXX`

# get logs
if test -z "$only_piece" -o "$only_piece" = "bootstrap" ; then
    get_git_log "$bootstrap_dir" "$temp_dir" "bootstrap"
fi

if test "$only_piece" != "bootstrap" ; then
    if test -z "$only_piece" ; then
        pieces_list=`ls $bootstrap_dir/clone`
    else
        pieces_list="$only_piece"
        if ! test -d "$bootstrap_dir/clone/$only_piece" ; then
            echo "Error: wrong piece; directory does not exist: $bootstrap_dir/clone/$only_piece"
            exit 1;
        fi
    fi
    for piece in $pieces_list ; do
        test -d "$bootstrap_dir/clone/$piece" || continue;
        get_git_log "$bootstrap_dir/clone/$piece" "$temp_dir" "$piece"
    done
fi

# special sections
echo "Looking for build bits..."

grep -h -i " build" $temp_dir/*.log | sort -u >"$temp_dir/build.special"
#sed -e "s|\(.*\)|'\1'|" "$temp_dir/build.special" >"$temp_dir/special.filter"
add_to_paterns "$temp_dir/build.special" "$temp_dir/special.patterns"

echo "Looking for global changes..."

cat $temp_dir/*.log | sort | uniq -c | grep -v "      1" | cut -c 9- | grep -v -f "$temp_dir/special.patterns" >"$temp_dir/common.special"
#sed -e "s|\(.*\)|'\1'|"  "$temp_dir/common.special" >>"$temp_dir/special.filter"
add_to_paterns "$temp_dir/common.special" "$temp_dir/special.patterns"

echo "Generating summary..."

rm -rf "$summary_log"

echo "+ common:" >>"$summary_log"
cat "$temp_dir/common.special" >>"$summary_log"

for log in `ls $temp_dir/*.log` ; do
    piece=`echo $log | sed "s|$temp_dir/\(.*\)\.log\$|\1|"`
    echo "+ $piece:" >>"$summary_log"
    grep -v -f "$temp_dir/special.patterns" "$log" >>"$summary_log"
done

echo "+ build bits:" >>"$summary_log"
cat "$temp_dir/build.special" >>"$summary_log"

rm -rf "$temp_dir"
