#!/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 messages/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"
}

set_destlogdir()
{
	assert_var DESTDIR
	export DESTLOGDIR=$DESTDIR/log
	mkdir -p $DESTLOGDIR/ || fatal "Error with $DESTLOGDIR"
}

copying_log()
{
	local DESTLOGFILE
	set_destlogdir

	DESTLOGFILE=$DESTLOGDIR/`basename $LOGFILE`
	rm -f $DESTLOGFILE.bz2
	cp -f $LOGFILE $DESTLOGFILE && bzip $DESTLOGFILE
}


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


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

# internal
print_pretty_distroname()
{
	if [ "$PREVSYSTEMLOGIT" = "$dist" ] ; then
		print_spaces_instead_string "$dist"
	else
		echo
		echo -n "$dist"
		PREVSYSTEMLOGIT=$dist
	fi
}

print_status()
{
	case "$1" in
		0) success ;;
		1) failure ;;
		22) skipped ;;
		*) failure ;;
	esac
	echo
}

logit()
{
	local RET STRMSG logtimestamp
	print_pretty_distroname

	STRMSG=" * [`date '+%T'`] $1 ... "
	shift

	if [ "$1" = "SKIPPED" ] ; then
		echo -n "$STRMSG"
		print_status 22
		return 0
	fi

	# print one line if scripted night build
	[ -z "$NIGHTBUILD" ] && echo -n "$STRMSG"
	logtimestamp=`date "+%s"`

	if [ -n "$LOGFILE" ] ; then
		echo >>$LOGFILE
		echo "STEP $STRMSG" >>$LOGFILE
		$* >>$LOGFILE 2>&1
	else
		$*
	fi
	RET=$?

	logtimestamp=$(( `date "+%s"` - $logtimestamp))
	[ -z "$NIGHTBUILD" ] && STRMSG=""

	echo -n "$STRMSG"
	[ $logtimestamp = "0" ] || echo -n "($logtimestamp secs) "

	[ "$1" = "SKIPPED" ] && RET=22
	print_status $RET

	# fix skipped status
	[ "$RET" = 22 ] && RET=0
	return $RET
}

loginfo()
{
	print_pretty_distroname
	echo " : $1"
}
