#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/db2/KLIB_DB2_swversion.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG
# @(#)  7d4c34b 43haes/lib/ksh93/db2/KLIB_DB2_swversion.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
#=head1 NAME
#
# KLIB_DB2_swversion - Obtain the DB2 software version running on the
#                      local node
#
#=head1 SYNOPSIS
#
# swversion=$(KLIB_DB2_swversion)
#
#=head1 DESCRIPTION
#
# Uses lslpp to determine the software level of the currently
# installed version of DB2, only extracts the version level
# (user visible version #) so if the lslpp output is 8.1.1.4 then
# the software level will be 8.1, alternatively if the level of
# DB2 software is 8.1.1.6 or above, then the level reported will
# be 8.2.
#
#=head1 ARGUMENTS
#
# None
#
#=head1 RETURN
#
#   0: success
#	1: failed
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_swversion
{
	. /usr/es/lib/ksh93/func_include

	lslpp -Lc 'db2*' 1>/dev/null 2>&1
	if [[ $? == 0 ]]; then
		lslpp -Lc 'db2*' | 
		while IFS=: read image tmp version tmp; do
                	if [[ ${image/*db2.engn/found} == "found" ]]; then
				echo $version | IFS='.' read a b c d e
				[[ "$a.$b.$c" == "8.1.1" ]] && (( $d > 6 )) && {
					echo "8.2"
					return 0
				}
				echo "$a.$b"
				return 0
			fi
		done
	else
		#
		# Starting from DB2 Version 9.0 onwards, it doesn't require any more OS support
		# during installation, we can't use 'lslpp' command to determine the DB2 software
		# installation. We are exporting the variable "DSE_INSTALL_DIR" with DB2 installed
		# location.
		#
		if [[ -d $DSE_INSTALL_DIR ]]; then
			version=$($DSE_INSTALL_DIR/install/db2ls 2>/dev/null | tail -1 | awk '{print $2}' | cut -f 1,2 -d '.')
			if [[ -n $version ]]; then
				echo $version
				return 0	
			fi
		fi
	fi
	echo ""
	return 1
}
