#!/bin/bash
if [ -f /usr/sbin/httpd2 ]
then
   httpdver=`/usr/sbin/httpd2 -v | head -1`
elif [ -f /usr/sbin/httpd ]
then
   httpdver=`/usr/sbin/httpd -v | head -1`
else
   echo "There is no httpd service found"
   exit 1
fi

echo "1. Web Admin only"
echo "2. Query Admin only"
echo "3. Both Web Admin and Query Admin"

read -p "Please select which component to install:" compId

if [[("$compId" != 1) && ("$compId" != 2) && ("$compId" != 3)]]
then
   echo "Invalid input"
   exit 1
fi


#########################################################
# Install rpm package
#########################################################
rpm -qa|grep ibm-commserver-webadmin-7.0.0.2-1 >& /dev/null
if [ $? -ne 0 ]
then
   rpm -Uhv ibm-commserver-webadmin-7.0.0.2-1.noarch.rpm
   if [ $? != 0 ]
   then
      echo "Possible error during install of ibm-commserver-webadmin package, will exit now."
      exit 1
   fi
fi


if [ -f /etc/redhat-release ]
then
   webdir=/etc/httpd/conf.d
fi

if [ -f /etc/SuSE-release ]
then
   webdir=/etc/apache2/conf.d
fi

# copy configuration file to web server dir based on the version of web server and  user choise

if [ ${httpdver:23:3} = 2.2 ]
then
      # install -p -m 755 snaquery.conf.apache22  /etc/apache2/conf.d/snaquery.conf
	if [[("$compId" = 1) || ("$compId" = 3)]]
	then
		cp /opt/ibm/sna/web/sna.conf.apache22 $webdir/sna.conf
	fi
	if [[("$compId" = 2) || ("$compId" = 3)]]
	then
		cp /opt/ibm/sna/query/snaquery.conf.apache22 $webdir/snaquery.conf
	fi
	echo "SuSE apache 2.2"
fi
  
if [ ${httpdver:23:3} = 2.4 ]
then
	if [[("$compId" = 1) || ("$compId" = 3)]]
	then
		cp /opt/ibm/sna/web/sna.conf $webdir/sna.conf
	fi
	if [[("$compId" = 2) || ("$compId" = 3)]]
	then
		cp /opt/ibm/sna/query/snaquery.conf $webdir/snaquery.conf
	fi
	echo "SuSE apache 2.4"
fi


echo "Install complete, configure user account now"
which htpasswd >& /dev/null
if [ $? -ne 0 ]
then
   cmdname=htpasswd2
else
   cmdname=htpasswd
fi

if [[("$compId" = 1) || ("$compId" = 3)]]
then
   read -p "Please input the User Account for Web Admin:" webusrname
   $cmdname -c /opt/ibm/sna/.webpasswd $webusrname
fi
if [[("$compId" = 2) || ("$compId" = 3)]]
then
   read -p "Please input the User Account for Query Admin:" queryusrname
   $cmdname -c /opt/ibm/sna/.querypasswd $queryusrname
fi

# add snauser to sna group

echo "add snauser to sna group"
user=snauser
group=sna

#create group if not exists
egrep "^$group" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
   groupadd $group
fi

#create user if not exists
egrep "^$user" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
   useradd -g $group $user
fi
