#!/bin/sh
# Copyright 09/07/99 Sun Microsystems, Inc. All Rights Reserved.
#
#pragma ident  "@(#)var-install	1.36 99/09/07 Sun Microsystems"
#
# The install directory EM_MIS_HOME (dest) is typically /opt/SUNWconn/em
# The run-time directory EM_RUNTIME (runtime) where local and persistent storage is made
# is typically /var/opt/SUNWconn/em
#
SYSTEMID=`/usr/bin/uname -n`

EM_HOME="${EM_HOME-/opt/SUNWconn/em}"
EM_MIS_HOME="${EM_MIS_HOME-/opt/SUNWconn/em}"
PERLLIB=$EM_MIS_HOME/lib/perl:$EM_MIS_HOME/lib/omap:$EM_MIS_HOME/lib/netassist:$EM_MIS_HOME/lib/nerveCenter:$EM_MIS_HOME/lib/objviews
EM_RUNTIME="${EM_RUNTIME-/var/opt/SUNWconn/em}"
EM_HLOG_DIR="${EM_HLOG_DIR-$EM_RUNTIME/data/HLOG}"
OPENWINHOME="${OPENWINHOME-/usr/openwin}"
PATH=$PATH:$EM_MIS_HOME/bin:$EM_MIS_HOME/etc:$EM_MIS_HOME/lib
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/openwin/lib:$EM_MIS_HOME/lib
export PATH EM_MIS_HOME PERLLIB SYSTEMID EM_HOME EM_MIS_HOME EM_RUNTIME LD_LIBRARY_PATH
runtime=$EM_RUNTIME
dest=$EM_MIS_HOME
ECHO=/bin/echo

if [ ! -d  ${dest} ]; then
	$ECHO You have no $dest
	$ECHO "You can't install this until you do."
	exit 1
fi

# mode owner group name
makedir()
{
	mode=$1
	owner=$2
	group=$3
	shift ;shift ; shift
	/usr/bin/mkdir -m $mode -p $* ; /usr/bin/chown $owner $* ; /usr/bin/chgrp $group $*
#	ls -ld $*
}

# mode owner group from to
copyfile()
{
#	$ECHO $4 \-\> $5\\c
	/usr/bin/cp -rp $4 $5
	if [ -d $5 ]; then
		/usr/bin/find $5 -type r -exec /usr/bin/chown $2 \{\} \;
		/usr/bin/find $5 -type r -exec /usr/bin/chgrp $3 \{\} \;
		/usr/bin/find $5 -type r -exec /usr/bin/chmod $1 \{\} \;
	else
		/usr/bin/chown -R $2 $5 ; /usr/bin/chgrp $3 $5 ; /usr/bin/chmod $1 $5
	fi
#	$ECHO
}

#
# Build the run-time environment.
#

makedir 755 bin other ${runtime}
/bin/rm -rf ${runtime}/conf ${runtime}/data ${runtime}/usr

makedir 777 root other \
	${runtime}/conf \
	${runtime}/data \
	${runtime}/data/HLOG \
	${runtime}/data/ASN1 \
	${runtime}/data/MDR \
	${runtime}/usr \
	${runtime}/usr/data \
	${runtime}/usr/data/MDR \
	${runtime}/usr/data/ASN1 

copyfile 644 bin other	$EM_MIS_HOME/build/acct/EM-startup-1	$runtime/conf/EM-startup-1
copyfile 644 bin other	$EM_MIS_HOME/build/acct/EM-startup-2	$runtime/conf/EM-startup-2
copyfile 644 bin other	$EM_MIS_HOME/build/acct/EM-config	$runtime/conf/EM-config
copyfile 644 bin other	$EM_MIS_HOME/build/acct/init_user	$runtime/conf/init_user
copyfile 644 bin other	$EM_MIS_HOME/build/acct/init_access_privileges	$runtime/conf/init_access_privileges
copyfile 644 bin other	$EM_MIS_HOME/build/acct/init_access_user_apps	$runtime/conf/init_access_user_apps
copyfile 644 bin other	$EM_MIS_HOME/build/acct/restart_user	$runtime/conf/restart_user
copyfile 644 bin other	$EM_MIS_HOME/build/acct/em_pageable_classes	$runtime/conf/em_pageable_classes
if [ -f $EM_MIS_HOME/install/log2rdb/log2rdb.def ]; then
  copyfile 644 bin other  $EM_MIS_HOME/install/log2rdb/log2rdb.def $runtime/conf/log2rdb.def
  copyfile 644 bin other  $EM_MIS_HOME/install/log2rdb/log2rdb.cfg $runtime/conf/log2rdb.cfg
fi

# Initialize CMIB2GDMO compiler work files
cd ${runtime}/data/MDR
/usr/bin/sed -e "s,\$EM_HOME,$EM_HOME," < \
       $EM_HOME/etc/snmp_mibs/EM_REFERENCED_MIB > \
       $EM_RUNTIME/data/MDR/EM_REFERENCED_MIB
/usr/bin/chmod 666 $EM_RUNTIME/data/MDR/EM_REFERENCED_MIB

$ECHO "Compiling ASN.1 files ...\c"
cd ${dest}/etc/asn1
for i in *.asn1
do
    $ECHO ".\c"
    ${dest}/bin/em_asn1 -o ${runtime}/data/ASN1 $i 
    EX_STAT=$?
    if [ $EX_STAT -ne 0 ]; then
	$ECHO "\n**** Error detected compiling $i, exit status $EX_STAT, aborting."
 	exit 1
    fi
done

$ECHO "\nCompiling gdmo files ..."
cd ${dest}/etc/gdmo
for i in *.gdmo ; do
    ${dest}/bin/em_gdmo -o ${runtime}/data/MDR -f $i
    EX_STAT=$?
    if [ $EX_STAT -ne 0 ]; then
	$ECHO "**** Error detected compiling $i, exit status $EX_STAT, aborting."
 	exit 1
    fi
done
# chown bin *
/bin/rm -f trace.out
