#!/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=2100

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 source_path="$1"
	local BUILDNAME="$2"
	# Do use direct package name?
	if echo $BUILDNAME | grep -q "\.src\.rpm" ; then
		BUILDSRPM="$source_path/$BUILDNAME"
	else
		BUILDSRPM="$source_path/$(ls -1 $source_path/$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 target_path="$1"
	local BUILDNAME="$2"

	# TODO: need to check target package
	if [ "$PKGFORMAT" = "deb" ] ; then
		# hack with convert from repl
		# see also converts/deb internal_get_deb_pkgname_from_rpm
		BUILDNAME=`echo $BUILDNAME | filter_deb_pkgnames`
	fi

	# check for package started with NAME
	BUILDPKG="$(ls -1 $target_path/$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 $target_path/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
	# CHECKME: miss export
	[ -z "$1" ] && DISTRNAME="" && DISTRVERSION="" && BUILDARCH="" && return 1
	# 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
	[ "$DISTRNAME" = "$DISTRVERSION" ] && DISTRNAME="" && DISTRVERSION=""
	export DISTRNAME DISTRVERSION BUILDARCH
}

# return full path to sources
# work for WINE@Etersoft/WINE and WINE@Etersoft/CIFS (linked)
get_sources_dir()
{
	local PRODPATH="$1"
	test -d "$PRODPATH" || fatal "$PRODPATH does not exists."
	# use real realpath command, not our replacement
	/usr/bin/realpath -e "$PRODPATH/sources" 2>/dev/null && return
	/usr/bin/realpath -e "$PRODPATH/../sources" 2>/dev/null && return
	fatal "Can't get source path from target path $PRODPATH. It is possible, sources dir missed there."
}

# Почему эти EXPMAINFILES вовсе не раскрыты???
# get package names extracted from package mask
# internal
get_target_package_names()
{
	local j
	for j in $EXPMAINFILES $EXPEXTRAFILES ; do
		get_pkgname_from_filename $j
	done
}

# 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 ; }

	# FIXME: why we need this?
	[ "$PKGVENDOR" = "gentoo" ] && [ "$PKGFORMAT" = "tbz2" ] && PKGFORMAT=tar.bz2
	return 0
}

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

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

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

is_remote_build()
{
	local rhost
	local dist="$1"
	if [ -z "$dist" ] ; then
		assert_var DISTRNAME DISTRVERSION
		dist=$DISTRNAME/$DISTRVERSION
	fi
	rhost=$(get_remote_host $dist)
	test -n "$rhost"
}

kormod config log list
