#!/bin/bash
#===========================================================================
# Copyright ©2004 - Copyright J. S. Gilstrap - johng@mylinuxisp.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Any knowledge gained by viewing and/or using this software may NOT be used
# to file for any 'submarine' patents on applicable algorithmns and/or ideas
# or any other method that would undermine the free use and/or distribution
# thereof. By viewing and/or using this software you agree to these terms.
# If you cannot agree to this then do NOT view and/or use this software.
#
# :set syntax=sh

# Print help and exit if ran directy.
if [ ${0##*/} = get_string ]
then
	echo "This is a function to prompt user for input."
	echo "It is to be used within a bash shell script."
	exit 1
fi

getString()
	{
	local string cnt total char valid skip
	[ -n "$1" ] && eval STRING=\$$1

	while :
	do
		printf "% ${MAXLNTH}b" "$PROMPT"

		if [ -n "$STRING" ]
		then
			printf " <${STRING}> : "
			read string

			if [ -n "$string" ]
			then
				if [ "$string" = clear ]
				then
					unset string

				elif [ "$string" != help ]
				then
					STRING="$string"
				fi
			fi
		else
			printf " <> : "
			read STRING
		fi

		  if [ "$STRING" = help ] 
		then
			unset STRING
			printf "$HELP"

		elif [ "$string" = help ] 
		then
			printf "$HELP"

		elif [ -z "$STRING" ]
		then
			if [ "$REQUIRED" = Y ]
            then
				printf "\n    [1mRequired. You Must Enter Data.[0m\n\n"
			else
				break 1
			fi

		elif [ "$VALIDATE" = Y ]
		then
			LENGTH="${#STRING}"
			$preRules	# Call Pre Loop Function

			if [ $LENGTH -ne 0 ]
			then
				COUNT=0
				total=0
				unset valid

				while [ $COUNT -ne $LENGTH ]
				do
					cnt=$COUNT
					let COUNT++
					$loopRules	# Call Loop Rules Function
					char=${STRING:$cnt:1}

					case "$char" in
						${MATCH}) valid="${valid}[32mY"
						          let total++ ;;
						       *) valid="${valid}[31mn" ;;
					esac
				done

				if [ $total -eq $LENGTH ]
				then
					break 1
				else
					printf "\n    [1mCorrect : ${valid}[0m\n"
					printf   "    [1mEntered :[0m ${STRING}\n\n"
					printf   "    [1mOnly $total of the $LENGTH Character(s) are Valid.[0m\n\n"	
					printf       "${ERR_MSG}\n\n"
					unset STRING

					printf "    Press <c> to Continue or <s> to Skip. : "
					read skip

					if [ "$skip" = s -a "$REQUIRED" != y ]
					then
						printf "\n    [1mInvalid Entry. String NOT Set![0m\n"
						sleep 1.5
						break 1

					elif [ "$skip" != c ]
					then
						printf "\n    [1mPlease use 'c' for Continue next time![0m\n"
					fi

					echo " "
				fi
			fi
		else
			break 1
		fi
	done

	[ -n "$1" ] && eval $1=$STRING
	}
