#!/bin/bash
# 2008-2015 Etersoft http://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]"
}

build32on64()
{
	# FIXME: wine hack, use external replacement package list
	[ "$BUILDARCH" = "x86_64" ] && rhas "$BUILDNAME" wine
}

# Hack for improve requires for 32-bit build on 64-bit system
# See rpmbph also
add_32bit_requires()
{
	assert_var DISTRNAME BUILDARCH BUILDNAME DISTRVERSION

	[ -z "$1" ] && return
	[ -z "$2" ] && return

	local GREP="$1"
	shift

	if build32on64 ; then
		if [ "$DISTRNAME" = "Fedora" ] || [ "$DISTRNAME" = "Scientific" ] || [ "$DISTRNAME" = "GosLinux" ] || [ "$DISTRNAME" = "CentOS" -a "$DISTRVERSION" != 5 ]; then
			# http://www.rpm.org/wiki/PackagerDocs/ArchDependencies
			# add (x86-32) to all -devel packages
			for i in $* ; do
				rhas "$GREP" "-devel$" && ! rhas "$i" "(x86-32)$" && echo "$i(x86-32)" && continue
				echo "$i"
			done | xargs -n 1000 echo
		elif [ "$DISTRNAME" = "Ubuntu" ] && version_more_version $DISTRVERSION 12.04 || [ "$DISTRNAME" = "Debian" ] && version_more_version $DISTRVERSION 7 ; then
			# i386 on Debian/Ubuntu: # add :i386 to all -dev packages
			for i in $* ; do
				rhas "$GREP" "-devel$" && ! rhas "$i" ":i386$" && echo "$i $i:i386" && continue
				echo "$i"
			done | xargs -n 1000 echo
		elif [ "$DISTRNAME" = "ArchLinux" ] ; then
			# i386 on ArchLinux: add lib32- to all lib packages
			for i in $* ; do
				rhas "$GREP" "^lib" && ! rhas "$i" "^lib32-" && echo "lib32-$i" && continue
				echo "$i"
			done | xargs -n 1000 echo
		elif [ "$DISTRNAME" = "SUSE" ] || [ "$DISTRNAME" = "SLED" ] || [ "$DISTRNAME" = "SLES" ]; then
			# i386 on SUSE or SLED : # add -32bit to all lib packages
			for i in $* ; do
				rhas "$GREP" "^lib" && ! rhas "$i" "-32bit$" && echo "$i-32bit" && continue
				echo "$i"
			done | xargs -n 1000 echo
		else
			echo "$@"
		fi
	else
		echo "$@"
	fi
}

# Part of local hack
hack_distr_requires()
{
	local GREP="$1"

	case "$PKGFORMAT" in
		"deb")
			echo "$GREP" | filter_deb_pkgnames
			return
			;;
	esac

	case "$DISTRNAME" in
		"Mandriva")
			if [ $BUILDARCH = "x86_64" ] && ! build32on64 ; then
				echo "$GREP" | sed -e "s|^lib\([^6]\)|lib64\1|g"
				return
			fi
			;;
		"ArchLinux")
			echo "$GREP" | sed -e "s|^ *\(.*\)-devel *\$|\1|g" | tr "[A-Z]" "[a-z]"
			return
			;;
		"FreeBSD")
			echo "$GREP" | sed -e "s|^ *\(.*\)-devel *\$|\1|g"
			return
			;;
		"Slackware"|"Gentoo")
			echo "$GREP" | sed -e "s|^ *\(.*\)-devel *\$|\1|g"
			return
			;;
	esac

	echo "$GREP"
}

# Get replacement rule for ALT package to local in $1 (scan for files in $@)
# sets ALTPKGNAME, TARGETPKGNAME variable
# used for hack: PKGFORMAT, DISTRNAME, BUILDARCH
# BUILDNAME used in add_32bit_requires for hack
tolocal_anyrepl()
{
	assert_var PKGFORMAT DISTRNAME BUILDNAME
	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
		rhas "$REPLRULE" "|" || 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" && TARGETPKGNAME=$(add_32bit_requires "$GREP" "$TARGETPKGNAME") && return 0
	done

	local NEWRESULT=$(hack_distr_requires "$GREP")

	FIXNEWRESULT=$(add_32bit_requires "$GREP" $NEWRESULT)

	ALTPKGNAME="$GREP"
	TARGETPKGNAME="$FIXNEWRESULT"

	# return if no chance to replace
	[ "$FIXNEWRESULT" = "$GREP" ] && return 1
	# HACK: return if we get complex result
	[ "$FIXNEWRESULT" != "$NEWRESULT" ] && return 0

	# this recursive needs to be argumented
	# try to resolve recursive
	#tolocal_anyrepl "$FIXNEWRESULT" $@
	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 " | sed -e "s|[,}]||g"
	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
internal_repl_list()
{
	# need be defined in detect_target_env()
	assert_var PKGVENDOR PKGFORMAT DISTRVERSION
	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
		if build32on64 ; then
			internal_repl_list $REPLBASE .x86_64-i586 | uniq
		else
			internal_repl_list $REPLBASE .x86_64 | uniq
		fi
	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 $PKGREPLBASE/../grprepl/grprepl)
}


# Converts ALT Linux Sisyphus dependencies to target notation
# and print out result dependencies
# call with package list
convert_pkgreqs_to_target()
{
	assert_var DISTRVENDOR BUILDARCH PKGFORMAT
	local repl_list
	repl_list=$(print_pkgrepl_list)

	local j
	for j in "$@" ; 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 $(BUILDNAME="$1" convert_pkgreqs_to_target $listdep)
}

