#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_get_arch.sh 1.5 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2000,2003 
# 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 
# @(#)08    1.5  src/bos/usr/lib/nim/methods/c_get_arch.sh, cmdnim, bos720  10/1/03  17:22:53 
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_get_arch.sh
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="location type"
OPTIONAL_ATTRS=""
typeset obj_arch=""
typeset arch_type=""
typeset location=""
src_access=""
typeset type=""

#---------------------------- prep_tape         --------------------------------
#
# NAME: prep_tape  
#
# FUNCTION:
#		prepares the specified tape device for use
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#		calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function prep_tape {

	# tape needs a "no-rewind-on-close" extension 
	# was one provided?
	if [[ ${location} = /dev/rmt[0-9].* ]]
	then

		[[ ${location} != /dev/rmt[0-9].1 ]] && \
			[[ ${location} != /dev/rmt[0-9].5 ]] && \
			error ${ERR_SOURCE} ${location}

		src_access=${location}

	else

		# append the extension
		src_access="${location}.5"

	fi

	# rewind the tape
	${TCTL} -f ${src_access} rewind 2>${ERR} || err_from_cmd tctl

	# position to 4th record
	${TCTL} -f ${src_access} fsf 3 2>${ERR} || err_from_cmd tctl

	# need to validate the AIX release level of this image, so we need the
	# 		the LPP_NAME file - restore it now
	cd ${TMPDIR} 2>${ERR} || err_from_cmd cd
	${RESTORE} -xqf ${src_access} ${LPP_NAME} >/dev/NULL 2>${ERR} || \
		err_from_cmd "${RESTORE} -xqf ${src_access} ${LPP_NAME}"

	# grab the arch from here.
	arch_type=`/usr/bin/head -n1 ${TMPDIR}/${LPP_NAME} 2>/dev/null | ${AWK} '{print $2}'`

} # end of prep_tape

#---------------------------- prep_cd           --------------------------------
#
# NAME: prep_cd
#
# FUNCTION:
#		determines the architectural type of an image CD-ROM, if possible
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#		calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function prep_cd {

	# mount the CDROM
	nim_mount ${location}
	[[ ! -e "${access_pnt}/${BOS_PATH_ON_CDROM}" ]] && \
		BOS_PATH_ON_CDROM=${BOS_PATH_ON_CDROM_OLD}
	src_access=${access_pnt}/${BOS_PATH_ON_CDROM}

	# validate that the bos image exists in the expected directory.
	# otherwise this must be some other type of CD.
	if [[ -f ${src_access} ]]
	then

		# need to get the architecture from this image,  so we need the
		# 		the LPP_NAME file - restore it now
		cd ${TMPDIR} 2>${ERR} || err_from_cmd cd
		${RESTORE} -xqf ${src_access} ${LPP_NAME} >/dev/null 2>${ERR} || \
			err_from_cmd ${RESTORE}
	
		# grab the arch from here.
		arch_type=`/usr/bin/head -n1 ${TMPDIR}/${LPP_NAME} 2>/dev/null | ${AWK} '{print $2}'`
	else
		arch_type="R"
	fi

} # end of prep_cd

#---------------------------- loc_SPOT         --------------------------------
#
# NAME: loc_SPOT
#
# FUNCTION:
#		determines the architectural type of a SPOT, if possible
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#		calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function loc_SPOT {

	typeset lpp_file_loc=""

	# make sure source pathname ends with "/usr"
	if [[ "${location##*/}" != usr ]]
	then
		location=${location}/usr
	fi

	lpp_file_loc=${location%/usr}/${LPP_NAME}

	# attempt to pull the value off the first line of the lpp_name file
	[[ -f ${lpp_file_loc} ]] &&
		arch_type=`/usr/bin/head -n1 ${lpp_file_loc} 2>/dev/null | ${AWK} '{print $2}'`

        # if we haven't found it yet let's default to the server platform
	if [[ -z ${arch_type} ]] || [[ ${location} = /usr ]] 
	then
		arch_type=`get_mac_arch`
	fi

} # end of loc_SPOT

#---------------------------- loc_LPP         --------------------------------
#
# NAME: loc_LPP       
#
# FUNCTION:
#		determines the architectural type of a lpp_source, if possible
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#		calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function loc_LPP {

	# verify the location of the lpp_source exists
	${C_STAT} -a location=${location} 2>${ERR} || err_from_cmd ${C_STAT}

	# need to determine the architectural type of this lpp_source.  The 
        #  data will reside in the LPP_NAME file - restore it now
	if [[ -f ${location}/bos ]]
	then
		cd ${TMPDIR} 2>${ERR} || err_from_cmd cd
		${RESTORE} -xqf ${location}/bos ${LPP_NAME} >/dev/null 2>${ERR} || \
			err_from_cmd ${RESTORE}

		# grab the arch from here
		[[ -f ${TMPDIR}/${LPP_NAME} ]] &&
			arch_type=`/usr/bin/head -n1 ${TMPDIR}/${LPP_NAME} 2>/dev/null | ${AWK} '{print $2}'`
	fi

        # if we haven't found it yet let's try the .toc in the lpp_source
	if [[ -z ${arch_type} ]] && [[ -f ${location}/.toc ]]
	then
		arch_type=`${AWK} '/^bos / {
			if ($5 == "bos" && $6 == "{" && $4 == "O" ) 
				print $3 
			}' ${location}/.toc 2>/dev/null `
	fi

        # if we haven't found it yet let's default to the server platform
	if [[ -z ${arch_type} ]]
	then
		arch_type=`get_mac_arch`
	fi


} # end of loc_LPP

