#
# Copyright (c) 1999-2001 Easysoft Ltd. All rights reserved.
#
# This script checks the neccessary tools are available on this machine.
#
# Calling format:
#
# install_check_tools
#
# returns:
#
# 0 = success
# 1 = error, tool not found
# 2 = environment variable not defined
#
# $Id: install_check_tools,v 1.7 2003/09/12 13:09:42 martin Exp $
#
# $Log: install_check_tools,v $
# Revision 1.7  2003/09/12 13:09:42  martin
# bug in last checkin.
#
# Revision 1.6  2003/09/12 13:08:20  martin
# Add tar.
#
# Revision 1.5  2001/04/26 13:28:55  martin
# Make sure head works.
# Use our tee if the system one does not work.
#
# Revision 1.4  2001/03/22 11:09:21  martin
# If tee missing the script should exit.
#
# Revision 1.3  2001/02/20 14:46:59  martin
# Add check for tee.
#
# Revision 1.2  2000/10/30 15:27:44  martin
# Fix bug ID 989 - OOB server install fails to get inetd to
# 	reread configuration.
#
# Revision 1.1  2000/03/17 16:36:58  martin
# Initial revision
#
#
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
#
# Check we have the tools we need.
# ================================
#
$ECHO
$ECHO "Checking you have the basic tools for the install..."
$ECHO
if ( pwd 1>/dev/null 2>/dev/null )
then
  $ECHO "pwd \c"
else
  $ECHO "Cannot install without \"pwd\""
fi
if ( echo "test" | grep test 1>/dev/null 2>/dev/null )
then
  $ECHO "grep \c"
else
  $ECHO "Cannot install without \"grep\""
  exit 1
fi
if ( $ECHO "test" | awk '{print $1}' 1>/dev/null 2>/dev/null )
then
  $ECHO "awk \c"
else
  $ECHO "Cannot install without \"awk\""
  exit 1
fi
if ( $ECHO "test" | cut -b1-2 1>/dev/null 2>/dev/null )
then
  $ECHO "cut \c"
else
  $ECHO "Cannot install without \"cut\""
  exit 1
fi
if ( ps 1>/dev/null 2>/dev/null )
then
  $ECHO "ps \c"
else
  $ECHO "Can't find a working \"ps\" command"
  exit 1
fi
if ( $ECHO "test" | sed 's/test/fred/g' 1>/dev/null 2>/dev/null )
then
  $ECHO "sed \c"
else
  $ECHO "Cannot install without \"sed\""
  exit 1
fi
if ( $ECHO "test" | cat 1>/dev/null 2>/dev/null )
then
  $ECHO "cat \c"
else
  $ECHO "Cannot install without \"cat\""
  exit 1
fi
if ( $ECHO "test" | wc -w 1>/dev/null 2>/dev/null )
then
  $ECHO "wc \c"
else
  $ECHO "Cannot install without \"wc\""
  exit 1
fi
if ( $ECHO "test" | head -1 1>/dev/null 2>/dev/null )
then
   out=`$ECHO "test" | head -1`
  if [ "$out" != "test" ];then
    $ECHO "head does not appear to work properly"
    exit 1
  fi
  $ECHO "head \c"
else
  $ECHO "Cannot install without \"head\""
  exit 1
fi
if ( uname -s 1>/dev/null 2>/dev/null )
then
  $ECHO "uname \c"
else
  $ECHO "Cannot install without \"uname\""
  exit 1
fi
if ( find . -print 1>/dev/null 2>/dev/null )
then
  $ECHO "find \c"
else
  $ECHO "Cannot install without \"find\""
  exit 1
fi
if ( id 1>/dev/null 2>/dev/null )
then
  $ECHO "id \c"
else
  $ECHO "Cannot install without \"id\""
  exit 1
fi
if ( $ECHO "hello" | tee ./teefile 1>/dev/null 2>/dev/null )
then 
  $ECHO "tee \c"
  $ECHO "hello" | tee -a ./teefile 1>/dev/null 2>/dev/null
  if [ $? -ne 0 ];then
    $ECHO "\ntee does not appear to support the -a (append) option"
    exit 1
  fi
  rm ./teefile
else
  #
  # Some distributions come with a simple replacement tee - just in case
  # All distributions should but they have not all caught up yet.
  #
  if [ ! -x ./tee ];then
    $ECHO "Cannot install without \"tee\""
    exit 1
  else
    # tell caller to use local tee
    touch ./usetee
  fi
fi
$ECHO "tar \c"
tar cf test.tar $0
if [ $? -ne 0 ];then
  $ECHO "\nTest tar failed - may impact install"
fi
if [ ! -r test.tar ];then
  $ECHO "\nTest tar file not created - may impact install"
fi
rm -f test.tar
exit 0