#!/usr/bin/ksh93

#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# =============================================================================
# vmc_common: Contains code common to multiple vmc finalizer scripts.
# =============================================================================
#

#
# Don't set PATH, rely on the PATH set in the caller.
#


typeset VMC_NEW_ISO=""

#######################################################################
#
# vmc_set_locale
#
# Make sure all math stuff runs in the "C" locale to avoid problems
# with alternative radix point representations (e.g. ',' instead of
# '.' in de_DE.*-locales). This needs to be set _before_ any
# floating-point constants are defined in this script).
#
# Input: none
# Returns: none
#
#######################################################################
#
#
function vmc_set_locale
{
	if [[ "${LC_ALL}" != "" ]] ; then
		export \
		LC_MONETARY="${LC_ALL}" \
		LC_MESSAGES="${LC_ALL}" \
		LC_COLLATE="${LC_ALL}" \
		LC_CTYPE="${LC_ALL}"
		unset LC_ALL
	fi
	export LC_NUMERIC=C

}

#######################################################################
#
# vmc_is_vbox_installed
#
# Check if Virtual Box is installed and validate the version is
# at least 3.
#
# Input: none
# Returns:
#   1 - The required VirtualBox version is not installed
#   0 - The required VirtualBox version is installed
#
#######################################################################
function vmc_is_vbox_installed
{
	#
	# Check if Virtual Box is installed
	#
	typeset vbox_ver_str      # The VBoxManage version string
	typeset -i vbox_major_ver # The major VBoxManage version number

	vbox_ver_str=$(VBoxManage -v 2>&1)
	cmdsts=$?
	if [[ ${cmdsts} != 0 ]] ; then
		print -u2 "\nError: VirtualBox is not installed"
		return 1 # error
	fi

	#
	# Virtual Box is installed. Also confirm it is at least version 3
	# Remove everything to the right of and including the left most "."
	#
	vbox_major_ver=${vbox_ver_str%%.*} 
	if [[ ${vbox_major_ver} < 3 ]] ; then
		print -u2 "\nError: VirtualBox must be at least"\
		    "version 3. The version installed is: ${vbox_ver_str}"
		return 1 # error
	fi

	#
	# Success - The required VirtualBox version is installed
	#
	return 0

}


#######################################################################
#
# vmc_set_new_iso_path
#
# Set the path to the newly modified ISO image. This is done in
# vmc_common function so it is set in one place.
#
# Input:
#	modified_iso_dir - The path to the modified ISO image.
# Returns:
#   1 - File not found
#   0 - Path set and file aleady exists
#
#######################################################################
function vmc_set_new_iso_path
{
	modified_iso_dir=$1
	VMC_NEW_ISO="${modified_iso_dir}/vmc_modified.iso"

	if [[ ! -f ${VMC_NEW_ISO} ]] ; then
		return 1 # File not found
	fi

	return 0

}

#######################################################################
# vmc_error_handler and vmc_error_handler_trap
#	The vmc_error_handler will set up the traps,
#	invoke cleanup to clean up any temporary resources then
#	exit with an error or 1.
#
#	This function is not defined using the function keyword
#	to avoid an exit loop.
#
#	vmc_error_handler_trap is the entry point registered with
#	the trap command for interupts (INT). Using this extra
#	level allows arguments to be passed to vmc_error_handler().
#
# Input:
#	stat - error status code to act upon
#       msg  - message to issue if an error_code is not success
#
# Returns: none
#
#######################################################################
vmc_error_handler_trap ()
{
	print -u2 "\nInvoking: vmc_error_handler_trap"
	vmc_error_handler 1 "Interrupt encountered. Exiting"
}

vmc_error_handler ()
{
	typeset -i stat=$1
	typeset    msg="$2"

	if [[ ${stat} != 0 ]] ; then
		print -u2 "\nInvoking: vmc_error_handler"

		trap "" ERR INT
		set +o errexit

		print -u2 "${msg}"

		#
		# cleanup must be defined in the caller.
		#
		cleanup

		exit 1
	fi
}