#---------------------------- loc_mksysb         ------------------------------
#
# NAME: loc_mksysb       
#
# FUNCTION:
#		determines the architectural type of a mksysb, if possible
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#		calls error on failure
#       Mksysbs created on 4.2 and later will contain a line in the form
#       "OSLEVEL= V.R.M.F" in the image.data file that holds the release
#       information.  For mksysbs created on 4.1, the uname information 
#       will continue to be used to retrieve the release information.
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function loc_mksysb {

	typeset datafile=${TMPDIR}/${IMAGE_DATA}
	typeset platform=""

	# filename containing mksysb must exist and must be local to server
	${C_STAT} -a location=${location} -a mode=0100000 2>${ERR} \
		|| err_from_cmd ${C_STAT}

	# for mksysb images, the platform info is in the image.data file
	# restore that file now
	cd ${TMPDIR} 2>${ERR} || err_from_cmd cd
	${RESTORE} -xqf ${location} ${IMAGE_DATA} >/dev/null 2>${ERR} || \
		err_from_cmd ${RESTORE}

	# IMAGE_DATA readable?
	[[ ! -r "${datafile}" ]] && error ${ERR_FILE_ACCESS} ${datafile}

	# PLATFORM specified in IMAGE_DATA?
	# if not, then arch is POWER
	platform=$( ${AWK} '/PLATFORM=/ {if ($1=="PLATFORM=") {print}}' ${datafile} 2>/dev/null )
	if [[ -n "${platform}" ]]
	then
		# remove the PLATFORM string
   	    platform=`echo ${platform} | ${CUT} -d"=" -f2`
		arch_type=`convert_arch_value ${platform}`
	else
		arch_type="R"
	fi

} # end of loc_mksysb

#---------------------------- ck_location       --------------------------------
#
# NAME: ck_location
#
# FUNCTION:
#		examines the specified location for its architectural type
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#		calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function ck_location {

	# what kind of location?
	case ${type} in

		device)		# tape or CDROM or path?
				if [[ ${location} = /dev/rmt[0-9]* ]]
				then
					prep_tape
				elif [[ ${location} = /dev/cd[0-9]* ]]
				then
					prep_cd
				elif [[ -a ${location} ]]
				then
					loc_LPP	
				else
					error ${ERR_SOURCE} ${location}
				fi
				;;

		spot)		# location is a SPOT
				loc_SPOT
				;;

		mksysb)		# location is a mksysb
				loc_mksysb
				;;

		lpp_source)	# location is a lpp_source
				loc_LPP
				;;

		*)		# unknown type
				error ${ERR_SYS} "unknown location type - \"${type}\""
				;;

	esac

} # end of ck_location

#---------------------------- c_get_arch         --------------------------------
#
# NAME: c_get_arch
#
# FUNCTION:
#		examines the architecture type for a specified location.
#		if it is at an acceptable architecture to NIM, this program will 
#       exit with 0; otherwise, it prints an error message and exits non-zero
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0			= architecture type ok
#		!= 0		= error encountered - message on stderr
#
# OUTPUT:
#-------------------------------------------------------------------------------

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# set parameters from command line
while getopts :a:qv c
do
	case ${c} in

		a)		# validate the attr ass
				parse_attr_ass "${OPTARG}"

				# include the assignment for use in this environment
				eval ${variable}=\"${value}\"
				;;

		q)		# show attr info
				cmd_what
				exit 0
				;;

		v)		# verbose mode (for debugging)
				set -x
				for i in $(typeset +f)
				do
					typeset -ft $i
				done
				;;

		\?)	# unknown option
				error ${ERR_BAD_OPT} ${OPTARG}
				;;
	esac
done

# check for missing attrs
ck_attrs

# check the specified source
ck_location

# convert the value in the arch_type to a value that NIM is
#  expecting.
obj_arch=`convert_arch_value ${arch_type}`

	
# if we get this far, architecture type is ok
# return the architecture type on stdout as attr assignments so the calling
#		method will pick them up
[[ -n ${obj_arch} ]] &&
	print "arch=${obj_arch}" ||
	exit 1

# all done
exit 0

