#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: lib/version.sh
# Copyright (C) 2010 The OpenSDE Project
#
# More information can be found in the files COPYING and README.
#
# This program 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; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

# NAME
#	version.sh - determine OpenSDE version information 
#
# SYNOPSIS
# 	version.sh [-fp]
# 
# DESCRIPTION
#	The version.sh script determines OpenSDE version informations
#	by first checking if dedicated VERSION files for the OpenSDE
#	framework and the package tree do exist, for extracting the
#	version strings out of the files. In the case a VERSION file
#	does not exist or if it extracts an empty version string from
#	the an existing VERSION file it will assign a default version. 
#
#	If the version strings of the framework and the package tree
#	are not the same the returned version string will be composed
#	out of framework and the package tree version strings delimited
#	by an '-'. Otherwise it will only return the framework version
#	string.
#		
# OPTIONS
#	-f
#		only return the version of the OpenSDE framework
#	-p
#		only return the version of the OpenSDE package tree
#	

version_sh_usage() {
	echo "Usage:  $0 [-fp]" >&2
}

while [ $# -gt 0 ] ; do
	case "$1" in
	    -f)
		mode=fm ;;
	    -p)
		mode=pkg ;;
	    -*) version_sh_usage
		exit 1 ;;
	    *)
		break ;;
	esac
	shift
done

[ -n "$SDEROOT" ] || SDEROOT='.'

for x in fmver:etc/VERSION pkgver:package/VERSION; do
	k="${x%%:*}" f="${x#*:}"
	v=$(head -n1 "$SDEROOT/$f" 2> /dev/null)
	[ -n "$v" ] || v=master
	eval "$k='$v'"
done

case "$mode" in
	fm)	echo "$fmver" ;;
	pkg)	echo "$pkgver" ;;
	*)	if [ "$fmver" = "$pkgver" ]; then
			echo "$fmver"
		else
			echo "$fmver-$pkgver"
		fi
		;;
esac