#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
# $Id: install_check_tools 24 2005-02-11 09:08:42Z martin $
#
# 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
#
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