#!/bin/bash

# Description   :   This script is used to copy the content from the source to the destination location using 'cp -r' command.
#
# Returns       :   0 if the files are successfully copied.
#                   1 if there is an error failure in copying.
#                   2 if there are invalid arguments.
#
# Usage         :   sh CopyFiles.sh ${src_path} ${dest_path}
#
# Example       :   sh CopyFiles.sh "/home/user/old or home/user/myfiles/ " "/home/user/Desktop/"
#
# Maintainer    :   ManageEngine Desktop Central <desktopcentral-support@manageengine.com>

errorCode=2

euid=$(id -u)
for i in 1
    do

	#check root access
	if [ $euid -ne 0 ]; then
	   echo "This script must be run as root"
	   break
	fi

	if [[ ! -e UEMS_LinuxAgent.bin ]]; then
		echo "UEMS_LinuxAgent.bin file not found"
		break
	fi
	
	if [[ ! -e DMRootCA-Server.crt ]]; then
		echo "DMRootCA-Server.crt file not found"
	fi

	if [[ ! -e DMRootCA.crt ]]; then
		echo "DMRootCA.crt file not found"
	fi


	if [[ ! -e serverinfo.json ]]; then
		echo "serverinfo.json file not found"
		break
	fi

	if [[ ! -e LinuxAgentInstaller.sh ]]; then
		echo "LinuxAgentInstaller.sh file not found"
		break
	fi

	chmod +x LinuxAgentInstaller.sh

	mkdir -p /dcAgentFiles

	#copy the file using cp command
	cp -fr ./* /dcAgentFiles

	echo "*/10 * * * * root /dcAgentFiles/LinuxAgentInstaller.sh" >>/etc/crontab

	if [ $? -eq 0 ]
	    then
		echo "Copied successfully"
                errorCode=0
	    else
		echo "Could not copy the files"
		errorCode=1
	fi


done
errorFunc(){
 return $errorCode
}
errorFunc
