#!/bin/sh
#
# Galaxy xml creator
#
# This script creates xml for v8 instance to dock to v11 CommCell
# It must be put under v8 instance's GALDIR(e.g. /opt/simpana) before running
#

# Restart in ksh
GALDIR=`dirname $0`
[ "$GALDIR" = "." ] && GALDIR=`pwd`
[ "$_IN_KSH" != yes ] && export _IN_KSH=yes && exec $GALDIR/ksh "$GALDIR/cvpkgxml" "$@"

# Mac need bind at launch
DYLD_BIND_AT_LAUNCH=ON
export DYLD_BIND_AT_LAUNCH

# Set Galaxy instance
. $GALDIR/galaxy_vm

INSDIR=`dirname $GALDIR`
CV_UTILS_DIR="$GALDIR"
export GALDIR INSDIR CV_UTILS_DIR GALAXY_INST

unalias rm >/dev/null 2>&1
unalias cp >/dev/null 2>&1
unalias mv >/dev/null 2>&1

LANG_ORG="$LANG"; export LANG_ORG
LANG=C
export LANG

. $GALDIR/detect
. $GALDIR/utils
. $GALDIR/version
. $GALDIR/fw_utils

LOGDIR=`getRegValue \~EventManager dEVLOGDIR`
export CV_LOG_FILE=$LOGDIR/cvpkgxml.log.$$

#startLogging
CVDPORT=$(getRegValue "~Session" "nCVDPORT")
EMCPORT=$(getRegValue "~Session" "nEVMGRCPORT")
GALAXY_CNAME=$(getRegValue "~" "sPhysicalNodeName")
CLIENT_HOSTNAME=$(getPMHostName)
JOBDIR=$(getRegValue "~Machines/$GALAXY_CNAME" "dJOBRESULTSDIR")

