#!/bin/bash
szOSType="unknown"
SERVICEINITPATH=/etc/init.d
SERVICESCAN=revescanD
SERVICEDEVICE=revedeviceD
SERVICESCHSCAN=revescheduleD
SERVICEPUPDATE=revepupdateD
SERVICEREBOUND=revexscanD

OS1=`lsb_release -ds | cut -c1-9`
OS2=`lsb_release -ds | cut -c1-12`

        
if [ -d /etc/rc.d/init.d ]
then
	SERVICEINITPATH=/etc/rc.d/init.d
fi
startDaemons()
{
#Start the REVE Scan Engine Daemon.
if [ $SERVICEINITPATH/$SERVICESCAN ]
then
	chmod 777 $SERVICEINITPATH/$SERVICESCAN
	service revescanD start
fi

#Start the REVE Scan Engine Device Daemon.
if [ $SERVICEINITPATH/$SERVICEDEVICE ]
then
	chmod 777 $SERVICEINITPATH/$SERVICEDEVICE
	service revedeviceD start
fi

#Start the REVE Schedule Scan Daemon.
if [ $SERVICEINITPATH/$SERVICESCHSCAN ]
then
        chmod 777 $SERVICEINITPATH/$SERVICESCHSCAN
        service revescheduleD start
fi

#Start the REVE Product Update Daemon.
if [ $SERVICEINITPATH/$SERVICEPUPDATE ]
then
        chmod 777 $SERVICEINITPATH/$SERVICEPUPDATE
        service revepupdateD start
fi

#Start the REVE Scan Rebound Daemon.
if [ $SERVICEINITPATH/$SERVICEREBOUND ]
then
        chmod 777 $SERVICEINITPATH/$SERVICEREBOUND
        service revexscanD start
fi


}

