#!/bin/bash
##
#  Korinf project
#
#  Common functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005-2010
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2010
#
#  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/>.
##

# needed version etersoft-build-utils package
NEEDETERBUILD=2023

realpath()
{
	readlink -f "$@"
}

load_etersoft_build_utils()
{
# Load etersoft-build-utils part
LOCETERBUILDDIR=`dirname $0`/$TOPDIR/share/eterbuild/functions
RELETERBUILDDIR=$TOPDIR/../etersoft-build-utils/share/eterbuild/functions

# Create link to etersoft-build-utils repo if not exists
if [ ! -L "$LOCETERBUILDDIR" ] ; then
	if [ ! -d "`dirname $0`/$RELETERBUILDDIR" ] ; then
		ETERBUILDDIR="/usr/share/eterbuild"
		if [ ! -d "$ETERBUILDDIR" ] ; then
			echo "Clone etersoft-build-utils git repository to the same directory as korinf checkouted" >&2
			exit 1
		fi
		ETERBUILDETC=$(realpath "$ETERBUILDDIR/../../etc")
		ETERBUILDBIN=$(realpath "$ETERBUILDDIR/../../bin")
		return
	else
		rm -f $LOCETERBUILDDIR
		ln -s ../$RELETERBUILDDIR $LOCETERBUILDDIR
	fi
fi

# HACK: set to real path
ETERBUILDDIR=$(realpath "`dirname $0`/$RELETERBUILDDIR/../../../share/eterbuild")
ETERBUILDETC=$(realpath "$ETERBUILDDIR/../../etc")
ETERBUILDBIN=$(realpath "$ETERBUILDDIR/../../bin")
if [ ! -d "$ETERBUILDDIR" ] ; then
	echo "Clone etersoft-build-utils git repository to the same directory as korinf checkouted" >&2
	exit 1
fi
}

[ -n "$TOPDIR" ] || TOPDIR=../
load_etersoft_build_utils

# these variable will not override in eterbuild common
. $ETERBUILDDIR/functions/common



KORINFETC=/etc/eterbuild
KORINFDIR=/usr/share/eterbuild

# if run from local installation
if ! dirname $0 | grep ^/usr/share >/dev/null ; then
	KORINFDIR=$(readlink -f `dirname $0`/$TOPDIR/share/eterbuild)
	KORINFETC=$(readlink -f `dirname $0`/$TOPDIR/etc)
	if [ -r "$KORINFETC/../AUTHORS" ] ; then
		if [ -n "$VERBOSE" ] ; then
			echo "Note: run from source tree, datadir=$KORINFDIR, sysconfdir=$KORINFETC"
		fi
		KORINFSOURCETREE=$(readlink -f "$KORINFETC/..")
	else
		KORINFETC=/etc/eterbuild
		KORINFDIR=/usr/share/eterbuild
	fi
fi

test -n "$KORINFDIR" && PATH=$KORINFDIR/korinf/commands:$PATH
export KORINFETC KORINFDIR

# Try to set clear environment
LC_ALL=C
unset DISPLAY

kormod()
{
	local i
	for i in $@ ; do
		. $KORINFDIR/korinf/$i
	done
}

# print out full path to src.rpm (get from BUILDNAME on SOURCEPATH)
get_src_package()
{
	local BUILDSRPM
	local SOURCEPATH="$1"
	local BUILDNAME="$2"
	# Do use direct package name?
	if echo $BUILDNAME | grep -q "\.src\.rpm" ; then
		BUILDSRPM="$SOURCEPATH/$BUILDNAME"
	else
		BUILDSRPM="$SOURCEPATH/$(ls -1 $SOURCEPATH/$BUILDNAME-[0-9]*.src.rpm 2>/dev/null | last_rpm).src.rpm"
	fi

	echo "$BUILDSRPM"
	test -r "$BUILDSRPM"
}