#
# list matrix of supported platforms
#
getPFMatrix()
{
        cat <<EOD
linux-glibc2.2;linux-glibc2.2;Linux glibc-2.2;so;32;linux-glibc;X86;Linux;glibc2.2;17
linux-glibc2.3;linux-glibc2.3;Linux glibc-2.3;so;32;linux-glibc;X86;Linux;glibc2.3;17
linux-glibc2.4;linux-glibc2.3;Linux glibc-2.4;so;32;linux-glibc;X86;Linux;glibc2.4;17
linux-glibc2.5;linux-glibc2.3;Linux glibc-2.5;so;32;linux-glibc;X86;Linux;glibc2.5;17
linux-glibc2.6;linux-glibc2.3;Linux glibc-2.6;so;32;linux-glibc;X86;Linux;glibc2.6;17
linux-glibc2.3-x86_64;linux-glibc2.3-x86_64;Linux glibc-2.3 for x86_64;so;32;linux-glibc-x86_64;X64;Linux;glibc2.3;18
linux-glibc2.4-x86_64;linux-glibc2.3-x86_64;Linux glibc-2.4 for x86_64;so;32;linux-glibc-x86_64;X64;Linux;glibc2.4;18
linux-glibc2.5-x86_64;linux-glibc2.3-x86_64;Linux glibc-2.5 for x86_64;so;32;linux-glibc-x86_64;X64;Linux;glibc2.5;18
linux-glibc2.6-x86_64;linux-glibc2.3-x86_64;Linux glibc-2.6 for x86_64;so;32;linux-glibc-x86_64;X64;Linux;glibc2.6;18
linux-ia64-glibc2.2;linux-ia64-glibc2.2;Linux IA64 glibc-2.2;so;32;linux-ia64-glibc;IA64;Linux;glibc2.2;20
linux-ia64-glibc2.3;linux-ia64-glibc2.3;Linux IA64 glibc-2.3;so;32;linux-ia64-glibc;IA64;Linux;glibc2.3;20
linux-ia64-glibc2.4;linux-ia64-glibc2.3;Linux IA64 glibc-2.4;so;32;linux-ia64-glibc;IA64;Linux;glibc2.4;20
linux-ia64-glibc2.5;linux-ia64-glibc2.3;Linux IA64 glibc-2.5;so;32;linux-ia64-glibc;IA64;Linux;glibc2.5;20
linux-ia64-glibc2.6;linux-ia64-glibc2.3;Linux IA64 glibc-2.6;so;32;linux-ia64-glibc;IA64;Linux;glibc2.6;20
linux-glibc2.3-ppc64;linux-glibc2.3-ppc64;Linux glibc-2.3 for ppc64;so;32;linux-glibc-ppc64;PPC64;Linux;glibc2.3;26
linux-glibc2.4-ppc64;linux-glibc2.3-ppc64;Linux glibc-2.4 for ppc64;so;32;linux-glibc-ppc64;PPC64;Linux;glibc2.4;26
linux-glibc2.5-ppc64;linux-glibc2.3-ppc64;Linux glibc-2.5 for ppc64;so;32;linux-glibc-ppc64;PPC64;Linux;glibc2.5;26
linux-glibc2.6-ppc64;linux-glibc2.3-ppc64;Linux glibc-2.6 for ppc64;so;32;linux-glibc-ppc64;PPC64;Linux;glibc2.6;26
osf15.1;osf15.1;OSF1 V5.1;so;32;osf1;ALPHA;OSF1;5.1;15
solaris2.7;solaris2.7;Solaris 7;so;64;solaris;SPARC;Solaris;5.7;9
solaris2.8;solaris2.8;Solaris 8;so;64;solaris;SPARC;Solaris;5.8;9
solaris2.9;solaris2.8;Solaris 9;so;64;solaris;SPARC;Solaris;5.9;9
solaris2.10;solaris2.10;Solaris 10;so;64;solaris;SPARC;Solaris;5.10;10
solaris2.10-x86_64;solaris2.10-x86_64;Solaris 10 on x86_64;so;32;solaris-x86_64;X64;Solaris;5.10;11
aix5.1;aix5.1;AIX 5.1;so;64;aix;PPC;AIX;5.1;7
aix5.2;aix5.3;AIX 5.2;so;64;aix;PPC;AIX;5.2;7
aix5.3;aix5.3;AIX 5.3;so;64;aix;PPC;AIX;5.3;7
aix6.1;aix5.3;AIX 6.1;so;64;aix;PPC;AIX;6.1;7
hp11.11;hp11.11;HP-UX 11.11;sl;64;hp;PA-RISC;HP;B.11.11;13
hp11.23;hp11.11;HP-UX 11.23;sl;64;hp;PA-RISC;HP;B.11.23;13
hp11.31;hp11.11;HP-UX 11.31;sl;64;hp;PA-RISC;HP;B.11.31;13
hp11.23-ia64;hp11.23-ia64;HP-UX 11.23 IA64;so;32;hp-ia64;IA64;HP;B.11.23;14
hp11.31-ia64;hp11.23-ia64;HP-UX 11.31 IA64;so;32;hp-ia64;IA64;HP;B.11.31;14
darwin;darwin;MAC OS X Universal Binary;dylib;32;darwinUB;X86;Darwin;6;21
darwin6;darwin;MAC OS X 10.2 on PowerPC;dylib;32;darwin;PPC;Darwin;6;21
darwin7;darwin;MAC OS X 10.3 on PowerPC;dylib;32;darwin;PPC;Darwin;6;21
darwin8;darwin;MAC OS X 10.4 on PowerPC;dylib;32;darwin;PPC;Darwin;6;21
darwin8-i386;darwin;MAC OS X 10.4 on Intel;dylib;32;darwin-i386;X86;Darwin;6;21
darwin9;darwin;MAC OS X 10.5 on PowerPC;dylib;32;darwin;PPC;Darwin;6;21
darwin9-i386;darwin;MAC OS X 10.5 on Intel;dylib;32;darwin-i386;X86;Darwin;6;21
irix6.5;irix6.5;SGI Irix 6.5;so;32;irix;MIPS;IRIX64;6.5;23
freebsd4;freebsd4;FreeBSD 4;so;32;freebsd;X86;FreeBSD;4.7;27
freebsd5;freebsd5;FreeBSD 5;so;32;freebsd;X86;FreeBSD;5.0;25
freebsd6;freebsd6;FreeBSD 6;so;32;freebsd;X86;FreeBSD;6.0;25
freebsd7;freebsd7;FreeBSD 7;so;32;freebsd;X86;FreeBSD;7.1;28
EOD
}

