#!/bin/bash

# makelog - Redirect make's output into logfile

usage() {
	echo "Usage: makelog <target>..."
}

if [ -z "$1" -o "$1" == "--help" ]; then
	usage
	exit 0
fi

params="$*"

logdate=$(date +%Y%m%d%H%M)
logdir=.log
logfile=${logdir}/${logdate}_make_${params// /_}.log

logtmp=$(mktemp makelog.XXXXXX)

# clean up at exit
trap "rm -f $logtmp" EXIT

if test -d root; then
	exist_root=true
else
	exist_root=false
fi

if $exist_root; then
	pushd root
	test -d .git || git init
	git add .
	git commit -a -m"- before $params"
	popd
fi

time make $params 2>&1 | tee $logtmp

if $exist_root; then
	pushd root
	git add .
	git commit -a -m"- after $params"
	popd
fi

test -d ${logdir} || mkdir ${logdir}
echo -e "\nmake $*:\n" >> ${logfile}
cat $logtmp >> ${logfile}