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

. $ETERBUILDDIR/functions/rpm

check_locking()
{
	if [ -f $HASHERDIR/lockdir/lockfile ]
	then
		echo
		echog "Hasher $HASHERDIR in use at `date`"
		echo -n "Wait for unlocking"
		while test -f $HASHERDIR/lockdir/lockfile
		do
			echo -n "."
			sleep $(($RANDOM%5+3))
		done
		echo
	fi
}

# workaround about hard coded path to sources.list in apt.conf
print_tmp_aptconf()
{
	test -r "$1" || fatal "$1 is missed"
	# Note: /etc/apt and /etc/eterbuild/apt is supported
	cat "$1" | \
		sed -e "s|\".*/sources.list|\"$OURTMPDIR/sources.list|g"
}

# printout sources.list with filtering
# workaround about hard coded arch in sources.list
print_tmp_sourceslist()
{
	local SLIST
	test -r "$1" || fatal "$1 is missed"
	# Note: /etc/apt and /etc/eterbuild/apt is supported
	SLIST=`cat "$1" | \
		sed -e "s|\"/etc/.*apt/sources.list|\"$ETERBUILDETC/apt/sources.list|g" | \
		grep "/sources.list" "$1" | sed -e 's|.*"\(.*\)".*|\1|g'`
	#SLIST="$ETERBUILDETC/apt/`basename $SLIST`"
	test -r "$SLIST" || fatal "'$SLIST' - sources.list path from $1 is missed"
	cat "$SLIST" | \
		sed -e "s|i586|$DEFAULTARCH|g"
}

prepare_aptconfig()
{
	print_tmp_aptconf $APTCONF >$OURTMPDIR/apt.conf.$MENV || fatal "Can't create apt.conf"
	export OURAPTCONF=$OURTMPDIR/apt.conf.$MENV
	print_tmp_sourceslist $APTCONF >$OURTMPDIR/sources.list.$MENV || fatal "Can't create sources.list"
	export OURSOURCES=$OURTMPDIR/sources.list.$MENV
}