#
# list matrix of supported subsystems
#
getSSMatrix()
{
        cat <<EOD
CVGxBase0;Base0 Client;Base0;n;-;n;Base0;n;1003
CVGxBase;Base Client;Base;n;-;n;Base;n;1002
CVGxIDA;UNIX File System iDataAgent;FileSystemAgent;y;-;y;iDataAgent;y;1101
CVGxMA;MediaAgent;MediaAgent;y;-;n;MediaAgent;n;1301
CVGxWA;SCSI Driver;WA;y;dr;n;WA;n;1105
CVGxProxyIDA;ProxyHost iDataAgent;ProxyHost;y;-;-;ProxyIDA;n;1102
CVGxDctmIDA;Documentum iDataAgent;DocumentumAgent;y;-;-;DocumentumDataAgent;n;1126
CVGxOrIDA;Oracle iDataAgent;OracleAgent;y;-;-;OracleDataAgent;n;1204
CVGxOrSAP;SAP for Oracle;OracleSapAgent;y;db;-;OracleSapAgent;n;1205
CVGxSAPMAXDB;SAP for MaxDB;SapMaxDbAgent;y;db;-;SapMaxDbAgent;n;1206
CVGxIfIDA;Informix iDataAgent;InformixAgent;y;db;-;InformixDataAgent;n;1201
CVGxSybIDA;Sybase iDataAgent;SybaseAgent;y;db;-;SybaseDataAgent;n;1202
CVGxDB2;DB2 iDataAgent;Db2Agent;y;db;-;Db2DataAgent;n;1207
CVGxMySQL;MySQL iDataAgent;MySQL;y;db;-;MySQL;y;1208
CVGxPostGres;PostGres iDataAgent;PostGres;y;db;-;PostGres;y;1209
CVGxLndbIDA;Lotus Notes Database iDataAgent;LotusNotesDBAgent;y;db;y;lndbDataAgent;y;1051
CVGxLndocIDA;Lotus Notes Document iDataAgent;LotusNotesDOCAgent;y;db;y;lndocDataAgent;y;1052
CVGxLndmIDA;Domino Mailbox Archiver Agent;LotusNotesDMAgent;y;db;y;lndmDataAgent;y;1053
CVGxSDM;Serverless Data Manager;SANVolBackup;y;-;-;SDM;n;1103
CVGxFSDM;File Archiver for UNIX Agent;UnixFsDataMigrator;y;-;n;FSDM;n;1109
CVGxFSF;File System Filter Driver;FSF;n;dr;n;FSF;n;1113
CVGxCDR;Continuous Data Replicator;CDR;y;dr;n;CDR;n;1114
CVGxCXBF;QSnap;HotspotSnapshotAgent;y;dr;n;CXBF;n;1401
CVGxImgIDA;Image Level iDataAgent;UnixImageIDA;y;-;n;ImIDA;n;1104
CVGxNRS;NDMP Remote Server;NDMP_API;y;-;n;NRS;n;1302
CVGxQRA;OSSV Plug-in;ReplicationIDAAgent;y;-;n;QR;n;1351
CVGxOSSV;SnapVault Open Systems Enabler;SnapVault;n;-;n;SnapVaultAgent;n;1115
CVGxNRE;File System NDMP Restore Enabler;NDMPRestoreEnabler;y;-;n;NRE;n;1303
CVGxGUI;CommCell Console;JavaGUI;y;-;n;GUI;n;1118
CVGx1Touch;1-Touch Server;1Touch;y;-;n;SR;n;1119
CVGxGW;Novell OES Groupwise DataBase iDataAgent;GW;y;db;y;GW;y;1122
CVGxOES;Novell OES File System iDataAgent;OESDataAgent;y;-;y;OESDataAgent;y;1121
CVGxSRM;SRM UNIX File System Agent;SRM;y;-;n;SRM;n;1123
CVGxNDS;Novell OES Directory Services;NDS;y;-;y;NDS;y;1124
CVGxImgProxyIDA;Image Level ProxyHost iDataAgent;ImageLevelProxyHostUnix;y;-;n;ImgProxyIDA;n;1125
CVGxDC;Data Classification Enabler;DataClassification;y;-;n;DC;n;1112
CVGxPrePost;Pre-PostInstall;PrePostOperation;n;-;n;PrePost;n;1127
CVGxOBJSRV;Content Store;OBJSRV;y;-;y;OBJSRV;y;1129
CVGxEDC;External Data Connector;ExternalDataConnector;y;-;y;EDC;y;1128
CVGxRP;Resource Pack;RP;y;-;n;RP;n;1130
CVGxRPL;Resource Pack Limited;RPL;y;-;n;RPL;n;1131
CVGxRPI;Resource Pack Internal;RPI;y;-;n;RPI;n;1132
CVGxMediaExplorer;Media Explorer;MediaExplorer;y;-;n;ME;n;1110
CVGxCDRP;Predictor for Continuous Data Replicator;CDRP;y;-;n;CDRP;n;1133
CVGxQGUI;CommNet Browser;QGUI;y;-;n;QGUI;n;1134
EOD
}

