#! /bin/sh
#
# $Id: gst_localize.sh,v 1.9.10.1 2009/01/16 02:01:03 grands1 Exp $ Copyright (c) 2000-2007 EMC Corporation.
#

#
# Copyright (c) 2000-2007 EMC Corporation.
#
# All rights reserved.  This is an UNPUBLISHED work, and
# comprises proprietary and confidential information of EMC.
# Unauthorized use, disclosure, and distribution are strictly
# prohibited.  Use, duplication, or disclosure of the software
# and documentation by the U.S. Government is subject to
# restrictions set forth in a license agreement between the
# Government and EMC or other written agreement specifying
# the Government's rights to use the software and any applicable
# FAR provisions, such as FAR 52.227-19.
#

#
# Function:
#	runcmd
#
# Description:
#	Runs the command and arguments specified by the positional
#	parameters, and logs the output to $instlog. If the command
#	returns a non-zero exit status, this script exits with a
#	status of 1.
#
# Arguments:
#
#	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			Command to execute.
#	$2, $3, ...		Command arguments.
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#
runcmd()
{
    echo "** running: $*" >> "$instlog"
    $* >> "$instlog" 2>&1
    if [ $? -ne 0 ] ; then
	echo "ERROR: Command $* failed!"
	exit 1
    fi
}

#
# Function:
#	getpid	
#
# Description:
# 	gets the pid of the process specified in the argument list
#
# Arguments:
#
#	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			Process name.
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#
getpid()
{
    if [ -x /usr/bin/ps ] ; then
	/usr/bin/ps -e | awk "/$1\$/ {print \$1}"
    else
       	/bin/ps -e | awk "/$1\$/ {print \$1}"
    fi
}

#
# Function:
#	remove_langs
#
# Description:
# 	Remove  NMC language files.
# 	Should be called during uninstall of NMC only	
#
# Arguments:
#
#	Parameter		Purpose
#	----------------------	--------------------------------------------
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#
remove_langs()
{
    files="web/java/gwt_res_??.jar \
	  web/java/gconsole_res_??.jar \
	  web/java/nwadmin_res_??.jar \
	  web/java/nmc_help_??.jar \
	  web/gconsole_??.jnlp \
	  web/msgs_??.js"

    # No error checking as there is not much we can do
    # and we do not want uninstall to fail 
    for file in $files; do
	rm -f "$base"/$file > /dev/null 2>&1
    done
}

#
# Function:
#	install_lang
#
# Description:
# 	Install NMC language files and configure NMC
#
# Arguments:
#
#	Parameter		Purpose
#	----------------------	--------------------------------------------
#	$1			Locale code.
#
# Returns:
#
#	Return Variable		Purpose
#	----------------------	--------------------------------------------
#
install_lang()
{
    runcmd cp -f /opt/nsr/web/msgs_$1.js $gstweb
    runcmd cp -f /opt/nsr/web/gconsole_$1.jnlp $gstweb
    runcmd cp -f /opt/nsr/web/java/gwt_res_$1.jar $gstweb/java
    runcmd cp -f /opt/nsr/web/java/gconsole_res_$1.jar $gstweb/java
    runcmd cp -f /opt/nsr/web/java/nwadmin_res_$1.jar $gstweb/java
    runcmd cp -f /opt/nsr/web/java/nmc_help_$1.jar $gstweb/java

    # Configure the conf file
    runcmd "$gstparam" -f "$gstconf" -d -n pkg_lang_packs -t string \
			-v "$1"
    runcmd "$gstparam" -f "$gstconf" -a -n pkg_lang_packs -t string \
			-v "$1"
    runcmd "$gstparam" -f "$gstconf" -d -n pkg_langjar_files -t string \
			-v "/$PKG/java/gwt_res_$1.jar"
    runcmd "$gstparam" -f "$gstconf" -a -n pkg_langjar_files -t string \
			-v "/$PKG/java/gwt_res_$1.jar"
    runcmd "$gstparam" -f "$gstconf" -d -n pkg_langjar_files -t string \
			-v "/$PKG/java/gconsole_res_$1.jar"
    runcmd "$gstparam" -f "$gstconf" -a -n pkg_langjar_files -t string \
			-v "/$PKG/java/gconsole_res_$1.jar"
    runcmd "$gstparam" -f "$gstconf" -d -n pkg_langjar_files -t string \
			-v "/$PKG/java/nwadmin_res_$1.jar"
    runcmd "$gstparam" -f "$gstconf" -a -n pkg_langjar_files -t string \
			-v "/$PKG/java/nwadmin_res_$1.jar"
    runcmd "$gstparam" -f "$gstconf" -d -n pkg_langjar_files -t string \
			-v "/$PKG/java/nmc_help_$1.jar"
    runcmd "$gstparam" -f "$gstconf" -a -n pkg_langjar_files -t string \
			-v "/$PKG/java/nmc_help_$1.jar"

}

