#! /bin/sh -efu
#
# Copyright (C) 2011  Aleksey Avdeev <solo@altlinux.ru>
#
# Common shell functions for cronbuild scripts.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

. cronbuild-sh-functions

get_langpackname()
{
	#
	# $1 -- specfile
	#

	sed -n -r '
	/^[[:space:]]*%define[[:space:]]+packagename[[:space:]]+[^[:space:]]+([[:space:]].*)?$/{
		s/^[[:space:]]*%define[[:space:]]+packagename[[:space:]]+([^[:space:]]+)([[:space:]].*)?$/\1/p
		q
	}' "$1"
}

get_branchname_lang()
{
	#
	# $1 -- pkgname
	#

	printf "%s\n" "ALT/$1/lang"
}

get_branchname_zip()
{
	#
	# $1 -- pkgname
	#

	printf "%s\n" "ALT/$1/zip"
}

get_moodlebranch()
{
	#
	# $1 -- specfile
	#

	local moodlebranch="`sed -n -r '
		/^[[:space:]]*%define[[:space:]]+moodlebranch[[:space:]]+[^[:space:]]+([[:space:]].*)?$/{
			s/^[[:space:]]*%define[[:space:]]+moodlebranch[[:space:]]+([^[:space:]]+)([[:space:]].*)?$/\1/p
			q
		}' "$1"`"
	if [ "$moodlebranch" = '%nil' ]; then
		moodlebranch="1.9"
	fi

	printf "%s\n" "$moodlebranch"
}

get_langpacksurl()
{
	#
	# $1 -- moodlebranch
	#

	printf "http://download.moodle.org/langpack/%s\n" "$1"
}

get_langbranchprefix()
{
	#
	# $1 -- moodlebranch
	#

	if [ "$moodlebranch" = "1.9" ]; then
		printf "ALT/moodle-lang-\n" "$1"
	else
		printf "ALT/moodle%s-lang-\n" "$1"
	fi
}
