#!/usr/bin/ksh93
################################################################
#### Program: fold_k93
#### 
#### Description: Emulation of the Unix "fold" command
#### 
#### Author: Dana French (dfrench@mtxia.com)
####         Copyright 2004
#### 
#### Date: 07/28/2004
#### 
################################################################
function fold_k93 {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset SPACES="${FALSE}"
  typeset WIDTH="80"
  typeset WIDCNT="0"
  typeset VERYVERB="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset FNAME

  while getopts ":sw:vV" OPTION
  do
    case "${OPTION}" in
        's') SPACES="${TRUE}";;
        'w') WIDTH="${OPTARG}";;
        'v') VERBOSE="${TRUE}";;
        'V') VERYVERB="${TRUE}";;
        '?') print "Syntax: fold_k93 [FILE]..." && exit 1 ;;
    esac
  done
 
  shift $(( ${OPTIND} - 1 ))

  typeset STDIN="${1:+${FALSE}}"
  STDIN="${STDIN:-${TRUE}}"

#### Read in the data either from STDIN or one or more files
  if (( STDIN == TRUE ))
  then
      while IFS="" read -r -- LINE
      do
        if [[ "_${LINE}" = "_" ]]
        then
          (( WIDCNT > 0 )) && print
          print
          WIDCNT="0"
        else
          if (( SPACES == TRUE ))
          then
              foldOnSpace_k93 WIDCNT "${LINE}" "${WIDTH}"
          else
              foldOnWidth_k93 WIDCNT "${LINE}" "${WIDTH}"
          fi
        fi
      done
  else
      for FNAME in "${@}"
      do
        if [[ -e "${FNAME}" ]]
        then
            exec 0<"${FNAME}"

            while IFS="" read -r -- LINE
            do
              if [[ "_${LINE}" = "_" ]]
              then
                (( WIDCNT > 0 )) && print
                print
                WIDCNT="0"
              else
                if (( SPACES == TRUE ))
                then
                    foldOnSpace_k93 WIDCNT "${LINE}" "${WIDTH}"
                else
                    foldOnWidth_k93 WIDCNT "${LINE}" "${WIDTH}"
                fi
              fi
            done
            exec 0<&-
        else
            print -u 2 "${0##*/}: ${FNAME}: No such file or directory"
            return 1
        fi
      done
  fi

  return 0
}
################################################################
function foldOnSpace_k93 {
  nameref WIDCNT="${1}"
  typeset LINE="${2}"
  typeset WIDTH="${3}"

  for FIT in ${LINE}
  do

    if (( ( WIDCNT + ${#FIT} + 1 ) < WIDTH ))
    then
      (( WIDCNT != 0 )) && print -r -n -- " "
      print -r -n -- "${FIT}"
      (( WIDCNT = WIDCNT + ${#FIT} + 1 ))

    elif (( ( WIDCNT + ${#FIT} + 1 ) == WIDTH ))
    then
      (( WIDCNT != 0 )) && print -r -n -- " "
      print -r -- "${FIT}"
      (( WIDCNT = 0 ))

    else
      print -r -n -- $'\n'"${FIT}"
      (( WIDCNT = ${#FIT} ))
    fi

  done
}
################################################################
function foldOnWidth_k93 {
  nameref WIDCNT="${1}"
  typeset LINE="${2}"
  typeset WIDTH="${3}"

  for FIT in ${LINE}
  do

    if (( ( WIDCNT + ${#FIT} + 1 ) < WIDTH ))
    then
      (( WIDCNT != 0 )) && print -r -n -- " " && (( WIDCNT++ ))
      print -r -n -- "${FIT}"
      (( WIDCNT = WIDCNT + ${#FIT} ))

    elif (( ( WIDCNT + ${#FIT} + 1 ) == WIDTH ))
    then
      (( WIDCNT != 0 )) && print -r -n -- " "
      print -r -- "${FIT}"
      (( WIDCNT = 0 ))

    else
      (( WIDCNT != 0 )) && print -r -n -- " " && (( WIDCNT++ ))
      (( PART = WIDTH - WIDCNT + 1))
      print -r -- "${FIT:0:PART}"
      print -r -n -- "${FIT:PART}"
      (( WIDCNT = ${#FIT} - PART ))
    fi

  done
}
################################################################

fold_k93 "${@}"