#!/bin/sh
# 2003-2005 Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
# 20.06.04, 15.01.05, 28.02.05, 07.03.05
#
# You can copy this file in ~/.ebconfig for per user settings

### Gettext ###
. gettext.sh

TEXTDOMAIN=etersoft-build-utils
export TEXTDOMAIN
TEXTDOMAINDIR='/usr/share/locale'
export TEXTDOMAINDIR

echog()
{
	if [ "$1" = "-n" ] ; then
		shift
		eval_gettext "$@"
	else
		eval_gettext "$@"; echo
	fi
}
### Gettext ###

#   ,  ccache  
export CC=gcc
export CXX=g++
#   - 
#export CPP=g++

if [ "$UID" = "0" ] ; then
	echog "It is strict recommended do not use these scripts as root"
	exit 1
fi

which ccache >/dev/null
if [ $? ]
then
	#   
	export GCC_USE_CCACHE=1
else
	echog "Note: you can install ccache for compiler cache and speedup compilation"
fi

# It can be overrides in config
DEFAULTARCH=i586
RPMDIR="$HOME/RPM"
APTCONF=/etc/apt/apt.conf
OWNERMAIL=`grep "^%packager" ~/.rpmmacros | head -n 1 | sed -e "s/%packager[ \t]*//g"`
BUILDROOT=`grep "^%_tmppath" ~/.rpmmacros | head -n 1 | sed -e "s/%_tmppath[ \t]*//g" | sed -e "s|%homedir|$HOME/|g"`
UPLOADDIR="$RPMDIR/upload"
LOGDIR="$RPMDIR/log"
REMOTERPMDIR=
HASHERDIR="$HOME/hasher"
RSYNCSISYPHUS=rsync.altlinux.org::ALTLinux/Sisyphus

BUILDSERVER=
#BUILDSERVERPATH="$HASHERDIR/repo/i586/RPMS.hasher"

test -f /etc/rpm/etersoft-build-config && . /etc/rpm/etersoft-build-config
test -f ~/.ebconfig  && . ~/.ebconfig
#test -f ~/.rpmconfig  && { echog "Please rename ~/.rpmconfig to ./ebconfig"; exit 1; }

eval BUILDROOT=$BUILDROOT
test -n "$BUILDROOT" && { mkdir -p $BUILDROOT ; ln -snf $BUILDROOT $RPMDIR/tmp ; } || { echog "Please set BUILDROOT in ~/.ebconfig" ; }
#ln -sf $RPMDIR/BUILD/

function get_var()
{
#	sed -r -n "s/^(.*)$1:(.*)$/\1/pi"
#	sed -r -n "s/^(.*)$1[ \t]*:[ \t]*(.*)$/\1/pi"
#	grep -i "^$1:" | head -n 1 | sed -e "s/^.*\?[ \t]*:[ \t]*//"
#	grep -i "^$1:" | head -n 1 | perl -pi -e "s/^.*?[ \t]*:[ \t]*//"
	grep -i "^$1:" | head -n 1 | sed -e "s/^[^:]*[ \t]*:[ \t]*//"

}

#      .
#   !
#    Name:   
function build_rpms_name()
{
	#TODO change : to = and execute?
	NAMESPEC=$1
	test -f $NAMESPEC || { echog "Spec $NAMESPEC does not exist"; exit 1; }
	tmprpm=`mktemp -t etersoft-build-utils.XXX || exit 1`
	rpm -bE $NAMESPEC | grep ":"  >$tmprpm
	CATSPEC="cat $tmprpm"
	BASENAME=$(cat $tmprpm | get_var "Name")
	RELEASE=$($CATSPEC | get_var "Release")
	VERSION=$($CATSPEC | get_var "Version")
	rm -f $tmprpm
	NAMERPMIN=$BASENAME-$VERSION-$RELEASE.$ARCH.rpm
	NAMESRPMIN=$BASENAME-$VERSION-$RELEASE.src.rpm
}

function check_log()
{
	echo
	echo "-------------------------------------------------------"
	echog "Check log for..."
	local LOGFILE
	LOGFILE=$1
	grep 'ld: warning: libstdc++\.so\.5, needed by [^ ]\+, may conflict with libstdc++\.so\.6' $LOGFILE
	RES=$?
	if [ $RES = 0 ] ; then
		echog "Error: libstdc++.so.5/6 conflicts"
		return 1
	fi
	grep 'python-strict' $LOGFILE && { echog "Warning: python-strict used!!!" ; }
	return 0
}


# LISTNAMES, options in arg
function pack_src_rpm()
{
ARGS=$@
for i in $LISTNAMES
do
	#     
	# BASENAME, RELEASE, VERSION, NAMESRPMIN, NAMERPMIN
	build_rpms_name $i
	echo
	if [ -n "${SIGN}" ];	then
		echog "Try to packing \$NAMESRPMIN package for sign"
		add_changelog $i
		if [ $? = "0" ]; then
			echog "Spec file \$i had not ChangeLog entry. It have added now, check it and enter your command again."
			echog "Wait for ten seconds."
			sleep 10
			exit 1
		fi
		rpm -bs $ARGS $i $NODEPS
	else
		echog "Just packing \$NAMESRPMIN"
		rpm -bs $ARGS $i $NODEPS || exit 1
	fi
	LISTBUILT="$LISTBUILT$NAMESRPMIN "
done
test -z "$LISTBUILT" && { echog "Error: List for build is empty. Check if file is exist."; exit 1 ; }
LISTRPMARGS=`echo ${LISTRPMARGS} | sed -e "s/--nodeps//g"`

}

function set_target_type()
{
	case "$1" in
		("M23" | "M24" | "M30" | "DD")
			MENV="$1"
			return 0;
			;;
	esac
	return 1;
}

# ,    :   -M23 -M24   ,   
function detect_target_env()
{
set_target_type $(basename `pwd`)
if [ -n "$MENV" ] ; then
	if [ $MENV = "DD" ] ; then
		echog "Target system: Daedalus, use usual \$APTCONF"
	else
		APTCONF=$APTCONF.$MENV
		echog "Target system: \$MENV, use \$APTCONF"
		MENVARG="-$MENV"
	fi
fi
}

function build_list_and_args()
{
LISTNAMES=""
LISTARGS=""
OPTINDEX=1
for i in "$@"
do
	#   ,  ,    .
	if [ -f $i ]; then
		LISTNAMES="$LISTNAMES $i"
	else
		# FIXME: how to remove first symbol?
		#if [ "`echo $i | cut -c 1`" = "-" ]; then
		if 	set_target_type ${i/-/} ; then
			shift $OPTINDEX
			OPTINDEX=$((OPTINDEX-1))
			#`echo $i | cut -c 2-4`
		else
			LISTARGS="$LISTARGS $i"
		fi
	fi
	OPTINDEX=$((OPTINDEX+1))
done
detect_target_env
#LISTRPMARGS=${LISTARGS/$MENVARG/}
# FIXME: spaces
LISTRPMARGS=$LISTARGS
test -n "$MENVARG" && LISTRPMARGS=`echo ${LISTARGS} | sed -e "s/$MENVARG//g"`
}


function check_key()
{
	echog "Check access to SSH private key..."
	ssh-add -l || ssh-add
}
