#!/bin/sh

#
# This script is use to check raidcom version ,do installation and change ownership of HORCM files (from root to megha) .
#

# This variable is use to store raidcom path.
raidPath=$1
echo $raidPath


baseFolder=/usr/local/megha
# This variable use to hold path of svp Folder
svpFolder="$baseFolder/lib/svp"

# This variable use to hold path of prob properties file folder
svpPropFileFolder="$baseFolder/conf/probe"

# This variable is use to hold path of error.txt and version.txt file
errorFile="$svpFolder/raidcomErrorfile.txt"
raidVersionFile="$svpFolder/raidcomVersion.txt"
svpLogFolder="$baseFolder/logs/SVPProbe"
logfile="$svpLogFolder/raidcom.log"

# Create SVPProbe folder inside logs if not present
if [ `file $svpLogFolder |grep "No such file"* |wc -w` -gt 0 ]
then
mkdir "$svpLogFolder"
chown -R megha:megha "$svpLogFolder"
fi

# This function is used to log a message in the log file
        
logMsg() {

time=$(date +"%Y-%m-%d %H:%M:%S")
	
echo "$time $1" >>$logfile

}

logMsg "Info: Start Raidcom Installation"

# Create svp Folder if not present.
if [ `file $svpFolder |grep "No such file"* |wc -w` -gt 0 ]
then
	logMsg "Info: Creating folder $svpFolder"
	mkdir "$svpFolder"
fi

# Create temp folder inside svp if not present
if [ `file $svpFolder/temp |grep "No such file"* |wc -w` -gt 0 ]
then
        logMsg "Info: Creating folder $svpFolder/temp"
        mkdir "$svpFolder/temp"
fi

