#!/bin/sh 
clear
echo "Checking for any Old Version"
searchcont=1
if [ $searchcont = 1 ]; then
 linecount=`rpm -qa | grep LSIStorageAuthority | wc -l`
 if [ $linecount = 0 ]; then
  status=1
 else
  if [ $linecount = 1 ]; then
     status=0
  else
    echo "More than one copy of LSIStorageAuthority has been installed. Exiting installation."
    exit 1
  fi
 fi
                                                                                
#echo "setuptype is $setuptype";
#echo "upgradesetuptype is $upgradesetuptype";
																
 if [ $status = 0 ]; then
  newdate=1507864390
  olddate=`rpm -q --queryformat "%{BUILDTIME}" LSIStorageAuthority`
  if [ $newdate -gt $olddate ]; then


      if [ "$setupmode" = "SILENT" ]; then
			ELU="y"
        	else
        		echo " LSIStorageAuthority application is already installed"
        		echo " Would you like to continue with the upgrade [y/n]:"
        		echo " y, uninstall previous version and install latest version"
        		echo " n, No changes made to the system, exit the installation"
			read ELU
      fi
#	echo "$ELU"
	if [ "$ELU" != "y" -a "$ELU" != "Y" ]; then
	echo "Exiting Installation " 
	exit 1
	fi
	echo "Uninstalling Old version"
  	#. /etc/init.d/slim_profile
	LSA_HOME=/opt/lsi/LSIStorageAuthority
  	"$LSA_HOME/.__uninst.sh"
	status=$?
	if [ $status = 0 ]; then
		if [ -f "$LSA_HOME/.__uninst.sh" ]; then	
			echo "Uninstall Failed. Exiting installation."
			exit 1
		fi
		echo "Uninstall Completed Successfully"
	else
		echo "Uninstall Failed. Exiting installation."
		exit 1
	fi
  else
        if [ $newdate = $olddate ]; then
         echo "The version is already installed."
        else
	 echo "The installed version is newer."
        fi
        echo "Exiting installation."
        exit 1
  fi
 else
  echo "No Old Version Found"
 fi
fi
echo "Continuing with installation"
exit 0
