#!/usr/bin/sh
#=============================================================================
# Produit	: HP-Openview IT/O
# Nom		: ito_util
# Auteur	: S. GISLAIN
# Date Creation	: 19/04/1999
# Objet		: Environnement execution ITO.
# Appel(s)	: /logiciel/OpC/opcagt
# Agent		: /var/opt/OV/bin/OpC/cmds/
# Manager	: /var/opt/OV/share/databases/OpC/mgd_node/customer/sun/sparc/solaris/cmds/
#		: /SCRIPTS/cmds/
# Environnements: HP-UX 9.05 10.20
#		: Solaris 2.5 2.5.1 2.6 7
#		: Aix 3.x 4.2 4.3
#		: Irix 5.3
# Parametre(s)	: aucun
# Codes retour	: = 0 => OK
#		: <>0 => KO
#=============================================================================
# Historique
#-----------------------------------------------------------------------------
# 21/04/1999 - S.GISLAIN
#	Passage en Bourne
# 22/10/1999 - S.GISLAIN
#	Utilisation d'un fichier temporaire car dans un while les variables
#	ne peuvent etre exportee en Bourne Shell (Sun)
#
#=============================================================================
# Fonctions
#-----------------------------------------------------------------------------
#	main
#=============================================================================
# Variables
#
HostPhysique=`uname -n`		# Host Physique

				# Si phys- => suppression !!! pas dans le fichier
				# de configuration
NomCourt=`echo ${HostPhysique} | sed 's/^phys-//g'`

				# Fichier d'environnement
FIC_ENV="/var/opt/OV/bin/OpC/cmds/${NomCourt}"

FIC_TMP="/tmp/$$_`basename $0`"		# Fichier temporaire

FlagTrouve=0			# Flag premiere ligne paragraphe
Produit=""			# Produit trouve'
VarIndice=""			# Variable utilisee comme indice par produit

UBIN=/usr/bin
RM=${UBIN}/rm

#-----------------------------------------------------------------------------
# Partie principale
#
if [ ! -f ${FIC_ENV} ]
then
	echo "Fichier ${FIC_ENV} inexistant !"
else
				# Boucle de parcours du fichier d'environnement
	cat ${FIC_ENV} | egrep -v "(^#|^$)" | sed 's/	/;/g' | while read Ligne
	do
				# Decoupage des champs de la ligne
		ChampUn=`echo "$Ligne" | awk -F";" '{ print $1 }'`
		ChampDeux=`echo "$Ligne" | awk -F";" '{ print $2 }'`
		ChampTrois=`echo "$Ligne" | awk -F";" '{ print $3 }'`

				# Premiere ligne du paragraphe
		if [ -n "${ChampUn}" ]
		then			# Host concerne ?
			if [ "${ChampUn}" = "${HostPhysique}" ]
			then		# Flag trouve
				FlagTrouve=1

				# Produit : ORA, CFT, MQS, ...
				Produit=${ChampDeux}

				# Compteur de nb d'instance pour le produit
				VarIndice="ITO_${Produit}_IND"

				echo "unset ${VarIndice}"

				# Chaine d'evaluation de l'indice
				ValIndice=`eval echo \$\{""${VarIndice}:-\"\"\}`

				# Initialisation ou incrementation du compteur
				if [ -z "${ValIndice}" ]
				then		# Le compteur n'existe pas, donc on le cree
					eval ${VarIndice}=1
				else		# Le compteur existe mais on doit verifier si le flag existe
						eval ${VarIndice}=` expr ${ValIndice} + 1 `
				fi

			else		# Autre paragraphe a ignorer
				if [ ${FlagTrouve} -eq 1 ]
				then
					eval unset ${VarIndice}
				fi

				FlagTrouve=0
			fi
					# Elements du paragraphe
		elif [ ${FlagTrouve} -eq 1 ]
		then
			ValIndice=`eval echo \$""ITO_${Produit}_IND`

			VarVariable="ITO_${Produit}_${ChampDeux}_${ValIndice}"

			echo "${VarVariable}=${ChampTrois}"
			echo "export ${VarVariable}"
		fi
	done > ${FIC_TMP}

	. ${FIC_TMP}

	${RM} -f ${FIC_TMP}

	#eval unset `echo ${ListeIndice}`

	unset Ligne ChampUn ChampDeux ChampTrois FlagTrouve Produit VarIndice ValIndice VarVariable
fi
#
# eof
#=============================================================================
