#!/bin/sh

# Copyright 2008-2014 Cumulus Systems Incorporated.
# All Rights Reserved.

ROOTDIRNAME="probe";
DATACOLLECTIONSCRIPT="dataGather.sh";
LOGFILENAME="unixTargetHostLogs.log";

#getting input arguments passed to script
operation=$1;
installDir=$2;

#Store the output for umask
umaskOutput=""

# Set the Installation directory path and probe home path
#
# @param:
#    None.
#
# @return:
#   0 if Success
#   1 if Error
init()
{
	echo "initializing the install directory and probe home."
	
	#Checked parameter for installation directory is not null
	if [ "$installDir" = "" ]; then
		echo "Please input valid installation directory" 
		return 1
	fi

	#Checked if the directory is exists or not
	if [ ! -d $installDir ]; then
		echo "Installation directory does not exists: [$installDir]" 
		return 1
	fi

	# Set probe home path.
	probeHome="$installDir/$ROOTDIRNAME"
	
	if [ ! -d $probeHome ]; then
		echo "Probe directory does not exist : [$probeHome]"
		return 1
	fi	

	cd $installDir
	
	return 0;
}

# Used to install Unix probe software on the target system.
#
# @param:
#    None.
#
# @return:
#	0 if Success
#	1 if Error
doInstall()
{	
	#create logs directory to dump log file
	mkdir -p "$installDir/logs"
	
	if [ $? -ne 0 ]; then
		echo "Unable to create logs directory: [$installDir/logs]";
		return 1
	fi		
	
	#delete already exist log file
	rm -f "$installDir/logs/$LOGFILENAME"
	
	if [ $? -ne 0 ]; then
		echo "Unable to delete already exist log file: [$installDir/logs/$LOGFILENAME]";
		return 1;
	fi
	
	#create log file to dump logs
	touch "$installDir/logs/$LOGFILENAME"
		
	if [ $? -ne 0 ]; then
		echo "Unable to create log file: [$installDir/logs/$LOGFILENAME]";
		return 1;
	fi	
	
	logMessage "Info" "Installing probe"
	
	umaskOutput=`umask`

	# Checked whether the directory have 0755 permission
	if [ "$umaskOutput" -ne "0022" ]; then
		logMessage "Error" "Install directory does not have permission 0755: [$installDir]"
		return 1
	fi	

	# Checked if the directory is symbolic link
	if [ -h $installDir ]; then
		logMessage "Error" "Install directory is a symbolic link"
		return 1
	fi
			
	# Checked if the runtime.cf file exist then delete it
	if [ -s $probeHome/conf/runtime.cf ]; then
		rm -rf $probeHome/conf/runtime.cf
		status=$?
				
		# Checked if the file is deleted successfully
		if [ $? -ne 0 ]; then
			logMessage "Error" "Unable to delete existing runtime.cf file"
			return 1
		fi
	fi 

	# Checked if runtime.cf.new exists or not
	if [ ! -s $probeHome/conf/runtime.cf.new ]; then
		logMessage "Error" "runtime.cf.new does not exists"
		return 1
	fi
	
	# rename the runtime.cf.new file
	mv $probeHome/conf/runtime.cf.new $probeHome/conf/runtime.cf
	status=$?

	# Checked if the runtime.cf file is renamed successfully or not
	if [ $? -ne 0 ]; then
		logMessage "Error" "Unable to rename runtime.cf.new file to runtime.cf file"
        return 1
    fi
	
	doGrantPermission
	
	return $?
}

# Start Unix probe application from system. It will collect configuration data.
#
# @param
#    None.
#
# @return:
#	0 if Success
#	1 if Error
doStart()
{
	#cd $probeHome
	
	#collect configuration data by using target based script
	sh $probeHome/lib/$DATACOLLECTIONSCRIPT -a $installDir
	
	if [ $? -ne 0 ]; then
		return 1;
	fi

	return 0;
}

# Grant appropriate permission to scripts on target machine
#
# @param
#    None.
#
# @return:
#	0 if Success
#	1 if Error
doGrantPermission()
{
	chmod -R a+x $probeHome/lib
	
	if [ $? -ne 0 ]; then
		logMessage "Error" "Unable to grant permission to lib: [$probeHome/lib]"
		return 1;
	fi	
	
	return 0;
}

# It will write the log message to log file
#
# @param
#	log type
#	log message	
#
# @return:
#	0 if Success
#	1 if Error
logMessage()
{	
	currentDateWithTimeZone=`date +"%Y-%d-%m %R:%S %z"`
	echo "[$currentDateWithTimeZone] [$1]: $2" >>"$installDir/logs/$LOGFILENAME"
}

# It will print the usage of this script
#
# @param
#	None
#
# @return:
#	None
usage()
{
	echo "Usage: sh doInstall.sh <task option> <install directory path> \n"
	echo "Task options are: \n"
	echo "-i                         : Installation mode.\n"
    echo "-a                         : Start data collection mode"
}

if [ "$#" -ne 2 ]; then
	echo "Incorrect number of arguments specify"
	usage
	
	exit 1;
fi	

case "$1" in
	-i)
		init
		
		if [ $? -ne 0 ]; then 
			echo "Failed to initialize environment"
			exit 1;
		fi	
		
		doInstall
		
		if [ $? -ne 0 ]; then 
			logMessage "Error" "Failed to install probe"
			exit 1;
		fi
		
		logMessage "Info" "Probe get installed successfully"
	    ;;
	-a)
		init
		
		if [ $? -ne 0 ]; then 
			logMessage "Error" "Failed to initialize environment for action start probe"
			exit 1;
		fi	
		
		logMessage "Info" "Start data collection"
		
		doStart
		
		if [ $? -ne 0 ]; then 
			logMessage "Error" "Failed to start data collection"
			exit 1;
		fi

		logMessage "Info" "Data get collected successfully"
		;;
    *)
        echo "Unknown argument '$1'" >&2
        exit 1
        ;;
esac
