#!/bin/ksh

# MissingEndQuote.ksh
# Fri Mar 20 15:19:44 PST 2009
# Chieh Cheng
# http://www.CynosureX.com/

# GNU General Public License (GPL) Version 2, June 1991

  scriptName=`basename "$0"`

  checkQuote ()
  {
    quoteCh="$1"
    file="$2"

    grep -n "${quoteCh}" "${file}" |
    while read line
    do
      num=`countQuotesOnLine "${quoteCh}" "${line}"`
      result=`IsOdd.sh ${num}`

      if [ ${result} -eq 1 ]
      then
        echo "${line}"
      fi
    done
  }

  countQuotesOnLine ()
  {
    quoteCh="$1"
    line="$2"

    pos=0
    length=${#line}
    count=0

    while [ ${pos} -lt ${length} ]
    do
      next=`expr ${pos} + 1`
      ch=`java SubString "${line}" ${pos} ${next}`
      pos=${next}

      if [ "${ch}" = "${quoteCh}" ]
      then
        count=`expr ${count} + 1`
      fi
    done

    echo "${count}"
  }

  usage ()
  {
    echo "  Usage:   ${scriptName} \"file\""
  }

  if [ $# -eq 1 ]
  then
    file="$1"

    if [ -f "${file}" ]
    then
      checkQuote "'" "${file}"
      checkQuote "\"" "${file}"
      checkQuote "\`" "${file}"
    else
      echo "${file} not found!"
    fi
  else
    usage
  fi
