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

OS1=`lsb_release -ds | cut -c8-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
		if [ "$OS1" -gt 16 ];then

			cp /usr/local/reveantivirus/tmp/Ubuntu_UninstallerShortcut18 /usr/share/applications/ReveUninstall.desktop
           	else
			cp /usr/local/reveantivirus/tmp/Ubuntu_UninstallerShortcut /usr/share/applications/ReveUninstall.desktop
		fi
            		chmod 744 /usr/share/applications/ReveUninstall.desktop
    

			cp /usr/local/reveantivirus/tmp/Ubuntu_Shortcut /usr/share/applications/ReveAntivirus.desktop
			chmod 744 /usr/share/applications/ReveAntivirus.desktop

			##	
			chmod 744 /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revescanD
			cp -f /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revescanD $SERVICEINITPATH/revescanD

			chmod 744 /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revedeviceD
			cp -f /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revedeviceD $SERVICEINITPATH/revedeviceD

			chmod 744 /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revescheduleD
			cp -f /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revescheduleD $SERVICEINITPATH/revescheduleD

			chmod 744 /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revepupdateD
            cp -f /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revepupdateD $SERVICEINITPATH/revepupdateD

			chmod 744 /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revexscanD
            cp -f /usr/local/reveantivirus/tmp/Ubuntu_Scripts/revexscanD $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
			
            elif [ "$szOSType" = "SuSE" ];then

			
			cp /usr/local/reveantivirus/tmp/Suse_UninstallerShortcut /usr/share/applications/ReveUninstall.desktop
            chmod 744 /usr/share/applications/ReveUninstall.desktop

			cp /usr/local/reveantivirus/tmp/Suse_Shortcut /usr/share/applications/ReveAntivirus.desktop
			chmod 744 /usr/share/applications/ReveAntivirus.desktop
	
			##	
			chmod 744 /usr/local/reveantivirus/tmp/Suse_Scripts/revescanD
			cp -f /usr/local/reveantivirus/tmp/Suse_Scripts/revescanD $SERVICEINITPATH/revescanD

			chmod 744 /usr/local/reveantivirus/tmp/Suse_Scripts/revedeviceD
			cp -f /usr/local/reveantivirus/tmp/Suse_Scripts/revedeviceD $SERVICEINITPATH/revedeviceD

			chmod 744 /usr/local/reveantivirus/tmp/Suse_Scripts/revescheduleD
			cp -f /usr/local/reveantivirus/tmp/Suse_Scripts/revescheduleD $SERVICEINITPATH/revescheduleD

			chmod 744 /usr/local/reveantivirus/tmp/Suse_Scripts/revepupdateD
            cp -f /usr/local/reveantivirus/tmp/Suse_Scripts/revepupdateD $SERVICEINITPATH/revepupdateD

			chmod 744 /usr/local/reveantivirus/tmp/Suse_Scripts/revexscanD
            cp -f /usr/local/reveantivirus/tmp/Suse_Scripts/revexscanD $SERVICEINITPATH/revexscanD


            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"s
					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"s
						#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 /usr/local/reveantivirus/database/sig_p/bdcore.so.linux-x86_64 /usr/local/reveantivirus/database/sig_p/bdcore.so
ln -sfn /usr/local/reveantivirus/lib/libusb-1.0.so /usr/local/reveantivirus/lib/libusb-1.0.so.0

#Qt softLink

#ln -sfn /usr/local/reveantivirus/lib/libicudata.so.54.1 /usr/local/reveantivirus/lib/libicudata.so.54

#ln -sfn /usr/local/reveantivirus/lib/libicui18n.so.54.1 /usr/local/reveantivirus/lib/libicui18n.so.54 

#ln -sfn /usr/local/reveantivirus/lib/libicuuc.so.54.1 /usr/local/reveantivirus/lib/libicuuc.so.54 

#ln -sfn /usr/local/reveantivirus/lib/libQt5Core.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Core.so
#ln -sfn /usr/local/reveantivirus/lib/libQt5Core.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Core.so.5
#ln -sfn /usr/local/reveantivirus/lib/libQt5Core.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Core.so.5.5

#ln -sfn /usr/local/reveantivirus/lib/libQt5DBus.so.5.5.1 /usr/local/reveantivirus/lib/libQt5DBus.so
#ln -sfn /usr/local/reveantivirus/lib/libQt5DBus.so.5.5.1 /usr/local/reveantivirus/lib/libQt5DBus.so.5
#ln -sfn /usr/local/reveantivirus/lib/libQt5DBus.so.5.5.1 /usr/local/reveantivirus/lib/libQt5DBus.so.5.5

#ln -sfn /usr/local/reveantivirus/lib/libQt5Gui.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Gui.so
#ln -sfn /usr/local/reveantivirus/lib/libQt5Gui.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Gui.so.5
#ln -sfn /usr/local/reveantivirus/lib/libQt5Gui.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Gui.so.5.5

#ln -sfn /usr/local/reveantivirus/lib/libQt5X11Extras.so.5.5.1 /usr/local/reveantivirus/lib/libQt5X11Extras.so
#ln -sfn /usr/local/reveantivirus/lib/libQt5X11Extras.so.5.5.1 /usr/local/reveantivirus/lib/libQt5X11Extras.so.5
#ln -sfn /usr/local/reveantivirus/lib/libQt5X11Extras.so.5.5.1 /usr/local/reveantivirus/lib/libQt5X11Extras.so.5.5

#ln -sfn /usr/local/reveantivirus/lib/libQt5XcbQpa.so.5.5.1 /usr/local/reveantivirus/lib/libQt5XcbQpa.so
#ln -sfn /usr/local/reveantivirus/lib/libQt5XcbQpa.so.5.5.1 /usr/local/reveantivirus/lib/libQt5XcbQpa.so.5
#ln -sfn /usr/local/reveantivirus/lib/libQt5XcbQpa.so.5.5.1 /usr/local/reveantivirus/lib/libQt5XcbQpa.so.5.5

#ln -sfn /usr/local/reveantivirus/lib/libQt5XmlPatterns.so.5.5.1 /usr/local/reveantivirus/lib/libQt5XmlPatterns.so
#ln -sfn /usr/local/reveantivirus/lib/libQt5XmlPatterns.so.5.5.1 /usr/local/reveantivirus/lib/libQt5XmlPatterns.so.5
#ln -sfn /usr/local/reveantivirus/lib/libQt5XmlPatterns.so.5.5.1 /usr/local/reveantivirus/lib/libQt5XmlPatterns.so.5.5

#ln -sfn /usr/local/reveantivirus/lib/libQt5Xml.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Xml.so
#ln -sfn /usr/local/reveantivirus/lib/libQt5Xml.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Xml.so.5
#ln -sfn /usr/local/reveantivirus/lib/libQt5Xml.so.5.5.1 /usr/local/reveantivirus/lib/libQt5Xml.so.5.5

#if [ "$OS2" == "Ubuntu 17.10" ];
#then
 #    cp /usr/local/reveantivirus/conf/qt.conf /usr/local/reveantivirus/bin > /dev/null 
#fi
#if [ "$OS1" == "Ubuntu 12" ];
#then
 #    cp /usr/local/reveantivirus/conf/qt.conf /usr/local/reveantivirus/bin > /dev/null
#fi

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

#echo "/usr/local/reveantivirus/lib" > /etc/ld.so.conf.d/a_reveantivirus.conf
#ldconfig

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

exit 0
