#! /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_moodleversionfilename()
{
	#
	# Global vars:
	# moodleversionfilename
	#

	if [ -z "$moodleversionfilename" ]; then
		printf "version.php"
	else
		printf "%s" "$moodleversionfilename"
	fi
}

get_moodlemoduleversionvarname()
{
	#
	# Global vars:
	# moodlemoduleversionvarname
	#

	if [ -z "$moodlemoduleversionvarname" ]; then
		printf "module->version"
	else
		printf "%s" "$moodlemoduleversionvarname"
	fi
}

get_moodlemodulerequiresvarname()
{
	#
	# Global vars:
	# moodlemodulerequiresvarname
	#

	if [ -z "$moodlemodulerequiresvarname" ]; then
		printf "module->requires"
	else
		printf "%s" "$moodlemodulerequiresvarname"
	fi
}

get_moodlemodulereleasevarname()
{
	#
	# Global vars:
	# moodlemodulereleasevarname
	#

	if [ -z "$moodlemodulereleasevarname" ]; then
		printf "module->release"
	else
		printf "%s" "$moodlemodulereleasevarname"
	fi
}

get_modoole_app_version()
{
	local moduleversionvarname="`get_moodlemoduleversionvarname`"
	sed -r -n "/^[[:space:]]*\\\$$moduleversionvarname[[:space:]]*=[[:space:]]*[0-9]([0-9.]*[0-9])?[[:space:]]*;/s/^[[:space:]]*\\\$$moduleversionvarname[[:space:]]*=[[:space:]]*([0-9]([0-9.]*[0-9])?)[[:space:]]*;.*$/\\1/p"
}

get_modoole_app_requires()
{
	local modulerequiresvarname="`get_moodlemodulerequiresvarname`"
	sed -r -n "/^[[:space:]]*\\\$$modulerequiresvarname[[:space:]]*=[[:space:]]*[0-9]([0-9.]*[0-9])?+[[:space:]]*;/s/^[[:space:]]*\\\$$modulerequiresvarname[[:space:]]*=[[:space:]]*([0-9]([0-9.]*[0-9])?)[[:space:]]*;.*$/\\1/p"
}

get_modoole_app_release()
{
	local modulereleasevarname="`get_moodlemodulereleasevarname`"
	sed -r -n "/^[[:space:]]*\\\$$modulereleasevarname[[:space:]]*=[[:space:]]*(['\\\"])[^0-9]*[0-9]([0-9.]*[0-9])?([^[:space:]0-9][^[:space:]]*)?([[:space:]][^'\\\"]*)?\\1[[:space:]]*;/s/^[[:space:]]*\\\$$modulereleasevarname[[:space:]]*=[[:space:]]*(['\\\"])[[:space:]]*([^[:space:]0-9]*[0-9]([0-9.]*[0-9])?([^[:space:]0-9][^[:space:]]*)?([[:space:]][^'\\\"]*[^[:space:]'\\\"])?)[[:space:]]*\\1[[:space:]]*;.*$/\\2/p"
}

get_modoole_app_releasebuild()
{
	get_modoole_app_release \
		| sed -r -n '1{
			/^[^0-9]*[0-9]([0-9.]*[0-9])?([^[:space:]0-9][^[:space:]]*)?[[:space:]]*\([[:space:]]*([Bb][Uu][Ii][Ll][Dd]:?[[:space:]]*)?[0-9]+[[:space:]]*\).*$/s/^[^0-9]*[0-9]([0-9.]*[0-9])?([^[:space:]0-9][^[:space:]]*)?[[:space:]]*\([[:space:]]*([Bb][Uu][Ii][Ll][Dd]:?[[:space:]]*)?([0-9]+)[[:space:]]*\).*$/\4/p
		}'
}

get_modoole_app_releasenum()
{
	get_modoole_app_release \
		| sed -r -n '1{
			/^[^0-9]*[0-9]([0-9.]*[0-9])?([^[:space:]0-9][^[:space:]]*)?([[:space:]].*)?$/s/^[^0-9]*([0-9]([0-9.]*[0-9])?)([^0-9].*)?$/\1/p
		}'
}

get_modoole_app_releasenumXYZ()
{
	get_modoole_app_releasenum \
		| sed -r -n '1{
			/^[[:space:]]*[0-9]+[[:space:]]*$/{
				s/^[[:space:]]*([0-9]+)[[:space:]]*?$/\1.0.0/p
				q
			}
			/^[[:space:]]*[0-9]+\.[0-9]+[[:space:]]*$/{
				s/^[[:space:]]*([0-9]+\.[0-9]+)[[:space:]]*?$/\1.0/p
				q
			}
			/^[[:space:]]*[0-9]+(\.[0-9]+){2}[[:space:]]*$/{
				s/^[[:space:]]*([0-9]+(\.[0-9]+){2})[[:space:]]*?$/\1/p
				q
			}
		}'
}