# print out full path to binary package (get from BUILDNAME on TARGETPATH)
get_bin_package()
{
	local BUILDPKG
	local TARGETPATH="$1"
	local BUILDNAME="$2"

	# check for package started with NAME
	BUILDPKG="$(ls -1 $TARGETPATH/$BUILDNAME[-_][0-9]* 2>/dev/null | tail -n1)"

	# try check in extra
	if [ ! -r "$BUILDPKG" ] ; then
		# check for package started with NAME
		BUILDPKG="$(ls -1 $TARGETPATH/extra/$BUILDNAME[-_][0-9]* 2>/dev/null | tail -n1)"
	fi

	if [ ! -r "$BUILDPKG" ] ; then
		# FIXME: Hack
		# asterisk before name for possible rename NAME <-> libNAME
		#BUILDPKG="$(ls -1 $PATHPKG/*$BUILDNAME[-_][0-9]* 2>/dev/null | head -n1)"
		BUILDPKG=
	fi

	echo $BUILDPKG
	test -r "$BUILDPKG"
}

# fills DISTRNAME, DISTRVERSION, BUILDARCH by arch/SystemName/Version
parse_dist_name()
{
	local dist=$1
	local v
	# dist format: [arch/]Name/Version
	DISTRVERSION=`echo $dist | sed -e "s|.*/||g"`
	v=`echo $dist | sed -e "s|/$DISTRVERSION\$||g"`
	DISTRNAME=`echo $v | sed -e "s|.*/||g"`
	BUILDARCH=`echo $v | sed -e "s|/.*||g"`
	# FIXME: i586 by default, some x86_64 detection?
	test "$BUILDARCH" = "$v" && BUILDARCH=i586
	# if parse error
	test "$DISTRNAME" = "$DISTRVERSION" && DISTRNAME="" && DISTRVERSION=""
	export DISTRNAME DISTRVERSION BUILDARCH
}

# return full path to sources, detected by TARGET dir
get_rpm_sources()
{
	local TARGETPATH="$1"
	local SOURCEPATH=
	# WINE/Distro/Version or x86_64/Distro/Version
	SOURCEPATH="$(readlink -f "$TARGETPATH/../../../sources")"
	# Distro/Version or x86_64/Distro
	[ -d "$SOURCEPATH" ] || SOURCEPATH="$(readlink -f "$TARGETPATH/../../sources")"
	# WINE/x86_64/Distro/Version
	[ -d "$SOURCEPATH" ] || SOURCEPATH="$(readlink -f "$TARGETPATH/../../../../sources")"
	# Distro (Windows)
	[ -d "$SOURCEPATH" ] || SOURCEPATH="$(readlink -f "$TARGETPATH/../sources")"
	echo "$SOURCEPATH"
	return 0
}

# set target vars before package build
# can change BUILDARCH
set_target_var()
{
	# defines PKGFORMAT, PKGVENDOR, RPMVENDOR
	set_target_pkg_env # (from etersoft-build-utils)

	[ -z "$PKGFORMAT" ] && { warning "PKGFORMAT is empty" ; return 1 ; }

	[ "$PKGVENDOR" = "gentoo" ] || return 0

	# HACK for Gentoo
	if [ -n "$BOOTSTRAP" ] ; then
		[ "$PKGFORMAT" = "tbz2" ] && PKGFORMAT=rpm
	else
		[ "$PKGFORMAT" = "tbz2" ] && PKGFORMAT=tar.bz2
	fi
	return 0
}

# $1 - dist like CentOS/5.4
get_rpm_args()
{
        grep "^$2 " $KORINFETC/rpmopt/rpm | cut -f2-100 -d" "
}

# $1 - dist like CentOS/5.4
get_remote_host()
{
        grep "^$1 " $KORINFETC/remote | ( read d host commands ; echo $host ; )
}

# $1 - dist like CentOS/5.4
get_remote_commands()
{
        grep "^$1 " $KORINFETC/remote | ( read d host commands ; echo $commands ; )
}

is_remote_build()
{
	local rhost
	assert_var DISTRNAME DISTRVERSION
	rhost=$(get_remote_host $DISTRNAME/$DISTRVERSION)
	test -n "$rhost"
}

kormod config log list
