#!/bin/bash

# DISCLAIMER   :   It is recomended to test this script on a test machine.
#				           ManageEngine will not be responsible for any
#                  damage/loss to the data/setup based on the behavior of the script.

# DESCRIPTION  :   Script to copy file(s) or directorie(s) within linux agent machine.
#                  or copy file(s) from the server console to linux agent machine by uploading Dependency File(s)
#
# ARGUMENT(S):

#  1) To copy file(s)/ directorie(s) within linux agent machine.

#       ARGUMENT FORMAT: <source_path(s)_in_linux_agent_machine> <destination_path>
#       EXAMPLE        : "/home/user/mysamples home/user/myfiles/" /home/user/Desktop/

#  2) To copy only file(s) from server to linux agent machine

#       ARGUMENT FORMAT: <exact_name_of_Dependency_File(s)> <destination_path>
#       EXAMPLES         1) copy_my_files.txt /home/user/Desktop
#                        2) "file1.sh file2.bash" /home/user/Desktop

#       DEPENDENCY FILE(S): Upload files which should be copied from server to linux agent machine.
#
#           RETURN VALUE                  MEANING

#                0             File(s)/ directorie(s) copied sucessfully.
#                1             Error while copying the File(s)/ directorie(s).
#                2             Invalid arguments.

# NOTE :
#   To see the script output, Kindly enable the option Enable logging in Troubleshooting while deploying configuration.

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

  #check no of arguments
  if [ $# -ne 2 ]; then
    echo "Incorrect Usage : Arguments mismatch."
    echo "Refer ARGUMENT(S) section in the script."
    break
  fi

  errorCode=0
  sourcePath=$1
  destPath=$2

  #copy the file using cp command
  cp -r $sourcePath $destPath

  if [ $? -eq 0 ]; then
    echo "Copied : $sourcePath to $destPath"
  else
    echo "Could not copy the content"
    errorCode=1
  fi

done

errorFunc() {
  return $errorCode
}
errorFunc