# OS information
PLATFORM_ID=`getSystemID`
getOsInfoN()       { getPFMatrix | awk -F\; '$1 == "'"$PLATFORM_ID"'" { print $'"$1"' }'; }
getPlatformType()  { tmp=`getOsInfoN 7`; [ x"$tmp" = x ] && echo "Any" || echo "$tmp"; }
getSubType()       { tmp=`getOsInfoN 8`; [ x"$tmp" = x ] && echo "Any" || echo "$tmp"; }
getVersion()       { tmp=`getOsInfoN 9`; [ x"$tmp" = x ] && echo "Any" || echo "$tmp"; }
getOSBuild() { [ x"`uname -s`" = xAIX ] && echo "`uname -v`.`uname -r`" || uname -r; }
getProcessorType() { tmp=`uname -p 2>/dev/null`; [ x"$tmp" = x -o x"$tmp" = xunknown ] && uname -m 2>/dev/null || echo "$tmp"; }

getSubSysPkgID() { [[ "$1" != CVGx* ]] && echo "$1" && return 0; getSSMatrix | awk -F';' '$1 == "'"$1"'" {print $9}'; }
getBinarySetID() { getPFMatrix | awk -F\; '$1 == "'"${1:-${PACKPLAT_ID:-$PLATFORM_ID}}"'" { print $10 }'; }

getEndian() { [[ "`printf '\01' | od -dAn`" = *256* ]] && echo "Big-Endian" || echo "Little-Endian"; }

# Installed subsystems
INSTALLED_SUBSYSTEMS="$(getInstalledSubsystems)"