#
# main
#
PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
export PATH

#  Figure out platform specific components 
ARCH=`uname`
PKG="LGTOnmc"

# Check if  this is uninstall or if brand has been specified
if [ -n "$1" -a "$1" = "EBS" ]; then
    SOL_PKGNAME="SUNWebsg"
    CMD_LANG=$2
elif [ -n "$1" -a "$1" = "REMOVE" ]; then
    uninstall=true 
    if [ -n "$2" -a "$2" = "EBS" ]; then
	SOL_PKGNAME="SUNWebsg"
    else
        SOL_PKGNAME="LGTOnmc"
    fi
else
    CMD_LANG=$1
    SOL_PKGNAME="LGTOnmc"
fi

case "$ARCH" in
    Linux )
	    base=`rpm -q --qf "%{INSTALLPREFIX}\n" lgtonmc 2> /dev/null`
	    initfile=/etc/init.d/gst
	    syblib="${base}/sybasa/lib"
	    bindir="${base}/bin"
	    LD_LIBRARY_PATH="${bindir}:${syblib}:$LD_LIBRARY_PATH"
	    export LD_LIBRARY_PATH
	    # Just exit if NMC has not been configured
	    grep -q "current_db_dir" "$base"/etc/gstd.conf 2> /dev/null
	    if [ $? -ne 0 ]; then
		exit 0
	    fi	
	    ;;
    AIX )
	    base="/opt/lgtonmc"
	    initfile=/etc/rc.gst
	    syblib="${base}/sybasa/lib"
	    bindir="${base}/bin"
	    LIBPATH="${bindir}:${syblib}:$LIBPATH"
	    export LIBPATH
	    # Just exit if NMC has not been configured
	    grep -q "current_db_dir" "$base"/etc/gstd.conf
	    if [ $? -ne 0 ]; then
		exit 0
	    fi	
	    ;;
    HP-UX ) 
	    base="/opt/lgtonmc"
	    initfile=/sbin/init.d/gst
	    syblib="${base}/sybasa/lib"
	    bindir="${base}/bin"
	    SHLIB_PATH="${bindir}:${syblib}:$SHLIB_PATH"
	    export SHLIB_PATH
	    # Just exit if NMC has not been configured
	    grep -q "current_db_dir" "$base"/etc/gstd.conf
	    if [ $? -ne 0 ]; then
		exit 0
	    fi	
	    ;;
    SunOS )
	    base=`pkginfo -r $SOL_PKGNAME`
	    initfile=/etc/init.d/gst
	    syblib="${base}/sybasa/lib"
	    bindir="${base}/bin"
	    LD_LIBRARY_PATH="${bindir}:${syblib}:$LD_LIBRARY_PATH"
	    export LD_LIBRARY_PATH
esac

gstparam="${base}/bin/gstparam"
gstconf="${base}/etc/gstd.conf"
gstconfig="${base}/bin/gstconfig"
instlog="${base}/logs/install.log"
gstweb="${base}/web"

umask 0022

# If this is uninstall just call the function and exit
if [ -n "$uninstall" -a "$uninstall" = "true" ]; then
    remove_langs
    exit 0
fi

# Insert install date in logfile
echo "  " >> "$instlog"
echo "Install of language packs begins `date`" >> "$instlog"
echo "  " >> "$instlog"

# stop any existing GST server
stopped_nmc=false
gstd_pid=`getpid gstd`
if [ -n "$gstd_pid" ] ; then
   stopped_nmc=true
   runcmd $initfile stop
fi

# If lang has been specified use that.
if [ -n "$CMD_LANG" ]; then
    prod_langs="$CMD_LANG"
else
    prod_langs="zh ja fr de ko"
fi

installed_lang=false
for prod_lang in $prod_langs; do
    if [ -f /opt/nsr/web/java/gwt_res_${prod_lang}.jar ]; then
	install_lang $prod_lang
	installed_lang=true
    fi
done

# configure system
if [ $installed_lang = "true" ]; then
    runcmd "$gstconfig"
fi

# Start the server if required 
if [ $stopped_nmc = "true" ]; then
    echo "Starting gstd " >> "$instlog"
    "$initfile" start
fi

exit 0
