#!/bin/sh

# Copyright 2004 Sun Microsystems, Inc. All rights reserved.
# SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.


# Script to replace the tokens in com.sun.cmm.as.xml and register AS with JESMF
PATH=/usr/bin:/usr/sbin:/bin

# Function getMilliseconds: Prints the milliseconds for the input date
getMilliseconds()
{
MONTHS="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
MILIS_PER_DAY=` expr 24 \* 60 \* 60 \* 1000`
YEAR_MILIS=0
MONTH_MILIS=0
DAY_MILIS=0
HOUR_MILIS=0
MINS_MILIS=0

DATE=$1

MONTH=`echo $DATE | cut -d" " -f1`
DAY=`echo $DATE | cut -d" " -f2`
YEAR=`echo $DATE | cut -d" " -f3`
TIME=`echo $DATE | cut -d" " -f4`
HOUR=`echo $TIME | cut -d: -f1`
MINS=`echo $TIME | cut -d: -f2`

YDIFF=`expr $YEAR - 1970`
leapcount=0
nonleapcount=0
i=1970
while [ $i -lt $YEAR ]
do
	rem=`expr $i % 4`
	if [ $rem -eq 0 ]
	then
		#echo "$i:leap"
		leapcount=`expr $leapcount + 1`
	else
		#echo $i
		nonleapcount=`expr $nonleapcount + 1`
	fi
	i=`expr $i + 1`
done

MILIS_PER_NONLEAP_YEAR=`expr 365 \* $MILIS_PER_DAY`
MILIS_PER_LEAP_YEAR=`expr 366 \* $MILIS_PER_DAY`

YEAR_MILIS=`expr $leapcount \* $MILIS_PER_LEAP_YEAR + $nonleapcount \* $MILIS_PER_NONLEAP_YEAR`

sum=0
for i in $MONTHS
do

	if [ $i = $MONTH ]
	then
		break
	else
		if [ $i = "Sep" ] || [  $i = "Apr" ] || [ $i = "Jun" ] || [ $i = "Nov" ]
		then
			sum=`expr $sum + 30` 
		else
			if [ $i = "Feb" ]
			then
				if [ `expr $YEAR % 4` -eq 0 ]
				then
					sum=`expr $sum + 29`
				else
					sum=`expr $sum + 28`
				fi
			else
				sum=`expr $sum + 31`
			fi
		fi
	fi

done

MONTH_MILIS=`expr $sum \* $MILIS_PER_DAY`

DAY=`expr $DAY - 1`
DAY_MILIS=`expr $DAY  \* $MILIS_PER_DAY`
HOUR_MILIS=`expr $HOUR \* 60 \* 60 \* 1000`
MINS_MILIS=`expr $MINS \* 60 \* 1000` 
TOTAL_MILIS=`expr $YEAR_MILIS + $MONTH_MILIS + $DAY_MILIS + $HOUR_MILIS + $MINS_MILIS`

echo "$TOTAL_MILIS" 
}


# Set error codes to be used by caller. The codes starts @101 to make sure
# that the existing OS codes are not used.
setErrorCodes() {
INVALID_OS_ARCH=101
MFWK_HOME_NOT_FOUND=102
AS_INSTALL_DIR_NOT_FOUND=103
COM_SUN_CMM_AS_XML_NOT_FOUND=104
MFWK_REGISTRATION_SUCCESS=105
MFWK_REGISTRATION_FAILED=106
TOKEN_REPLACEMENT_FAILED=107
USAGE_ERROR=107
}

setSolarisDefaults(){
AS_INSTALL_DIR="/opt/SUNWappserver/appserver"
MFWK_HOME="/opt/SUNWmfwk"
INSTALLDATE=`pkgparam SUNWasu INSTDATE`
DATE=`getMilliseconds "${INSTALLDATE}"`
}

setLinuxDefaults(){
AS_INSTALL_DIR="/opt/sun/appserver"
MFWK_HOME="/opt/sun/mwfwk"
INSTALLDATE=`rpm -q --qf="%{INSTALLTIME}" sun-asu`
DATE=${INSTALLDATE}"000"
}


# This script can only be run on Solaris and Linux. Bails out if an attempt is
# made to run on any other OS.
checkOS() {
uName=`${UNAME}`
if [ "${uName}" = "SunOS" ]
then
        setSolarisDefaults
else if [ "${uName}" = "Linux" ]
then
        setLinuxDefaults
else
        exit ${INVALID_OS_ARCH}
fi
fi
}

parseArgs() {

if [ $# -gt 2 ]
then
	exit ${USAGE_ERROR}
fi
if [ $# -gt 1 ]
then
        MFWK_HOME=$2
fi
if [ $# -gt 0 ]
then
	AS_INSTALL_DIR=$1
fi

}
# initializing the variables used in the script
initVars() {
UNAME=uname
SOURCE_FILE="lib/install/templates/ee/com.sun.cmm.as.xml"
DEST_FILE="lib/com.sun.cmm.as.xml"
MFWKSETUP="bin/mfwksetup"
}

#validate the arguments
validateArgs(){
if [ ! -f "${MFWK_HOME}/${MFWKSETUP}" ]
then
	exit ${MFWK_HOME_NOT_FOUND}
fi

if [ ! -d "${AS_INSTALL_DIR}" ]
then
	exit ${AS_INSTALL_DIR_NOT_FOUND}
fi
}

doProcessing(){
SRC=${AS_INSTALL_DIR}/${SOURCE_FILE}
DEST=${AS_INSTALL_DIR}/${DEST_FILE}
if [ ! -f "$SRC" ]
then
	exit ${COM_SUN_CMM_AS_XML_NOT_FOUND}
fi

sed \
	-e "s,\${InstalledLocation},${AS_INSTALL_DIR}," \
	-e "s,\${InstalledDate},${DATE}," \
	< $SRC > $DEST	
RETVAL=$?
if [ ! ${RETVAL} = 0 ]
then
	exit ${TOKEN_REPLACEMENT_FAILED}
fi	
#Register with JESMF

${MFWK_HOME}/${MFWKSETUP} -r ${DEST}
RETVAL=$?
if [ ! ${RETVAL} = 0 ]
then
	exit ${MFWK_REGISTRATION_FAILED}
fi
exit ${MFWK_REGISTRATION_SUCCESS}
}

# The program starts here
initVars
setErrorCodes
checkOS
parseArgs $*
validateArgs
doProcessing