# Adding service script in init.d
AddService()
{
                if [ "$szOSType" = "debian" ];then

			OSverC=`lsb_release -ds | cut -c8-12`
			if [ $OSverC == "17.10" ];
			then
				cp /lib/reveAM/Ubuntu_UninstallerShortcut18 /usr/share/applications/REVEUninstall.desktop
                		chmod 777 /usr/share/applications/REVEUninstall.desktop
			else
				if [ $OSverC == "18.04" ];
				then
					cp /lib/reveAM/Ubuntu_UninstallerShortcut18 /usr/share/applications/REVEUninstall.desktop
                        		chmod 777 /usr/share/applications/REVEUninstall.desktop
				else
					cp /lib/reveAM/Ubuntu_UninstallerShortcut /usr/share/applications/REVEUninstall.desktop
                                	chmod 777 /usr/share/applications/REVEUninstall.desktop
				fi
			fi

			cp /lib/reveAM/Ubuntu_Shortcut /usr/share/applications/ReveAntivirus.desktop
			chmod 777 /usr/share/applications/ReveAntivirus.desktop
				
			chmod 777 /lib/reveAM/Ubuntu_Scan
			cp -f /lib/reveAM/Ubuntu_Scan $SERVICEINITPATH/revescanD

			chmod 777 /lib/reveAM/Ubuntu_Device
			cp -f /lib/reveAM/Ubuntu_Device $SERVICEINITPATH/revedeviceD

			chmod 777 /lib/reveAM/Ubuntu_ScheduleScan
			cp -f /lib/reveAM/Ubuntu_ScheduleScan $SERVICEINITPATH/revescheduleD

			chmod 777 /lib/reveAM/Ubuntu_ProductUpdate
                        cp -f /lib/reveAM/Ubuntu_ProductUpdate $SERVICEINITPATH/revepupdateD

			chmod 777 /lib/reveAM/Ubuntu_Rebound
                        cp -f /lib/reveAM/Ubuntu_Rebound $SERVICEINITPATH/revexscanD

                        update-rc.d $SERVICESCAN defaults > /dev/null 
			update-rc.d $SERVICEDEVICE defaults > /dev/null
			update-rc.d $SERVICESCHSCAN defaults > /dev/null
			update-rc.d $SERVICEPUPDATE defaults > /dev/null
			update-rc.d $SERVICEREBOUND defaults > /dev/null
			
                else
                        chkconfig --add $SERVICESCAN 2>>/dev/null
			chkconfig --add $SERVICEDEVICE 2>>/dev/null
			chkconfig --add $SERVICESCHSCAN 2>>/dev/null
			chkconfig --add $SERVICEPUPDATE 2>>/dev/null
			chkconfig --add $SERVICEREBOUND 2>>/dev/null
                fi

	if [ $? -ne 0 ]
	then
		return 1
	fi

	return 0
}
# Get OS 
getOs()
{
	
	
	RELEASEFILE=""
	if [[ "$szOSType" = "unknown" ]]; then
		#if [ -x "/sbin/SuSEconfig" ]; then
			if [ -x "`which yast 2>/dev/null`" -o -x "`which yast2 2>/dev/null`" ]; then
				if [ -e "/sbin/init.d" ]; then
					#/bin/echo "OS: SuSE-like (less 7.1) system"
					szOSType="SuSE" #"SuSE70"
				else
				 if [ -e "/etc/init.d" ]; then
					#/bin/echo "OS: SuSE-like (7.1 or upper) system"
					szOSType="SuSE" #"SuSE71"
				 else
					#/bin/echo "OS: SuSE-like system"
					szOSType="SuSE"
				 fi
				fi
				#Check for Distro.
				RELEASEFILE=$(ls /etc/*release* | grep SuSE) 2>/dev/null
                                if [ ! -z "$RELEASEFILE" ]; then
                                	DistroType="SuSE"
                                fi

			fi
		#else
		if [[ "$szOSType" = "unknown" ]]; then
			if [ -x "`which chkconfig 2>/dev/null`" ]; then
				if [ -e "/etc/rc.d/init.d" ]; then
					#/bin/echo "OS: Redhat-like system"
					szOSType="redhat"

					### Select the Distro type.
					RELEASEFILE=$(ls /etc/*release* | grep redhat)  2>/dev/null
					if [ ! -z "$RELEASEFILE" ]; then
					DistroType="RedHat"
					fi

					RELEASEFILE=$(ls /etc/*release* | grep mandrake) 2>/dev/null
					if [ ! -z "$RELEASEFILE" ]; then
					DistroType="Mandrake"
					fi

					RELEASEFILE=$(ls /etc/*release* | grep fedora)  2>/dev/null
					if [ ! -z "$RELEASEFILE" ]; then
					DistroType="fedora"
					fi

				else
					exit 7
				fi
			else
				if [ -x "`which dpkg 2>/dev/null`" ]; then
					if [ -e "/etc/init.d" ]; then
						#/bin/echo "OS: Debian-like system"
						szOSType="debian"
					else
						exit 7
					fi
				else
					if [ -e "/etc/rc.d/rc.M" ]; then
						#/bin/echo "OS: slackware-like system"
						szOSType="slackware"
						#exit 7
					else
						#/bin/echo "can't recognize your operating system and install services"
						exit 7
					fi
				fi
			fi
		fi	
	fi

#	return 7;
AddService
}

getOs
ln -sfn /lib/reveAM/AMdata/uLoc1/bdcore.so.linux-x86_64 /lib/reveAM/AMdata/uLoc1/bdcore.so

if [ "$OS2" == "Ubuntu 17.10" ];
then
     cp /lib/reveAM/Cdata/qt.conf /lib/reveAM/ > /dev/null 
fi

if [ "$OS1" == "Ubuntu 12" ];
then
     cp /lib/reveAM/Cdata/qt.conf /lib/reveAM/ > /dev/null
fi

startDaemons
OSverC=`lsb_release -ds | cut -c8-9`
if [ $OSverC -gt 14 ];
then 
systemctl daemon-reload
fi

if grep -q "/lib/reveAM/DEPLOYMENT_FILES/lib" "/etc/ld.so.conf"
then
echo "Config enviroment already set"
else
cat>>/etc/ld.so.conf<<EOF
#Begin REVE Antivirus Lib Config
/lib/reveAM/DEPLOYMENT_FILES/lib/
#End REVE Antivirus Lib Config
EOF

fi
ldconfig
exit 0
