#!/bin/bash

# Description   :   This script is used to install mac agent through ssh in computers specified in a text file which is passed as argument.
#
# Returns       :   0 if executed successfully with or without errors.
#                   2 if no arguments are given / Agent not found in the same directory
#
# Usage         :   RemoteAgentInstallerforMac.sh ${file_containing_computer_names_seperated_by_newline}
#
# Example       :   RemoteAgentInstallerforMac.sh computers.txt
#                   computer.txt contains list of computer names(seperated by newline character) where the agent is to be installed
#
# Maintainer    :   ManageEngine Desktop Central <desktopcentral-support@manageengine.com>

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

agentdirectory=$DIR

agent=0

diff=$(ls "$agentdirectory" | grep UEMS_MacAgent.pkg)
if [ $? -ne 0 ]
then
    echo "No package file available in the directory. Please make sure the 'script', 'UEMS_MacAgent.pkg' and 'serverinfo.plist' are in the same directory."
    exit 2
fi


if [ $# -lt 1 ]
 	then
 		echo "Incorrect Usage : No arguments given."
 		echo "Usage : RemoteAgentInstallerforMac.sh \${file_containing_computer_names_seperated_by_newline}"
 		exit 2
fi

echo "The below entered credential will be used to install agent in remote machine(s) specified in the text file."

echo -n "Enter Username : "
read userName

echo -n "Enter Password : "
read -s password

filename=$1
filelines=`cat "$DIR/$filename"`

echo "----------------------------------------------------------------------"
for sysName in $filelines;
do

	echo "Trying to copy files to $sysName using $userName credentials"
    
    sshpass -p "$password" scp "$agentdirectory/UEMS_MacAgent.pkg" "$userName"@"$sysName":

    sshpass -p "$password" scp "$agentdirectory/serverinfo.plist" "$userName"@"$sysName":

    echo "Successfully copied files. DC Agent installation initiated"
    
    sshpass -p "$password" ssh "$userName"@"$sysName" chmod 755 UEMS_MacAgent.pkg
    
        if [ $? -ne 0 ]
    then
        echo "DC Agent chmod failed for $sysName."
    else
        echo "DC Agent chmod success for $sysName."
    fi
    
    if [[ "$userName" == "root" ]]
    then
        sshpass -p "$password" ssh "$userName"@"$sysName" "installer -pkg UEMS_MacAgent.pkg -target /"
    else
        echo "Script will prompt for Password. Not need to enter password."
        sshpass -p "$password" ssh "$userName"@"$sysName" "echo \"$password\" | sudo -S installer -pkg UEMS_MacAgent.pkg -target /"
    fi

    if [ $? -ne 0 ]
    then
        echo "DC Agent installation failed for $sysName."
    else
        echo "DC Agent installation success for $sysName."
    fi


    sshpass -p "$password" ssh "$userName"@"$sysName" rm -f UEMS_MacAgent.pkg
    sshpass -p "$password" ssh "$userName"@"$sysName" rm -f serverinfo.plist

    echo "Temporary files removed from $sysName"

    echo "----------------------------------------------------------------------"

done

exit 0
