#! /bin/bash
#
# ssmuse

# GPL--start
# This file is part of ssm (Simple Software Manager)
# Copyright (C) 2005-2010 Environment/Environnement Canada
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# GPL--end

printUsage() {
	echo "\
usage: ssmuse <shellType> [params] [options]
              -h|--help

Generate commands for configuring the current shell
environment to use the package installed at <packageHome>.
Note: the output should be eval'ed in order for the
configuration to take effect. For example:
    \$ eval \"\`ssmuse sh -d /opt/ssm\`\"   # for sh
or
    % eval \"\`ssmuse csh -d /opt/ssm\`\"  # for csh

Params:
    <shellType>
        'sh' for sh-type (sh/bash/ksh) commands.
        'csh' for csh-type (csh/tcsh) commands.

    -d|--domainHome <domainHome>
        The domain home (base path) to search for packages.

Options:
    -p|--packageName <packageName>[,...]
        Name of the package(s) to access. If no packageName
        is specified, then all published packages of the domain
        and subdomains are assumed. Otherwise, the packageName
        may take the forms:
            name_version_platform - fully specified
            name_version - best platform match
            name - highest version, best platform match
    --order prepend|append
        Ordering used for updating the PATH settings. \"prepend\"
        is used by default.
"
}

domainHome=""
packageName=""
order="prepend"
shellType=""

if [ $# -lt 1 ]; then
	printUsage
	exit 1
elif [ $1 = "-h" -o $1 = "--help" ]; then
	printUsage
	exit 1
else
	shellType=$1; shift 1
fi

if [ "${shellType}" = "" ]; then
	echo "echo \"Error: Missing shellType.\""
	exit 1
fi

while [ $# -gt 0 ]; do
	case $1 in
	"-d"|"--domainHome")
		shift 1
		domainHome=$1; shift 1
		;;
	"-p"|"--packageName")
		shift 1
		packageName=$1; shift 1
		;;
	"--order")
		shift 1
		order=$1; shift 1
		;;
	*)
		echo "echo \"Error: Unexpected parameter ($1).\""
		exit 1
		;;
	esac
done

if [ "${domainHome}" = "" ]; then
    echo "echo \"Error: Domain home not specified.\""
    exit 1
fi

# set up
ssm_lib="${domainHome}/lib/ssm.d/ssm_lib.sh"
if [ -r "${ssm_lib}" ]; then
	. "${ssm_lib}"
	platforms=`getSystemPlatforms "${domainHome}"`
else
	echo "echo \"Error: Cannot find required file (${ssm_lib}).\""
	exit 1
fi

# get domain version (useful for compatibility testing)
domainVersion=`getDomainVersion "${domainHome}" 2>&1`
if [ $? -ne 0 ]; then
	domainVersion="0"
fi

# special case for <8.0 (will be removed in future)
case ${domainVersion} in
0)
	installedDir=`\ls "${domainHome}/etc/ssm.d/installed"`
	;;
*)
	installedDir=`getInstalledDir "${domainHome}"`
esac

ssmGenerateEnvConfigExec="${domainHome}/lib/ssm.d/ssm_generateenvconfig.sh"

if [ -x ${ssmGenerateEnvConfigExec} ]; then
	if [ "${packageName}" = "" ]; then
		cmds=`${ssmGenerateEnvConfigExec} ${order} "${shellType}" "${domainHome}"`
	else
		cmds=""

		for packageName in `IFS=","; echo ${packageName}`; do
			newPackageName=""

			case "${packageName}" in
			*_*_*)
				newPackageName="${packageName}"
				;;
			*)
				if [ "${domainVersion}" = "0" ]; then
					cmds="${cmds}
echo \"Error: Feature not supported by domain (${domainHome}).\""
					continue
				fi

				case "${packageName}" in
				*_*)

					# find suitable package among platforms
					newPackageName=`matchPackagePlatform "${installedDir}" "${packageName}" "${platforms}"`
					if [ "${newPackageName}" = "" ]; then
						# could not find match
						continue
					fi
					;;
				*)
					# find newest suitable package among versions and platforms
					versions=`getPackageVersions "${installedDir}" "${packageName}"`
					versions=`echo ${versions} | \sed 's/ /\n/g' | \sort -n -r`
					for version in ${versions}; do
						newPackageName="${packageName}_${version}"
						newPackageName=`matchPackagePlatform "${domainHome}" "${newPackageName}" "${platforms}"`
						if [ "${newPackageName}" = "" ]; then
							# could not find match
							continue
						else
							break
						fi
					done
					;;
				esac
			esac

			# add if full package name found
			if [ "${newPackageName}" != "" ]; then
				cmds="${cmds}
`${ssmGenerateEnvConfigExec} ${order} ${shellType} ${domainHome} ${newPackageName}`"
			fi
		done
	fi
	echo "${cmds}"
else
	echo "echo \"Error: Cannot find required file (${ssmGenerateEnvConfigExec}).\""
	exit 1
fi
