#! /bin/sh

# Usage: echo_n-sh string [...]

# Some "echo" commands builtin to shells and on older systems don't grok
# the -n flag, so look for version that does or use printf(1) if available

# XXX it would probably be easier to just replace this with our own
# implementation of "echo"

# Copyright (c) 2003-2011
# Distributed Systems Software.  All rights reserved.
# See the file LICENSE for redistribution information.
#
# $Id: echo_n-sh 2528 2011-09-23 21:54:05Z brachman $

do_printf() {
  prog="$1"

  first=1
  shift
  for i
  do
    if test "${first}" -eq 1
    then
      $prog "%s" "$i"
      first=0
    else
      $prog " %s" "$i"
    fi
  done

}

honours_nflag() {
  prog="$1"

  a=`$prog -n foo; $prog -n baz`
  if test "$a" = "foobaz"
  then
    return 0
  else
    return 1
  fi
}

# The hunt begins...
# The first choice is the portable, POSIX, printf(1)
if test -f "/usr/bin/printf" -a -x "/usr/bin/printf"
then
  do_printf "/usr/bin/printf" "$*"
  exit $?
fi

if test -f "/bin/printf" -a -x "/bin/printf"
then
  do_printf "/bin/printf" "$*"
  exit $?
fi

# On some Solaris platforms, the envar SYSV3 must be set to
# get the -n flag to work
SYSV3=1; export SYSV3

# The second choice is to use a builtin echo command
if honours_nflag echo
then
  echo -n "$*"
  exit 0
fi

# Now try a separate echo command in one of the standard places
if honours_nflag /bin/echo
then
  /bin/echo -n "$*"
  exit 0
fi

if honours_nflag /usr/ucb/echo
then
  /usr/ucb/echo -n "$*"
  exit 0
fi

if honours_nflag /usr/bin/echo
then
  /usr/bin/echo -n "$*"
  exit 0
fi

# SOL... someone should notice the screwy output and fix us
echo -n "$*"

