

#!/bin/bash

show_usage() {
echo
echo ${0##/*}" Usage:"
echo "  Instructions for usage go here"
# this prints the program name -example syntax follows
echo "${0##/} -fFIELDS -dSEP STRING"
echo
exit
}

case $1 in
	""|"-h"|"--help") show_usage ;;
esac



###
# Minimum number of arguments needed by this program
MINARGS=3

# count the number of arguments given
COUNT=0
for ARGV in "$@" ; do (( COUNT++ )) ; done
ARGC=$COUNT
# [[ $DEBUG ]] && echo "ARGC=$COUNT" ##debug

# check to make sure enough arguments were given
if [[ $ARGC -lt $MINARGS ]] ; then
 [[ $DEBUG ]] && echo "Too few arguments given (Minimum:$MINARGS)"
 show_usage
fi

# Assign the variables to ARGV[?]
COUNT=0
for ARGV in "$@" ; do
(( COUNT++ )) ;
 declare ARG_${COUNT}="$ARGV"
# [[ $DEBUG ]] && echo $ARG_$COUNT ##debug
done

# [[ $DEBUG ]] && echo "$ARG_1 $ARG_2 $ARG_3" ##debug

###
# process_arguments
# could use for ARGV_?  in ARGC
# process command-line arguments
for WORD in "$@" ; do
	case $WORD in
		-*)  true ;
			case $WORD in
				-f=?) [[ $DEBUG ]] && echo "Short single FIELD Option using '='"
					FIELDS=${WORD##*=}
					[[ $DEBUG ]] && echo FIELDS=$FIELDS
					shift
				;;
				-f=*|--fields=*) [[ $DEBUG ]] && echo "Short range FIELD Option using '='"
					FIELDS=${WORD##*=}
					[[ $DEBUG ]] && echo FIELDS=$FIELDS
					shift
				;;
				-f?) [[ $DEBUG ]] && echo "Short single FIELD Option"
					FIELDS=${WORD##*f}
					[[ $DEBUG ]] && echo FIELDS=$FIELDS
					shift
				;;
				-f) [[ $DEBUG ]] && echo "Short split FIELD Option"
					if [[ ${2:0:1} != "-" ]] ; then
					 FIELDS=$2
					 [[ $DEBUG ]] && echo FIELDS=$FIELDS
					 shift 2
					else
					 echo "Missing argument"
					 show_usage
					fi
				;;
				-f*) [[ $DEBUG ]] && echo "Short FIELD Option range"
					FIELDS=${WORD##*f}
					[[ $DEBUG ]] && echo FIELDS=$FIELDS
					shift
				;;
				-d*) [[ $DEBUG ]] && echo "Short-short DEL Option"
					SEP=${WORD##*d}
					[[ $DEBUG ]] && echo SEP=$SEP
					shift
				;;
			esac
		;;
	esac
done




###
# function _length counts the characters in a string and
# echo the result. Requires PARSESTRING
function _length() { COUNT=0
while [[ $PARSESTRING != "" ]] ; do
	# get the first remaining character
	FC=${PARSESTRING:0:1}
	# cut the first character
	PARSESTRING="${PARSESTRING#${FC}*}"
	(( COUNT++ ))
done
echo $COUNT
}
# example usage
#PARSESTRING=$STRING
#[[ $DEBUG ]] && echo STRINGLEN=$(_length $PARSESTRING)


###
# function _freq counts the number of matches
# of PATTERN in PARSESTRING and returns FREQ
function _freq() { FREQ=0
! [[ $PATTERN ]] && PATTERN=$1
! [[ $PARSESTRING ]] && PARSESTRING=$2
while [[ $PARSESTRING != "" ]] ; do
	case $PARSESTRING in
		*$PATTERN*) FREQ=$(( FREQ + 1 )) ;
			PARSESTRING=${PARSESTRING#*${PATTERN}} ;;
		*) PARSESTRING="" ;;
	esac
done
echo $FREQ
}
# example usage
#PARSESTRING=$STRING
#PATTERN=$SEP
#[[ $DEBUG ]] && echo "Found $(_freq) matches."
# _freq $SEP $STRING


###
# _print_field is UNUSED
# print the contents of a single FIELD before the SEP in PARSESTRING
# Requires/defaults: FIELD=$1 PATTERN=$2 PARSESTRING=$3
function _print_field() { FREQ=0
! [[ $FIELD ]] && FIELD=$1
! [[ $PATTERN ]] && PATTERN=$2
! [[ $PARSESTRING ]] && PARSESTRING=$3
while [[ $PARSESTRING != "" ]] ; do
	case $PARSESTRING in
		*$PATTERN*) FREQ=$(( FREQ + 1 )) ;
			# echo FIELD_${FREQ}=${PARSESTRING%%${PATTERN}*}
			# declare FIELD_${FREQ}=${PARSESTRING%%${PATTERN}*}
			# [[ $DEBUG ]] && echo "FIELD_$FREQ=$FIELD_FREQ"
			if [[ $FIELD = $FREQ ]] ; then
				 # echo FIELD_${FREQ}=${PARSESTRING%%${PATTERN}*}
				 # right_of_first SEP
				 STUB=${PARSESTRING#*${PATTERN}}
				 # left_of_first SEP
				 echo ${STUB%%${PATTERN}*}
			fi
			PARSESTRING=${PARSESTRING#*${PATTERN}} 
			#echo $PARSESTRING
			;;
		*) PARSESTRING="" ;;
	esac
done
}
# example
# _print_field $FIELD $SEP $STRING