#!/bin/sh
CORE_ROOT=/
CORE_FILES_DIR=core_files
MNT_DIR=mnt
checkResult() {
	case $ftpResult in
0)	printf "Files transferred successfully!\n"
	exit
	;;
1)	printf "Could not connect to remote host.\n"
	;;
2)	printf "Could not connect to remote host - timed out.\n"
	;;
3)	printf "Transfer Failed!\n"
	;;
4)	printf "Transfer Failed - Timed out.\n"
	;;
5)	printf "Directory change failed.\n"
	;;
6)	printf "Directory change failed - timed out.\n"
	;;
7)	printf "Malformed URL.\n"
	;;
8)	printf "Useage Error.\n"
	;;
9)	printf "Error in login configuration file\n"
	;;
10)	printf "Library initialization failed.\n"
	;;
11)	printf "Session initialization failed.\n"
	;;
*)	printf "NCFTP Failed (%d)!\n" $ftpResult
	;;

	esac
}
ftpCoreFilesListAll() {
	markedFiles=0
	idx=0
	ftpFileList=" "
	while [ $idx -lt $coreFilesCount ]
	do
		ftpFileList="${ftpFileList}  ${coreFileArry[$idx]}"
#		ftpFileList="${ftpFileList}  `/usr/bin/dirname ${coreFileArry[$idx]}`"
		markedFiles=`/fabos/bin/expr ${markedFiles} + 1`
		idx=`/fabos/bin/expr ${idx} + 1`
	done
	hostName="${argHostName:-UnKnown}"
	printf "Destination IP Address[${hostName}]? "
	read input
	if [ -n "$input" ]
	then
		cmdHostName="$input"
	else
		cmdHostName="$hostName"
	fi

	userName="${argUserName:-anonymous}"
	printf "User Name[${userName}]? "
	read input
	if [ -n "$input" ]
	then
		cmdUserName="-u $input"
	else
		cmdUserName="-u $userName"
	fi

	remoteDir="${argRemoteDir:-.}"
	printf "Remote Directory Name[${remoteDir}]? "
	read input
	if [ -n "$input" ]
	then
		cmdRemoteDir="$input"
	else
		cmdRemoteDir="$remoteDir"
	fi

#
# ncftpput does not like "remote directory name" (cmdRemoteDir) ending with
# a "/" and so we will do a manipulation....
#
	cmdRemoteDir=`echo ${cmdRemoteDir} | /bin/sed 's/\/*$//' `
	if [ -z ${cmdRemoteDir} ]
	then
		# if we are left out with nothing, put a "." there
		if [ "X${input}" = "X/" ]
		then
			cmdRemoteDir=/
		else
			cmdRemoteDir=.
		fi
	fi

	if [ -n "$argPassWord" ]
	then
		cmdPassWord="-p ${argPassWord}"
	else
		cmdPassWord=""
	fi
	/bin/ncftpput  -R -m ${cmdUserName} ${cmdPassWord} ${cmdHostName} \
		${cmdRemoteDir}  ${ftpFileList}
	ftpResult=$?
	checkResult
}
ftpCoreFilesListMarked() {
	markedFiles=0
	idx=0
	ftpFileList=" "
	while [ $idx -lt $coreFilesCount ]
	do
		if [ "${coreSelArry[${idx}]}" = "*" ]
		then
			#ftpFileList="${ftpFileList}  `/usr/bin/dirname ${coreFileArry[$idx]}`"
			ftpFileList="${ftpFileList}  ${coreFileArry[$idx]}"
			markedFiles=`/fabos/bin/expr ${markedFiles} + 1`
		fi
		idx=`/fabos/bin/expr ${idx} + 1`
	done
	hostName="${argHostName:-UnKnown}"
	printf "Destination IP Address[${hostName}]? "
	read input
	if [ -n "$input" ]
	then
		cmdHostName="$input"
	else
		cmdHostName="$hostName"
	fi

	userName="${argUserName:-anonymous}"
	printf "User Name[${userName}]? "
	read input
	if [ -n "$input" ]
	then
		cmdUserName="-u $input"
	else
		cmdUserName="-u $userName"
	fi

	remoteDir="${argRemoteDir:-.}"
	printf "Remote Directory Name[${remoteDir}]? "
	read input
	if [ -n "$input" ]
	then
		cmdRemoteDir="$input"
	else
		cmdRemoteDir="$remoteDir"
	fi
#
# ncftpput does not like "remote directory name" (cmdRemoteDir) ending with
# a "/" and so we will do a manipulation....
#
	cmdRemoteDir=`echo ${cmdRemoteDir} | /bin/sed 's/\/*$//' `
	if [ -z ${cmdRemoteDir} ]
	then
		# if we are left out with nothing, put a "." there
		if [ "X${input}" = "X/" ]
		then
			cmdRemoteDir=/
		else
			cmdRemoteDir=.
		fi
	fi

	if [ -n "$argPassWord" ]
	then
		cmdPassWord="-p ${argPassWord}"
	else
		cmdPassWord=""
	fi

	/bin/ncftpput  -R -m ${cmdUserName} ${cmdPassWord} ${cmdHostName} \
		${cmdRemoteDir}  ${ftpFileList}
	ftpResult=$?
	checkResult
}
# Function to remove marked core files
rmCoreFilesListMarked() {
	markedFiles=0
	idx=0
	while [ $idx -lt $coreFilesCount ]
	do
		if [ "${coreSelArry[${idx}]}" = "*" ]
		then
			printf "removing core files under: %s\n" ${coreFileArry[$idx]}
			/bin/rm -f  ${coreFileArry[$idx]}/*
			markedFiles=`/fabos/bin/expr ${markedFiles} + 1`
		fi
		idx=`/fabos/bin/expr ${idx} + 1`
	done
}

# Function to list a list of marked core files only
listCoreFilesListMarked() {
	markedFiles=0
	idx=0
	while [ $idx -lt $coreFilesCount ]
	do
		if [  "${coreSelArry[${idx}]}" = "*" ]
		then
			printf "	%s\n" ${coreFileArry[$idx]}
			markedFiles=`/fabos/bin/expr ${markedFiles} + 1`
		fi
		idx=`/fabos/bin/expr ${idx} + 1`
	done
}

# Function to remove ALL core files
rmCoreFilesListAll() {
	idx=0
	while [ $idx -lt $coreFilesCount ]
	do
		printf " removing core files under: %s\n" ${coreFileArry[$idx]}
		/bin/rm -f ${coreFileArry[$idx]}/*
		idx=`/fabos/bin/expr ${idx} + 1`
	done
}

# Function to list ALL core files (without marks)
listCoreFilesListAll() {
	idx=0
	while [ $idx -lt $coreFilesCount ]
	do
		printf "	%s\n" ${coreFileArry[$idx]}
		idx=`/fabos/bin/expr ${idx} + 1`
	done
}

# Function to list a list of core files with marks
listCoreFilesList() {
	if [ $coreFilesCount -eq 0 ]
	then
		printf "No core files found!\n"
		exit
	fi
	printf "following %d directories contains core files:\n" $coreFilesCount
	idx=0
	while [ $idx -lt $coreFilesCount ]
	do
		printf "	[%s]%d: %s\n" "${coreSelArry[${idx}]}" $idx ${coreFileArry[$idx]}
		idx=`/fabos/bin/expr ${idx} + 1`
	done
}

# Function to get a list of core files
getCoreFileList() {
	core_files_list=`/usr/bin/find ${CORE_ROOT}${CORE_FILES_DIR} ${CORE_ROOT}${MNT_DIR}/${CORE_FILES_DIR} -name "*core*" -type f -print 2> /dev/null`
	if [ -z "${core_files_list}" ]
	then
		printf "No core files found!\n"
		exit
	fi
	tmpList=""
	for coreFile in ${core_files_list}
	do
		tmpList="${tmpList}  `/usr/bin/dirname ${coreFile}`"
	done
	core_files_list=`/bin/ls -d $tmpList | /bin/sort | /usr/bin/uniq`
	idx=0
	for coreFile in ${core_files_list}
	do
		coreFileArry[${idx}]="${coreFile}"
		coreSelArry[${idx}]=" "
		idx=`/fabos/bin/expr ${idx} + 1`
	done
	coreFilesCount=$idx
}


# main starts here
	argHostName=""
	argUserName=""
	argPassWord=""
	argRemoteDir=""

# get the command options, if any
	while getopts h:u:p:d: c
	do
		case $c in
		h) argHostName=$OPTARG;;
		u) argUserName=$OPTARG;;
		p) argPassWord=$OPTARG;;
		d) argRemoteDir=$OPTARG;;
		*)
			printf "usage:
	$0 [-h host-name] [-u user-name] [-p password] [-d remote-directory]\n"
		exit;;
		esac
	done

# get a snap shot of the available core files, if anything is created
# latter, we don't consider them at this run
getCoreFileList

# keep bugging the user untils we transfer successfully or
# he decides to quit
while [ 1 ]
do
listCoreFilesList
printf "
	Welcome to core files management utility.

	Menu
	1(or R): Remove all core files
	2(or F): FTP all core files
	3(or r): Remove marked files
	4(or f): FTP marked files
	5(or m): Mark Files for action
	6(or u): Un Mark Files for action
	9(or e): Exit

Your choice: "
read choice
case $choice in
5|m)
	printf "Enter File Number to mark: "
	read choice
	if [ -z "$choice" ]
	then
		choice=invalid
	fi
	nchoice=`echo $choice | /usr/bin/tr -cd "[0-9]\n"`
	if [ "$nchoice" != "$choice" ]
	then
		printf "Please enter a number\n"
	else
		if [ $nchoice -lt 0 -o $nchoice -ge $coreFilesCount ]
		then
			printf "Please enter a value between 0 and `/fabos/bin/expr $coreFilesCount - 1`\n"
		else
			coreSelArry[$nchoice]="*"
		fi
	fi
	;;
6|u)
	printf "Enter File Number to un mark: "
	read choice
	if [ -z "$choice" ]
	then
		choice=invalid
	fi
	nchoice=`echo $choice | /usr/bin/tr -cd "[0-9]\n"`
	if [ "$nchoice" != "$choice" ]
	then
		printf "Please enter a number\n"
	else
		if [ $nchoice -lt 0 -o $nchoice -ge $coreFilesCount ]
		then
			printf "Please enter a value between 0 and `/fabos/bin/expr $coreFilesCount - 1`\n"
		else
			coreSelArry[$nchoice]=" "
		fi
	fi
	;;
1|R)
	# Remove ALL core files

	# let us give them a list of ALL core files
	listCoreFilesListAll
	printf "
	You have opted to remove ALL core files:-\n"
	printf "Please confirm (Y/[N]): "
	read choice
	if [ X${choice} = Xy -o X${choice} = XY -o X${choice} = \
	  Xyes -o X${choice} = XYES ] 
then
	printf "Removing files....\n"
	rmCoreFilesListAll
	printf "Done!\n"
	exit
else
	printf "core files NOT removed\n"
fi
	exit
	;;
3|r)
	# Remove Marked files only

	# let us give them a list files what they marked
	listCoreFilesListMarked
	if [ $markedFiles -gt 0 ]
	then
		printf "
		You have opted to remove core files under these directories:-\n"
		printf "Please confirm (Y/[N]): "
		read choice
		if [ X${choice} = Xy -o X${choice} = XY -o X${choice} = \
	  	Xyes -o X${choice} = XYES ] 
		then
			printf "Removing files....\n"
			rmCoreFilesListMarked
			printf "Done!\n"
			exit
		else
			printf "core files NOT removed\n"
		fi
	else
		printf "No core files marked\n"
	fi
	;;
2|F)
	# ftp ALL core files
	listCoreFilesListAll
	printf " You have opted to FTP these core files:-\n"
	printf "Please confirm (Y/[N]): "
	read choice
	if [ X${choice} = Xy -o X${choice} = XY -o X${choice} = \
  	Xyes -o X${choice} = XYES ] 
	then
		ftpCoreFilesListAll
	else
		printf "core files NOT FTP'd\n"
	fi
	;;
4|f)
	# let us give them a list files what they marked
	listCoreFilesListMarked
	if [ $markedFiles -gt 0 ]
	then
		printf "
		You have opted to FTP these core files:-\n"
		printf "Please confirm (Y/[N]): "
		read choice
		if [ X${choice} = Xy -o X${choice} = XY -o X${choice} = \
	  	Xyes -o X${choice} = XYES ] 
		then
			ftpCoreFilesListMarked
		else
			printf "core files NOT FTP'd\n"
		fi
	else
		printf "No core files marked\n"
	fi
	;;
9|e)
	exit
	;;
*)
	printf "Invalid choice\n"
	;;
esac
printf "Enter CR To Continue.... "
read choice
printf "\n\n\n\n\n\n"
done # main while
