#!/bin/sh
# Copyright 01/05/00 Sun Microsystems, Inc. All Rights Reserved.
#
#pragma ident  "@(#)var-install	1.50 00/01/05 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=$EM_MIS_HOME/bin:$EM_MIS_HOME/etc:$EM_MIS_HOME/lib:$PATH
LD_LIBRARY_PATH=$EM_MIS_HOME/lib:$LD_LIBRARY_PATH:/usr/openwin/lib

export PATH EM_MIS_HOME PERLLIB SYSTEMID EM_HOME EM_MIS_HOME EM_RUNTIME \
       LD_LIBRARY_PATH

ROOT_OWNER=informix
BIN_OWNER=informix
GROUP=informix

runtime=$EM_RUNTIME
dest=$EM_MIS_HOME
configloc="/etc/opt/SUNWconn/em"


###############################################################################
# errmsg  
###############################################################################
#
# Echo the arguments.
#
###############################################################################

errmsg() 
{
    if [ "$1" = "-n" ] ; then
        shift
        gettext var-install "$*"                     # no newline
    else
        gettext var-install "$*\\n"
    fi
}


###############################################################################
# debugmsg  
###############################################################################
#
# Echo the arguments only if the -d option hasn't been given. 
#
###############################################################################

debugmsg() 
{
    gettext var-install "$*\\n" > ${debug_device:-/dev/null} 2>&1
}


###############################################################################
# progress
###############################################################################
#
# Mark the passage of time to convince the user we haven't really hung at all.
#
###############################################################################

progress()
{
    if [ "${quiet:-FALSE}" = TRUE -o "${debug:-FALSE}" = TRUE ] ; then
	return
    fi

    progress_counter=`expr ${progress_counter:-1} + 1`   
    position=`expr $progress_counter % 4`

    if [ $position -eq 0 ] ; then
	echo '|\b\c'
    elif [ $position -eq 1 ] ; then
	echo '/\b\c'
    elif [ $position -eq 2 ] ; then
	echo '-\b\c'
    else
	echo '\\\b\c'
    fi
}


###############################################################################
# makedir  
###############################################################################
#
# Make a directory.  Usage: makedir 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 $*
}



###############################################################################
# copyfile  
###############################################################################
#
# Copy a file.  Usage: copyfile mode owner group from to.
#
###############################################################################

copyfile()
{
    /usr/bin/cp -rp $4 $5
    if [ -d $5 ]; then
	/usr/bin/find $5 -type r -exec /usr/bin/chown $2 \{\} \;
	progress
	/usr/bin/find $5 -type r -exec /usr/bin/chgrp $3 \{\} \;
	progress
	/usr/bin/find $5 -type r -exec /usr/bin/chmod $1 \{\} \;
	progress
    else
	/usr/bin/chown -R $2 $5 ; /usr/bin/chgrp $3 $5 ; /usr/bin/chmod $1 $5
    fi
}

#
# Build the run-time environment.
#

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

makedir 755 $BIN_OWNER  $GROUP ${runtime}
makedir 775 $ROOT_OWNER $GROUP ${runtime}/data

/bin/rm -rf ${runtime}/conf ${runtime}/data/HLOG ${runtime}/data/ASN1 \
	    ${runtime}/data/MDR ${runtime}/usr ${runtime}/debug
progress

makedir 777 $ROOT_OWNER $GROUP \
    ${runtime}/conf \
    ${runtime}/data/HLOG \
    ${runtime}/data/ASN1 \
    ${runtime}/data/MDR \
    ${runtime}/usr \
    ${runtime}/usr/data \
    ${runtime}/usr/data/MDR \
    ${runtime}/usr/data/ASN1 \
    ${runtime}/debug
progress

copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/EM-startup-1 \
				$runtime/conf/EM-startup-1
progress
copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/EM-startup-2 \
				$runtime/conf/EM-startup-2
progress
copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/EM-config \
				$runtime/conf/EM-config
progress
copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/init_user \
				$runtime/conf/init_user
progress
copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/init_access_privileges \
				$runtime/conf/init_access_privileges
progress
copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/init_access_user_apps \
				$runtime/conf/init_access_user_apps
progress
copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/restart_user \
				$runtime/conf/restart_user
progress
copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/em_pageable_classes \
				$runtime/conf/em_pageable_classes
progress
copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/em_autoexd.cfg \
				$runtime/conf/em_autoexd.cfg
progress

if [ -f $EM_MIS_HOME/install/log2rdb/log2rdb.def ]; then
    copyfile 644 $BIN_OWNER $GROUP  $EM_MIS_HOME/install/log2rdb/log2rdb.def \
			    	    $runtime/conf/log2rdb.def
    progress
    copyfile 644 $BIN_OWNER $GROUP  $EM_MIS_HOME/install/log2rdb/log2rdb.cfg \
			    	    $runtime/conf/log2rdb.cfg
    progress
fi

#
# GDMO/ASN1 compiler - USEFUL.MIB install
#

makedir 755 $BIN_OWNER $GROUP ${configloc}
progress

makedir 777 $ROOT_OWNER $GROUP \
	${configloc}/conf \
	${configloc}/conf/compilers
progress

copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/config/USEFUL.mib	\
				$configloc/conf/compilers/USEFUL.mib
progress

# 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

debugmsg "Compiling ASN.1 files..."

cd ${dest}/etc/asn1
for i in *.asn1; do
    debugmsg "\t$i"
    ${dest}/bin/em_asn1 -o ${runtime}/data/ASN1 -f $i > \
	${debug_device:-/dev/null} 2>&1
    EX_STAT=$?
    if [ $EX_STAT -ne 0 ]; then
	errmsg -n "\\n**** Error detected compiling "
	errmsg -n "$i, " 
	errmsg -n "exit status "
	errmsg "$EX_STAT"
    fi
    progress
done

debugmsg "Compiling GDMO files..."
cd ${dest}/etc/gdmo
for i in *.gdmo ; do
    ${dest}/bin/em_gdmo -o ${runtime}/data/MDR -f $i > \
	${debug_device:-/dev/null} 2>&1

    EX_STAT=$?
    if [ $EX_STAT -ne 0 ]; then
	errmsg -n "\\n**** Error detected compiling "
	errmsg -n "$i, " 
	errmsg -n "exit status "
	errmsg "$EX_STAT"
    fi
    progress
done

#
# Install CMIP AutoRegistration Database
#

makedir 777 $ROOT_OWNER $GROUP \
    ${configloc}/conf \
    ${configloc}/conf/cmipautoreg
progress

copyfile 644 $BIN_OWNER $GROUP	$EM_MIS_HOME/build/acct/EM_CMIP_AUTOREG.DB \
				$configloc/conf/cmipautoreg/EM_CMIP_AUTOREG.DB
progress
/bin/rm -f trace.out
exit 0
