#!/bin/ksh
#
#ident	"@(#)install.hcts	1.13	05/01/12 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#checking for the validity of hostname

version=`/usr/bin/uname -r|awk -F. '{print $2}'`

hostname=`hostname`

if [ -z "$hostname" ]
then
	echo "ERROR:"
	echo "The hostname shouldn't be NULL"
	exit 1
fi


hostlen=`echo $hostname|sed -n '/.*\{31\}/p'`

if [ -n "$hostlen" ]
then
	echo "ERROR:"
	echo "A host name should be a string of one to 30 characters"
	exit 1
fi

hostcontent=`echo $hostname|grep "^[a-zA-Z]"|grep "[a-zA-Z0-9]$"|grep -v "[^a-zA-Z0-9._-]"`

if [ -z "$hostcontent" ]
then
	echo "ERROR:"
	echo "\"$hostname\" is not a valid hostname"
	exit 1
fi

#check if hostname is accessible
err=`/usr/sbin/ping $hostname 2>&1`
if [ $? -ne 0 ]
then
	echo "ERROR:"
	echo "The current host:$hostname should be accessible"
	exit 1
fi


# Check for  SUNWhcts or SUNWstaf packages 
/usr/bin/pkginfo | egrep "SUNWhcts|SUNWstaf" > /dev/null
if [ $? -eq 0 ]
then            # packages are installed
	echo "-----------------------------------------------------------------"
	echo "ERROR:"
	echo "   The HCTS application is already installed."
	echo "   Use the uninstall.hcts command to remove the existing version."
	echo "-----------------------------------------------------------------"
	exit 3	
fi
if [ -z "`/usr/bin/pkginfo |egrep "SUNWapchr"`" -o -z "`/usr/bin/pkginfo|egrep "SUNWapchu"`" ]
then
	echo "-------------------------------------"
	echo "ERROR:"
	echo "	 The Apache Web Server is not found."
	echo "	 You must install the Apache Web Server"
	echo "-------------------------------------"
	exit 3
fi

/usr/bin/pkginfo | grep "SUNWxorg-demo" > /dev/null
if [ $? -ne 0 ]
then
	if [ $version -gt 9 ]
	then
		/usr/sbin/pkgadd -d . -G SUNWxorg-demo
	else
		/usr/sbin/pkgadd -d . SUNWxorg-demo
	fi
	
else
	 # packages are installed
	echo "------------------------------------------------------------------------"
	echo "ERROR:"
	echo "   The SUNWxorg-demo is already installed."
	echo "   Use the \"pkgrm SUNWxorg-demo\" command to remove the existing package."
	echo "------------------------------------------------------------------------"
	exit 3
	
fi

if [ $version -gt 9 ]
then
	/usr/sbin/pkgadd -d . -G SUNWhcts SUNWstaf
else
	/usr/sbin/pkgadd -d . SUNWhcts SUNWstaf
fi


