#! /bin/bash

# FUNCTIONS

# function to print the usage message
print_using() {
	echo "Usage: $0 [<ip address file> <copy files file> <remote script file>]"
	echo ""
	echo "If <ip address file> is not set, file 'ip-addresses.txt' is used."
	echo "If <copy files file> is not set, file 'copy_files.txt' is used."
	echo "If <remote script file> is not set, file 'remote_script.sh' is used."
	echo ""
	echo "This script requires 'sshpass'."
}

# copy files to remote host
copy_files() {
	ip=$1
	copy_files_file=$2

	# for every line in copy files file
	for file in `cat "$copy_files_file"`; do

		if [[ $file == \#* ]]; then
			# skip lines start with #
			#echo skip $file
			: # do nothing
		else
			fromfile=`echo "$file" | cut -d, -f1`
			tofile=`echo "$file" | cut -d, -f2`

			if [[ -z tofile ]]; then
				tofile=fromfile
			fi

			echo ""
			echo "Copying file '$fromfile' to  host '$ip:$tofile' ..."
			sshpass -p "$PASSWORD" scp -o StrictHostKeyChecking=no "$fromfile" "$username@$ip:$tofile" || echo >&2 "Failed copying file."
		fi

	done
}

# run script on remote host
run_remote_script() {
	ip=$1
	remote_script_file=$2

	echo ""
	echo "Running script '$remote_script_file' on host '$ip' ..."
	# copy script file
	sshpass -p "$PASSWORD" scp -o StrictHostKeyChecking=no "$remote_script_file" "$username@$ip:$remote_script_file"  || echo >&2 "Failed copying script."
	# start script file and delete file afterward
	sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no "$username@$ip" "sh $remote_script_file; rm $remote_script_file"  || echo >&2 "Failed executing script."

}

# function that does something for every ip address in file $IP_ADDR_FILE
do_for_ip_address() {
	ip=$1
	echo ""; echo ""; echo ""
	echo "Using IP: $ip ..."

	# do here what is you want to do for every IP address ...
	copy_files $ip $2
	run_remote_script $ip $3

	echo ""
	echo "Done with IP: $ip."
}



# HELP PARAMETER

# if the first paramter of this script is "-h" than print the usage message
if [[ "$1" == "-h" ]]; then
	print_using
	exit 0;
fi


# Check if sshpass is installed on this machine
command -v sshpass >/dev/null 2>&1 || {
	echo >&2 "Error: 'sshpass' is not installed.  Aborting."
	echo ""
	print_using
	exit 4
}




# FILE NAME PARAMETERS

# check if the first parameter is empty or not
if [[ -z $1 ]]; then
	# use default file name
	IP_ADDR_FILE="ip-addresses.txt"
else
	# use first parameter as file name
	IP_ADDR_FILE="$1"
fi

# check if IP address file exists
if [[ ! -f "$IP_ADDR_FILE" ]]; then
	echo >&2 "Error: File '$IP_ADDR_FILE' does not exist.  Aborting.";
	echo ""
	print_using
	exit 1;
fi

echo "Using IP address file: '$IP_ADDR_FILE' ..."



# check if the second parameter is empty or not
if [[ -z $2 ]]; then
	# use default file name
	COPY_FILES_FILE="copy_files.txt"
else
	# use second parameter as file name
	COPY_FILES_FILE="$2"
fi

# check if copy files file exists
if [[ ! -f "$COPY_FILES_FILE" ]]; then
	echo >&2 "Error: File '$COPY_FILES_FILE' does not exist.  Aborting.";
	echo ""
	print_using
	exit 2;
fi

echo "Using copy files file: '$COPY_FILES_FILE' ..."



# check if the third parameter is empty or not
if [ -z $3 ]; then
	# use default file name
	REMOTE_SCRIPT_FILE="remote_script.sh"
else
	# use third parameter as file name
	REMOTE_SCRIPT_FILE="$3"
fi

# check if remote script files file exists
if [ ! -f "$REMOTE_SCRIPT_FILE" ]; then
	echo >&2 "Error: File '$REMOTE_SCRIPT_FILE' does not exist.  Aborting.";
	echo ""
	print_using
	exit 3;
fi

echo "Using remote script file: '$REMOTE_SCRIPT_FILE' ..."



# PROMPT FOR PASSWORD
echo ""
username=root
read -s -p "Enter SSH password: " PASSWORD



# LOOP IP ADDRESS FILE


# for each line in $IP_ADDR_FILE
# set the field seperator to a newline
IFS="
"
# for each field in file
for ip in `cat "$IP_ADDR_FILE"`; do

	if [[ $ip == \#* ]]; then
		# skip lines start with #
		#echo skip $ip
		: # do nothing
#	elif [ -z $ip ]; then
#		# skip empty line
#		echo "skip empty line"
	else
		do_for_ip_address $ip $COPY_FILES_FILE $REMOTE_SCRIPT_FILE
	fi

done
