#!/bin/bash
##
#  Korinf project
#
#  Log related functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 2009
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.

#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

export COLUMNS=80
kormod outformat


success()
{
	MOVE_TO_COL
	echo -n '[ '
	SETCOLOR_SUCCESS
	echo -n 'DONE'
	SETCOLOR_NORMAL
	echo -n ' ]'
}


failure()
{
	MOVE_TO_COL
	echo -n '['
	SETCOLOR_FAILURE
	echo -n 'FAILED'
	SETCOLOR_NORMAL
	echo -n ']'
}

skipped()
{
	MOVE_TO_COL
	echo -n '['
	SETCOLOR_WARNING
	echo -n 'SKIPPED'
	SETCOLOR_NORMAL
	echo -n ']'
}


# set log dir in current dir or in TMPDIR and create link from the root dir
set_log_dir()
{
	ALOGDIR=`pwd`/log
	if [ ! -w "$ALOGDIR" ] ; then
		ALOGDIR=$TMPDIR/korinf-log
		if [ -n "$KORINFSOURCETREE" ] && [ ! -L "$KORINFSOURCETREE/log" ] ; then
			ln -s $ALOGDIR "$KORINFSOURCETREE/log"
		fi
	fi

	[ -z "$1" ] || ALOGDIR="$ALOGDIR$1"

	[ -z "$ADEBUG" ] || echo "set_log_dir: Check me and disable. I create logdir in $ALOGDIR"
	mkdir -p $ALOGDIR
}


init_dist_log()
{
	[ -z "$ALOGDIR" ] || set_log_dir
	# FIXME: used in ELOGFILE creating
	LOGDIR="$ALOGDIR/$1"
	mkdir -p $LOGDIR || fatal "Can't create $LOGDIR"
	LOGFILE=$LOGDIR/$BUILDNAME.log
	>>$LOGFILE || fatal "Can't create log $LOGFILE"
	PREVSYSTEMLOGIT=""
}

reset_dist_log()
{
	>$LOGFILE || fatal "Can't create log $LOGFILE"
}


copying_log()
{
	local DESTLOGFILE DESTLOGDIR
	if [ -n "$MAINFILESLIST" ] ; then
		DESTLOGDIR=$DESTDIR/log/
	else
		DESTLOGDIR=$DESTDIR/extra/log/
	fi
	mkdir -p $DESTLOGDIR
	DESTLOGFILE=$DESTLOGDIR/`basename $LOGFILE`
	rm -f $DESTLOGFILE.bz2
	cp $LOGFILE $DESTLOGFILE && bzip $DESTLOGFILE
}


print_spaces_instead_string()
{
	local LEN STR SPACES
	STR="$1"
	LEN=${#STR}
	SPACES="                                                  "
	echo "$SPACES" | cut -c 1-$LEN
}


write_report()
{
	set_log_dir
	# FIXME: create dir here
	if [ -d "$ALOGDIR" ] ; then
		cat >> $ALOGDIR/autobuild.report.log
	fi
}

PREVSYSTEMLOGIT=
logit()
{
	local RET STRMSG SYSNOW
	if [ "$PREVSYSTEMLOGIT" = "$dist" ] ; then
		SYSNOW=`print_spaces_instead_string "$dist"`
	else
		echo
		SYSNOW=$dist
	fi
	STRMSG="$SYSNOW * [`date '+%R'`] $1 ... "
	PREVSYSTEMLOGIT=$dist
	# print one line if scripted night build
	[ -z "$NIGHTBUILD" ] && echo -n "$STRMSG"
	shift
	TIMESTAMP=`date "+%s"`
	if [ ! "$1" = "SKIPPED" ] ; then
		$* >>$LOGFILE 2>&1
	fi
	RET=$?
	let TIMESTAMP=`date "+%s"`-$TIMESTAMP
	[ -z "$NIGHTBUILD" ] && STRMSG=""
	#[ "$RET" = 0 ] && STRMSG="$STRMSG done" || STRMSG="$STRMSG FAILED"
	#[ $TIMESTAMP = "0" ] && echo "$STRMSG" || echo "$STRMSG ($TIMESTAMP )"
	echo -n "$STRMSG"
	[ $TIMESTAMP = "0" ] || echo -n "($TIMESTAMP ) "
	if [ "$1" = "SKIPPED" ] ; then
		skipped
	else
		[ "$RET" = 0 ] && success || failure
	fi
	echo
	return $RET
}
