#!/bin/bash
if [ -f /usr/sbin/apache2 ]
then
   httpdver=`/usr/sbin/apache2 -v | head -1`
elif [ -f /usr/sbin/apache ]
then
   httpdver=`/usr/sbin/apache2 -v | head -1`
elif [ -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 apache 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

#Check if RHEL/SLES or UBUNTU platform

if [ "x$LINUX_VENDOR" = "x" ]
then
  RHEL=`grep -i rhel /etc/os-release 2>/dev/null | wc -l`
  if [ $RHEL != 0 ]
  then
    LINUX_VENDOR=RHEL
  fi
fi
if [ "x$LINUX_VENDOR" = "x" ]
then
  SLES=`grep -i sles  /etc/os-release 2>/dev/null | wc -l`
  if [ $SLES != 0 ]
  then
    LINUX_VENDOR=SLES
  fi
fi
# Ubuntu check
if [ "x$LINUX_VENDOR" = "x" ]
then
  UBUNTU=`grep -i ubuntu /etc/os-release 2>/dev/null | wc -l`
  if [ $UBUNTU != 0 ]
  then
    LINUX_VENDOR=UBUNTU
  fi
fi

if [ "$LINUX_VENDOR" != "UBUNTU" ]
then
#########################################################
#  Do dependency check for perl-CGI installed
#########################################################
rpm -qa 2>/dev/null | grep -i perl | grep -i cgi >/dev/null 2>&1
if [ $? = 1 ]
then
    echo "Package perl-CGI required for WebAdmin install."
    echo "Install perl-CGI before continuing to install."
    exit 2
fi

#########################################################
# Install rpm package, nodeps required due to false
#  requirement check from rpmbuild on webadmin_bld_token.pl 
#########################################################
rpm -qa|grep ibm-commserver-webadmin-7.1.1.0-1 >& /dev/null
if [ $? -ne 0 ]
then
   rpm --nodeps -Uhv ibm-commserver-webadmin-7.1.1.0-1.noarch.rpm
   if [ $? != 0 ]
   then
      echo "Possible error during install of ibm-commserver-webadmin package, will exit now."
      exit 1
   fi
fi

if [ "$LINUX_VENDOR" != "SLES" ]
then
   webdir=/etc/httpd/conf.d
else 
   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_rpm.conf.apache22 $webdir/sna.conf
        fi
        if [[ ("$compId" = 2) || ("$compId" = 3) ]]
        then
                cp /opt/ibm/sna/query/snaquery_rpm.conf.apache22 $webdir/snaquery.conf
        fi
        echo "apache 2.2"
fi

if [ ${httpdver:23:3} = 2.4 ]
then
        if [[ ("$compId" = 1) || ("$compId" = 3) ]]
        then
                cp /opt/ibm/sna/web/sna_rpm.conf $webdir/sna.conf
        fi
        if [[ ("$compId" = 2) || ("$compId" = 3) ]]
        then
                cp /opt/ibm/sna/query/snaquery_rpm.conf $webdir/snaquery.conf
        fi
        echo "apache 2.4"
fi

else
#########################################################
# Install debian package
#########################################################
dpkg -l|grep ibm-commserver-webadmin-7.1.1.0-1 >& /dev/null
if [ $? -ne 0 ]
then
   dpkg -i ibm-commserver-webadmin_7.1.1.0-1_all.deb
   if [ $? != 0 ]
   then
      echo "Possible error during install of ibm-commserver-webadmin package, will exit now."
      exit 1
   fi
fi


if [ -f /etc/lsb-release ]
then
   webdir=/etc/apache2/sites-available/
fi

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

ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/cgi.load

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_deb.conf.apache22 $webdir/sna.conf
      ln -s $webdir/sna.conf /etc/apache2/sites-enabled/sna.conf
	fi
	if [[ ("$compId" = 2) || ("$compId" = 3) ]]
	then
		cp /opt/ibm/sna/query/snaquery_deb.conf.apache22 $webdir/snaquery.conf
      ln -s $webdir/snaquery.conf /etc/apache2/sites-enabled/snaquery.conf
	fi
	echo "Ubuntu apache 2.2"
fi
  
if [ ${httpdver:23:3} = 2.4 ]
then
	if [[ ("$compId" = 1) || ("$compId" = 3) ]]
	then
		cp /opt/ibm/sna/web/sna_deb.conf $webdir/sna.conf
      ln -s $webdir/sna.conf /etc/apache2/sites-enabled/sna.conf
	fi
	if [[ ("$compId" = 2) || ("$compId" = 3) ]]
	then
		cp /opt/ibm/sna/query/snaquery_deb.conf $webdir/snaquery.conf
      ln -s $webdir/snaquery.conf /etc/apache2/sites-enabled/snaquery.conf
	fi
	echo "Ubuntu apache 2.4"
fi
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

