#!/bin/sh

# Usage: sh setup.sh 
#
# To Verify the size of the file after join, please modify the variable
# TOTAL_SIZE Give the total size of the file expected in bytes 
# (ex. TOTAL_SIZE=8889717 for 8 MB)
#
# To specify the split files that are to be joined edit the variable 
# FILE_LIST. The files specified in this list only will be joined.  Make 
# sure the file list is given within double quotes and are separated by 
# single space (ex.  FILE_LIST="Advent1.zip Advent2.zip").

# Finally to give a name to the joined file, edit the variable DEST_FILE_NAME 
# (ex.  DEST_FILE_NAME="Advent.zip")

CUR_DIR=`pwd`

TOTAL_SIZE=144616890

FILE_LIST="AdventNet_WebNMS_5_Solaris_Professional_1.bin AdventNet_WebNMS_5_Solaris_Professional_2.bin AdventNet_WebNMS_5_Solaris_Professional_3.bin"

DEST_FILE_NAME="AdventNet_WebNMS_5_Solaris_Professional.bin"

#	Joins the file

if [ ! -f ${CUR_DIR}/${DEST_FILE_NAME} ]
then

	for file in ${FILE_LIST}
	do

		if [ -f ${CUR_DIR}/${file} ]
		then
			cat ${CUR_DIR}/${file} >> ${CUR_DIR}/${DEST_FILE_NAME}
			if [ $? -ne 0 ]
				then
					exit 1
			fi		
		else
			echo "File ${CUR_DIR}/${file} Not Found,  Cannot Proceed Further!!!"
			#echo "Cleaning up ${CUR_DIR}/${DEST_FILE_NAME} ..."
			rm -f ${CUR_DIR}/${DEST_FILE_NAME}
			exit 1
		fi

	done

else

	echo "File ${CUR_DIR}/${DEST_FILE_NAME} Already Exists, Cannot Proceed Further!!!"
	exit 1

fi

#	Checks the size of the joined file with Total Size given for reference

JOIN_SIZE=`ls -l ${CUR_DIR}/${DEST_FILE_NAME} | awk '{print $5}'`

if [ ${TOTAL_SIZE} -eq ${JOIN_SIZE} ]
then
    chmod a+x ${DEST_FILE_NAME}	
    echo "The file ${DEST_FILE_NAME} is ready for installation."
else
    echo "Size not Matched!!! Possible cause downloaded file(s) may be corrupted ..."
	#	echo "Cleaning up ${CUR_DIR}/${DEST_FILE_NAME} ..."
		rm -f ${CUR_DIR}/${DEST_FILE_NAME}
fi
