#!/bin/csh

cat - <<EOF
I will now perform various checks to see if you have the appropriate software
installed. To override these checks, type
make force
EOF

if ( "x" == "x"$1) then
  echo No BINDIR specified!
  exit 1
endif

if ( "x" == "x"$2) then
  echo No CXX specified, assuming g++ !
  set CXX=g++
else
  set CXX="$2"
endif

if ( ! -d $1 ) then
  echo the BINDIR variable of the makefile indicates that
  echo executables should be put into $1
  echo But $1 does not exists.
  echo Do you want me to create it\? \(y/n\)
  echo $< | grep -q -i y
  if ( $status == 0 ) then
     mkdir $1
  else
    echo Please edit the makefile so that BINDIR points to an existing directory.
    exit 1
  endif
endif


${CXX} --version >& /dev/null
if ( $status != 0 ) then
  echo You don\'t have ${CXX}. You can download g++ from http://www.gnu.org/
  exit 1
endif

make --version x >& /dev/null
if ( $status != 0 ) then
  foreach makename (gmake gnumake)
    $makename --version x >& /dev/null
    if ( $status == 0 ) then
      echo It looks like GNU make on your system is called $makename
      echo Please use $makename
      exit 1
    endif
  end
  echo You do not have gnu make. Please download it from http://www.gnu.org/
  exit 1
endif

perl -v >& /dev/null
if ( $status != 0 ) then
  echo You do not have perl. Please download it from http://www.perl.com/
  exit 1
endif

#tail +2 $0 >& /dev/null
#if ( $status != 0 ) then
#  tail -n +2 $0 >& /dev/null
#  if ( $status == 0 ) then
#    echo Patching tail command...
#    ./patchtail
#  endif
#endif

cd src

echo Testing compiler features...
echo -n 'PATCHCXXFLAGS=' >! makefileflags
foreach feature (  )
  echo -n " " $feature ": "
  ${CXX} test${feature}.c++ -o test${feature} >&! tmp.out
  if (`cat tmp.out | wc -l` != 0) then
    echo -n ' -ffriend-injection' >> makefileflags
    echo "patched"
  else
    echo "ok"
  endif
  rm -f tmp.out
end

echo "#\!/bin/sh" >! ./patchlang
echo -n "cat -" >> ./patchlang
echo -n " | ./patchconst " >> ./patchlang
chmod +x ./patchlang
foreach feature ( friend doth str2s template uT static )
  echo -n " " $feature ": "
  ${CXX} test${feature}.c++ -o test${feature} >&! tmp.out
  if (`cat tmp.out | wc -l` != 0) then
    echo -n ' | ' ./patch${feature} >> ./patchlang
    echo "patched"
  else
    echo "ok"
  endif
  rm -f tmp.out
end
echo "" >> ./patchlang

cd ..

echo Tests successfully passed.