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



get_linked_system()
{
	grep "^$1 " $KORINFETC/linked | cut -d" " -f2
}

get_linked_x86_64_wine_system()
{
	grep "^$1 " $KORINFETC/linked-x86_64-wine | cut -d" " -f2
}


# get list from TARGETPATH/distro.list or use lists/$ALL
# arg: ALL or other full list
get_target_list()
{
	local TLIST RLIST
	local FILE=$1

	[ -n "$TARGETPATH" ] && TLIST=$TARGETPATH/distro.list
	# for compatibility
	[ "$FILE" = "x86_64" ] && FILE="x86_64/ALL"

	RLIST=$KORINFETC/lists/$FILE
	if [ "$FILE" = "ALL" ] || [ "$FILE" = "x86_64/ALL" ]; then
		[ -f "$RLIST" ] && TLIST=$RLIST || fatal "Can't read ALL list"
	else
		# if can't find distro.list, use lists/
		[ ! -r "$TLIST" ] && [ -f "$RLIST" ] && TLIST=$RLIST
	fi
	[ -n "$TLIST" ] && [ -r "$TLIST" ] && [ -f "$TLIST" ] || TLIST=$KORINFETC/lists/ALL
	# || fatal "get_target_list failed to get list of targets from $TLIST"
	echo $TLIST
}

check_target_dist()
{
	local DIST="$1"
	[ -n "$DIST" ] || fatal "missed arg for check_target_dist"
	get_distro_list $(get_target_list "") | grep -q "^$DIST$" || get_distro_list $(get_target_list "ALL") | grep -q "^$DIST$"
}

get_real_target_dist()
{
	#assert_var LOCALLINUXFARM
	local DIST="$1"
	check_target_dist "$DIST" || fatal "Unknown distro $DIST"
	local LINKED=$(get_linked_system "$DIST")
	[ -n "$LINKED" ] && DIST="$LINKED"
	# TODO: check this test with ALT Linux and FreeBSD
	# test -d "$LOCALLINUXFARM/$DIST/dev" || fatal "$LOCALLINUXFARM/$DIST is not correct system chroot"
	echo "$DIST"
}


# Set REBUILDLIST according TARGETPATH (if exists), existing REBUILDLIST and some heruistic
set_rebuildlist()
{
	assert_var KORINFETC
	local RESULT
	if [ -n "$REBUILDLIST" ] && [ ! "$REBUILDLIST" = "all" ] ; then
		# Test for absolute path
		if [ -r "/$REBUILDLIST" ] ; then
			RESULT="Use distros from full path $REBUILDLIST"
		elif [ -r "$KORINFETC/lists/$REBUILDLIST" ] ; then
			# for compatibility
			[ "$REBUILDLIST" = "x86_64" ] && REBUILDLIST="x86_64/ALL"
			REBUILDLIST=$KORINFETC/lists/$REBUILDLIST
			RESULT="Use distros from $REBUILDLIST"
		else
			# Build for concrete target if it is exists
			check_target_dist $REBUILDLIST || fatal "Run with unknown target '$REBUILDLIST'"
			RESULT="Use $REBUILDLIST target"
		fi
	else
		# if rebuild list is empty or all - use distro.list if possible, ALL in other cases
		REBUILDLIST=$(get_target_list "")
		RESULT="Use distros from $REBUILDLIST"
	fi
	[ -n "$QUIET" ] || echo $RESULT
}

