#!/bin/bash
# 2008 Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain

# convert pkg names to Debian style
filter_deb_pkgnames()
{
	sed -e "s|^ *\(.*\)-devel *\$|\1-dev|g" | tr "[A-Z]" "[a-z]"
}

# Get replacement rule for ALT package to local in $1 (scan for files in $@)
# sets ALTPKGNAME, TARGETPKGNAME variable
# used for hack: PKGFORMAT, DISTRNAME, BUILDARCH
tolocal_anyrepl()
{
	local i REPLRULE WARULES
	local GREP=$1
	shift
	# TODO: fix space removing
	WARULES="s/^ *//g 
		s/ *\$//g 
		s/ *|/|/g 
		s/| */|/g"

	for i in $@ ; do
		REPLRULE=`grep -v "^#" "$i" 2>/dev/null | grep -- "^ *$GREP *|" | sed -e "$WARULES" | head -n1`
		# For broken rule
		echo $REPLRULE | grep "|" >/dev/null || REPLRULE=""
		#REPLRULE=`echo $REPLRULE | sed -r -e 's,|,!,g'`
		ALTPKGNAME=`echo $REPLRULE | cut -d"|" -f1 | sed -e "s|\+|\\\\\+|g"`
		TARGETPKGNAME=`echo $REPLRULE | cut -d"|" -f2 | sed -e "s|\+|\\\\\+|g"`
		test -n "$REPLRULE" && return 0
	done

	# Part of local hack
	NEWRESULT="$GREP"

	if [ "$PKGFORMAT" = "deb" ] ; then
		local NEWRESULT=`echo $GREP | filter_deb_pkgnames`
	fi

	# Add hack for replace lib with lib64 on Mandriva
	if [ "$DISTRNAME" = "Mandriva" ] && [ $BUILDARCH = "x86_64" ] ; then
		local NEWRESULT=`echo $GREP | sed -e "s|^lib\([^6]\)|lib64\1|g"`
	fi

	if [ "$DISTRNAME" = "ArchLinux" ] || [ "$DISTRNAME" = "FreeBSD" ]  ; then
		local NEWRESULT=`echo $GREP | sed -e "s|^ *\(.*\)-devel *\$|\1|g" | tr "[A-Z]" "[a-z]"`
	fi

	if [ "$DISTRNAME" = "Slackware" ] ; then
		local NEWRESULT=`echo $GREP | sed -e "s|^ *\(.*\)-devel *\$|\1|g" `
	fi

	ALTPKGNAME=$GREP
	TARGETPKGNAME="$NEWRESULT"
	[ "$NEWRESULT" = "$GREP" ] && return 1

	# try to resolve recursive
	tolocal_anyrepl $NEWRESULT $@
	return 0
}

# Clean require names from various stuffs
clean_pkgreq()
{
	local i VAR
	VAR=`cat | sort -u`
	for i in $VAR ; do
		echo "$i" | egrep "gcc[0-9]|cpp[0-9]|gcc-c++[0-9]" >/dev/null && continue
		echo "$i" | egrep "gcc\$|cpp\$|gcc-c++\$" >/dev/null && continue
		echo "$i" | grep "[()<=>]" >/dev/null && continue
		echo "$i" | grep "^ *[0-9]\.[0-9]" >/dev/null && continue
		echo -n "$i "
	done | filter_strip_spaces
}

# Print list of all build requires in ALT notation
print_buildreq()
{
	eval_spec ${1} | grep "^Build.*Req" | sed -e "s|^.*:||g" | clean_pkgreq
}

# Print list of all pkg requires
print_pkgreq()
{
	eval_spec ${1} | grep "^Requires" | sed -e "s|^.*:||g" | clean_pkgreq
}

# Print list of all groups
print_grpreq()
{
	eval_spec ${1} | grep "^Group" | sed -e "s|^.*:||g" | sort -u | filter_strip_spaces
}

# FIXME: need improvement
# use tests/test_repl_find.sh for test it
# PKGVENDOR, PKGFORMAT, DISTRVERSION is already defined in detect_target_env() func
internal_repl_list()
{
	local REPLBASE="$1"
	local ARCHEXT="$2"
	local FINDPKG=$REPLBASE.$PKGVENDOR.$DISTRVERSION$ARCHEXT
	# sure we use our version firstly
	echo $FINDPKG
	#  Hack for sort by last numbers: sort -n -t . -k 3
	( ls -1 $REPLBASE.$PKGVENDOR*$ARCHEXT 2>/dev/null | \
			( test -n "$ARCHEXT" && grep -v "$PKGVENDOR$ARCHEXT\$" || grep -v "x86_64" ) ; echo $FINDPKG ) | \
		sort -u | sort -n -t . -k 3 | grep "^$FINDPKG\$" -B1000 | sort -r -n -t . -k 3
	echo $REPLBASE.$PKGVENDOR$ARCHEXT
	[ "$PKGVENDOR" = "alt" ] || echo "$REPLBASE.$PKGFORMAT$ARCHEXT "
}

# internal
print_replbased_list()
{
	local REPLBASE="$1"
	if [ "$BUILDARCH" = "x86_64" ] ; then
		internal_repl_list $REPLBASE .x86_64 | uniq
	fi

        # general rules listing
	internal_repl_list $REPLBASE "" | uniq
}

PKGREPLBASE=$ETERBUILDDIR/pkgrepl

# TODO: list in  alph. order and use if <= then our version
print_pkgrepl_list()
{
	print_replbased_list $PKGREPLBASE/pkgrepl
}

print_grprepl_list()
{
	print_replbased_list $(realpath -f $PKGREPLBASE/../grprepl/grprepl)
}


# Converts ALT Linux Sisyphus dependencies to target notation
# and print out result dependencies
# needs DISTRVENDOR, BUILDARCH, PKGFORMAT and so one
# call with package list
convert_pkgreqs_to_target()
{
	local listdep=$@ TARGETPKGNAME
	repl_list=$(print_pkgrepl_list)

	local j
	for j in $listdep; do
		tolocal_anyrepl $j $repl_list && estrlist list "$TARGETPKGNAME" || echo $j
	done | sort -u | sed -e "s|\\\\+|\+|g"
}

# Prints out buildreqs in target notation for SPEC (1st arg)
print_target_buildreq()
{
	local listdep=$(print_buildreq ${1})
	echo $(convert_pkgreqs_to_target $listdep)
}