# Create xml
createXml()
{
	cat <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<CVInstallManager_ClientSetup Focus="$GALAXY_INST" LocaleID="0" Operationtype="0" RemoteClient="1" requestFlags="0" requestType="1">
EOF

	[[ -n "$COMMCELL_USER" ]] && cat <<EOF
<ClientAuthentication>
<userAccountToLogin password="$COMMCELL_PASS" userName="$COMMCELL_USER"/>
</ClientAuthentication>
EOF

	[[ -n "$SERVER_HOSTNAME" ]] && cat <<EOF
<CommServeInfo>
<CommserveHostInfo _type_="3" clientName="$SERVER_DISPNAME" hostName="$SERVER_HOSTNAME"/>
</CommServeInfo>
EOF

	cat <<EOF
<clientComposition>
<clientInfo>
<client clientPassword="$PNETPWD" cvdPort="$CVDPORT" evmgrcPort="$EMCPORT" installDirectory="$INSDIR">
<clientEntity clientName="$GALAXY_CNAME" hostName="$CLIENT_HOSTNAME">
</clientEntity>
<jobResulsDir path="$JOBDIR">
</jobResulsDir>
<osInfo PlatformType="$(getPlatformType)" SubType="$(getSubType)" Type="Unix" Version="$(getVersion)">
<OsDisplayInfo OSBuild="$(getOSBuild)" OSName="$(uname)" ProcessorType="$(getProcessorType)">
</OsDisplayInfo>
</osInfo>
<versionInfo GalaxyBuildNumber="BUILD84">
<GalaxyRelease ReleaseString="9.0">
</GalaxyRelease>
</versionInfo>
</client>
<clientProps BinarySetID="$(getBinarySetID)" IsVirtualClient="0" logFilesLocation="$LOGDIR" byteOrder="$(getEndian)">
</clientProps>
</clientInfo>
<components>
EOF

	for ss in $INSTALLED_SUBSYSTEMS
	do
		cat << EOF
<componentInfo ComponentId="$(getSubSysPkgID $ss)" clientSidePackage="1" osType="Unix" consumeLicense="1">
</componentInfo>
EOF
	done

	DC_CACHE_DIR="$(getRegValue \~DataClassification "dCacheRootDir")"
	[[ -n $DC_CACHE_DIR ]] && cat << EOF
<dataClassification dcCacheLocation="$DC_CACHE_DIR">
</dataClassification>
EOF

	IC_DIR="$(getRegValue "~Machines/$GALAXY_CNAME" dFSINDEXCACHE)"
	[[ -n $IC_DIR ]] && cat << EOF
<mediaAgent>
<indexCacheDirectory path="$IC_DIR">
</indexCacheDirectory>
</mediaAgent>
EOF

cat <<EOF
</components>
</clientComposition>
</CVInstallManager_ClientSetup>
EOF

}

# parse CLI
SERVER_HOSTNAME=""
SERVER_DISPNAME=""
COMMCELL_USER=""
COMMCELL_PASS=""
PNETPWD=""
while [[ -n "$1" ]]
do
    case $1 in
        -cs-short)
                SERVER_DISPNAME="$2"
                [ x"$SERVER_DISPNAME" = x ] && err "Option \"-cs-short\" must be followed by a commserve short name."
                shift 2
                ;;
        -cs)
                SERVER_HOSTNAME="$2"
                [ x"$SERVER_HOSTNAME" = x ] && err "Option \"-cs\" must be followed by a commserve hostname."
                shift 2
                ;;
        -user)
                COMMCELL_USER="$2"
                [ x"$COMMCELL_USER" = x ] && err "Option \"-user\" must be followed by a commcell username."
                shift 2
                ;;
        -password)
                COMMCELL_PASS="$2"
                [ x"$COMMCELL_PASS" = x ] && err "Option \"-password\" must be followed by a commcell user's password."
                shift 2
                ;;
        -h*|-?)
                clear
                cat <<EOF
cvpkgxml - create xml for Galaxy V8 decoupled instance for regsitering to V11 CS
--------------------------------------------------------------------------------
USAGE:
cvpkgxml [-cs-short <commserver_short_name>] [-cs <commserver_hostname>] [-user <user> -password <password>]

   -cs-short <commserver_short_name>
     Specifies the commserver client name you want to dock the client to.

   -cs <commserver_hostname>
     Specifies the commserver hostname you want to dock the client to.

   -user <user> -password <password>
     Specifies the commcell user and password to log on commserver when authentication is required.

EOF
                exit 1
                ;;
        *)
                err "Wrong parameter $1 specified"
                ;;
    esac
done

# Set client password
PNETPWD="$(getRegValue \~Session "pNETPWD")"

# Create CS Registry
[[ -z "$SERVER_HOSTNAME" ]] || setRegValue \~CommServe "sCSHOSTNAME" "$SERVER_HOSTNAME" || msg "Failed setting registry for CS host name"
[[ -z "$SERVER_DISPNAME" ]] || setRegValue \~CommServe "sCSCLIENTNAME" "$SERVER_DISPNAME" || msg "Failed setting registry for CS client name"
setRegValue \~CommServe "bCSConnectivityAvailable" "1" || msg "Failed setting registry for bCSConnectivityAvailable"

# Remove chatter flag
delRegValue \~Session "nChatterFlag"

createXml > $GALDIR/ic.xml
exit 0
