#!/bin/sh
#
# initterm: reset terminal and set up sensible stty settings

sttycmd='stty'

case "${TERM}" in
  nd120 | nd120-*)
    # nd120 sets CapsLock on a reset
    # (we should really edit the termcap instead)
    ;;
  *)
    if . _findprog && _findprog reset; then
      # We need to reset the terminal, but the reset
      # program is a bit of a disaster.  There's no
      # good alternative to using it, so we just have
      # to kludge around the brokenness.
      #
      # use -Q (quiet) option if supported
      reset -Q || reset
    fi
    ;;
esac

# reset has been observed to bugger this up
sttycmd="${sttycmd} clocal"

# disable flow control
sttycmd="${sttycmd} stop '^-' start '^-' -ixon"

# these have been observed to intermittently get clobbered on some systems
sttycmd="${sttycmd} susp '^Z' eof '^D'"

# tabs dont work on many terminals; dont try to be clever about it
sttycmd="${sttycmd} -tabs"

# get erase right
case "${TERM}" in
  linux* | vt[0-9]* | nd120 | nd120-*)
    sttycmd="${sttycmd} erase '^?'"
    e2sttycmd="stty erase2 '^H'"
    ;;
  *)
    sttycmd="${sttycmd} erase '^H'"
    e2sttycmd="stty erase2 '^?'"
    ;;
esac

# do stty settings
stty sane
eval "${sttycmd}"
eval "${e2sttycmd}" 2>/dev/null