# This function clean any file inside $raidPath
cleanUp () {
	
	logMsg "Info: Inside cleanUp, deleting files under [$raidPath]"
	rm -rf $raidPath/*
	
}


# Check if RMHORC file present or not.

if [ `file $raidPath/RMHORC |grep "No such file"* |wc -w` -gt 0 ]
then
	logMsg "Error: RMHORC file not uploaded."
	echo "Error:PADUR_YOU_HAVE_NOT_UPLOADED_RMHORC_FILE,_PLEASE_PROVIDE_CORRECT_FILE" > $errorFile;
	cleanUp
	exit 1;
fi


# Change current directory to raidcom directory where RMHORC present.
cd $raidPath

# Use "cpio" command to created directory structure of HORCM.
cpio -idmu < RMHORC

# check if raidqry file present or not, if not exit with error msg

if [ `file HORCM/usr/bin/raidqry |grep "No such file"* |wc -w` -gt 0 ]
then
	logMsg "Error: raidqry file is not present."
	echo "Error:PADUR_RAIDQRY_FILE_NOT_PRESENT" > $errorFile
	cleanUp
	exit 1;
#elif [ `./HORCM/usr/bin/raidqry -h |grep "Model"* | grep "Linux"* |wc -w` -gt 0 ]
else
	# Dump file information.
	logMsg "Info: File [HORCM/usr/bin/raidqry] Information :"
	file HORCM/usr/bin/raidqry >>$logfile 2>&1
	logMsg "Info: HORCM/usr/bin/raidqry output :"
	./HORCM/usr/bin/raidqry -h >>$logfile 2>&1
	raidVer=`./HORCM/usr/bin/raidqry -h |grep "Ver&Rev"* |cut -d ":" -f 2 | sed 's/\s*//g' | sed 's/\//-/g'`
	
fi

# Check for 64 bit 
#if [ `file HORCM/usr/bin/raidqry | grep "32-bit"* | wc -w` -gt 0 ]
#then
#	logMsg "Error: Not a 64-bit version of RMHORC file."
#	echo "Error:PADUR_NOT_A_SUPPORTED_TYPE,_PLEASE_PROVIDE_64_BIT_VERSION_OF_RMHORC_FILE" > $errorFile
#	cleanUp
#	exit 1	
#
#fi

# Check if file is of IA-64 Art
if [ `file HORCM/usr/bin/raidqry | grep "IA-64"* | wc -w` -gt 0 ]
then
	logMsg "Error: Provided file is of IA-64 version of RMHORC file."
	echo "Error:PADUR_NOT_A_SUPPORTED_TYPE,_PLEASE_PROVIDE_64_BIT_VERSION_OF_RMHORC_FILE" > $errorFile
	cleanUp
	exit 1

fi

# Check if machine already have raidcom installed on it.
if [ `file /HORCM/usr/bin/raidqry |grep "No such file"* |wc -w` -eq 0 ]
then
	logMsg "Info: Already have raidcom Installed"
	
	logMsg "Info: Compare version"
	
	raidOldVer=`/HORCM/usr/bin/raidqry -h |grep "Ver&Rev"* |cut -d ":" -f 2 | sed 's/\s*//g' | sed 's/\//-/g'`
	# Compare these two version.
	#$raidVer=` $raidVer sed 's/\/-/g'`	
	logMsg "Info: Given raidcom version :$raidVer and old raidcom version :$raidOldVer"
	
if [ $raidOldVer = $raidVer ]
then
	logMsg "Error: Provided Raidcom version [$raidVer] is already installed."
    echo "Error:PADUR_PROVIDED_VERSION_%S_HAS_ALREADY_INSTALLED=$raidVer" > $errorFile
	cleanUp
	exit 1
elif [ $raidOldVer \> $raidVer ]
then
	logMsg "Error: Provided Raidcomversion [$raidVer] is older than  installed version [$raidOldVer]."
    echo "Error:PADUR_PROVIDED_VERSION_%S_IS_OLDER_THAN_ALREADY_INSTALLED_VERSION_%S=$raidVer=$raidOldVer" > $errorFile
	cleanUp
	exit 1
else
		logMsg "Info: given raidcom version is new, Uninstall old raidcom and install new version"
	
	#check if any configuration data collection process runing, if runing then sleep for 5 min and again check.
	while [ `ps -ef | grep perf |grep confDataGenerator.pl | wc -w` -gt 0 ]
	do
		logMsg "Info: Configuration data collection is in progess, wait till data collection get complete."
		sleep $sleepTime
	done
	
	# shutdown all runing instance number (Read instance number from insNumList.txt file)
	for n in $(find $svpPropFileFolder -name SVP*.properties)
	do
		while read LINE
		do
			if [ `echo $LINE | grep "InstanceNumber"* | wc -w` -gt 0 ]; then
				insNumber=`echo $LINE | grep "InstanceNumber"* | cut -d "=" -f 2 | sed 's/\s*//g'`
				logMsg "Info: shuting down instance [$insNumber]"
				horcmshutdown.sh $insNumber
			fi
		done < $n
	done
	
	# Run unstall script
	/HORCM/horcmuninstall.sh > /dev/null 2>&1
	
	# Delete raidcom folder Folder
	logMsg "Info: Deleting raidcom folder [$svpFolder/raidcom_$raidOldVer]."
	rm -rf $svpFolder/raidcom_$raidOldVer
	
	# Delete symbolic link
	rm -f /HORCM
	
fi

fi
	
	
raidcomFolder="$svpFolder/raidcom_$raidVer"

# Create if raidcomFolder not present
if [ `file $raidcomFolder |grep "No such file"* |wc -w` -gt 0 ]
then
	logMsg "Info: Creating folder [$raidcomFolder]"
	mkdir "$raidcomFolder"
fi

cd "$svpFolder"

# Move given raidPath folder to /usr/local/megha/raidcomFolder
mv "$raidPath"/* "$raidcomFolder/" >>$logfile 2>&1

# Remove symbolic link if present.
rm -f /HORCM

# Create symbolic link for /HORCM.
ln -s "$raidcomFolder/HORCM" /HORCM >>$logfile 2>&1

# Executes the RAID Manager installation command.
logMsg "Info: Installing raidcom."

/HORCM/horcminstall.sh > "$svpFolder/temp/raidOutput.txt" 2>&1

# Check for successfull installation
if [ `wc -w < $svpFolder/temp/raidOutput.txt` -gt 0 ]
then
	# installation failed. 
	logMsg "Error: Installation of Raidcom [$raidVer] is failed."
	echo "Error:PADUR_INSTALLATION_OF_RAIDCOM_%S_FAILED=$raidVer" > $errorFile
	
	# Do clean up
	
	# Delete raidcom folder Folder
	rm -rf $svpFolder/raidcom_$raidVer
	
	# Delete symbolic link
	rm -f /HORCM
	
	exit 1;
fi

rm -f $svpFolder/temp/raidOutput.txt

# Start all stoped instances
logMsg "Info: Start all stoped instances"
for n in $(find $svpPropFileFolder -name SVP*.properties)
do
	while read LINE
    do
        if [ `echo $LINE | grep "InstanceNumber"* | wc -w` -gt 0 ]
		then
            insNum=`echo $LINE | grep "InstanceNumber"* | cut -d "=" -f 2 | sed 's/\s*//g'`
			logMsg "Info: Starting instance [$insNumber]."
            horcmstart.sh $insNum
		fi

    done < $n
done

# change ownership of files so to allow megha user to run raidcom commands
logMsg "Info: Changing ownership of files so to allow megha user to run raidcom commands"
chown megha /HORCM/etc/horcmgr
chown -R megha /HORCM/usr/bin/
chown -R megha /HORCM/log*
chown -R megha /HORCM/usr/var/
chown -R megha:megha "$svpFolder"
chown -R megha:megha "$svpLogFolder"
# run /HORCM/etc/horcmgr (ToDo: should it run as megha or root)
#sudo -u megha /HORCM/etc/horcmgr
/HORCM/etc/horcmgr


# Write Raidcom version to given file
logMsg "Info: writing raidcom version [ $raidVer ] to file [ $raidVersionFile ]."
echo "RaidcomVersion=$raidVer" > $raidVersionFile
exit 0;
