#!/bin/sh
#/**
# * \file  kccmUpgrade
# * \brief kccmUpgrade  file 
# * \details \n
# * This file copy the kccm application and restart the s3 and AWS Application
# * \note \n
# * KCCM\n
# * COPYRIGHT (C) ALL RIGHTS RESERVED.\n
# * \n
# * The reproduction, transmission or use of this document or its contents\n
# * is not permitted without express written authority. Offenders will be\n
# * liable for damages. All rights, including rights created by patent\n
# * grant or registration of a utility model or design, are reserved.\n
# * \n
# * \version n/a
# *
# */
shaFlag=0
searchDirectory=/root/Upgradefile/kccm/
destDirectory=/kccm/
ScriptFolder=/kccm/upgrade/
ScriptDir=/kccm/upgrade/rfsupgrade
currdir=$(pwd)

if [ -r $searchDirectory ]; then
	for entry in "$searchDirectory"/*
	do
	        extractedBinFile=$(basename $entry)
	        echo "Name of the file in actual bin file " $extractedBinFile
	        if [ -r $destDirectory/$extractedBinFile ]; then
			echo "File '$extractedBinFile' Exists"
			if [ -d $destDirectory/$extractedBinFile ]; then
				cp -rf $searchDirectory/$extractedBinFile/* $destDirectory/$extractedBinFile/
			else
				cp -rf $searchDirectory$extractedBinFile $destDirectory/
			fi
	        else
			echo "The File '$extractedBinFile' Does Not Exist"
			cp -rf $searchDirectory$extractedBinFile $destDirectory/
		fi
	done
	rm -rf $searchDirectory
	echo "Self Upgrade Completed"
	shaFlag=1
else
	echo "No file is available to start selfupgrade"
fi

if [ -r $ScriptDir ]; then
	echo "rfs upgrade started"
      	chmod +x $ScriptDir
      	cd /kccm/upgrade/
      	chmod +x rfsupgrade
      	./rfsupgrade
	cd /
	shaFlag=1
fi

if [ -r $ScriptFolder ]; then
	rm -rf $ScriptFolder
fi

if [ $shaFlag -eq 1 ]; then
	cd /home/
	./calculateshasum.sh /kccm/
	./update.sh
	sync
	cd /
fi
