#!/bin/sh

rm -f test* okar okcc okcclink okcpp okcpplink okcompilers oklibs

exec < compilers
exec > okcompilers

foundokcc=0
while read c cpp options
do
  echo "=== `date` === checking $c $cpp $options" >&2
  rm -f test*
  (
    echo "#!/bin/sh"
    echo 'PATH="'"$PATH"'"'
    echo 'export PATH'
    echo "$c" "$options" '"$@"'
  ) > test-okcc
  chmod 755 test-okcc
  (
    echo "#!/bin/sh"
    echo 'PATH="'"$PATH"'"'
    echo 'export PATH'
    echo "$cpp" "$options" '"$@"'
  ) > test-okcpp
  chmod 755 test-okcpp
  cat lib.c main.c > test.c || continue
  ./test-okcc -o test test.c || continue
  ./test || continue
  cat lib.c main.c > test.cpp || continue
  ./test-okcpp -o test test.cpp || continue
  ./test || continue
  cp main.c test1.c || continue
  cp lib.c test2.c || continue
  ./test-okcc -c test1.c || continue
  ./test-okcc -c test2.c || continue
  ./test-okcc -o test1 test1.o test2.o || continue
  ./test1 || continue
  cp main.c test3.cpp || continue
  cp lib.c test4.cpp || continue
  ./test-okcpp -c test3.cpp || continue
  ./test-okcpp -c test4.cpp || continue
  ./test-okcpp -o test3 test3.o test4.o || continue
  ./test3 || continue
  case $foundokcc in
    0)
      cp -p test-okcc okcc || continue
      cp -p test-okcpp okcpp || continue
      echo "=== `date` === success: $c $cpp $options is ok" >&2
      echo "#!/bin/sh"
      echo "echo '"$c $cpp $options"'"
      foundokcc=1
      syslibs=""
      for i in -lm -lnsl -lsocket
      do
        echo "=== `date` === checking $i" >&2
        (
          echo "#!/bin/sh"
          echo 'PATH="'"$PATH"'"'
          echo 'export PATH'
          echo "$c" "$options" '"$@"' "$i" "$syslibs"
        ) > test-okcclink
	chmod 755 test-okcclink
        (
          echo "#!/bin/sh"
          echo 'PATH="'"$PATH"'"'
          echo 'export PATH'
          echo "$cpp" "$options" '"$@"' "$i" "$syslibs"
        ) > test-okcpplink
	chmod 755 test-okcpplink
	cat lib.c main.c > test.c || continue
	./test-okcclink -o test test.c $i $syslibs || continue
	./test || continue
	cat lib.c main.c > test.cpp || continue
	./test-okcpplink -o test test.cpp $i $syslibs || continue
	./test || continue
	syslibs="$i $syslibs"
	cp -p test-okcclink okcclink
	cp -p test-okcpplink okcpplink
	(
	  echo '#!/bin/sh'
	  echo 'echo "'"$syslibs"'"'
	) > oklibs
	chmod 755 oklibs
      done
    ;;
    1)
      cp main.c test5.c || continue
      cp lib.c test6.c || continue
      ./okcc -c test5.c || continue
      ./test-okcc -c test6.c || continue
      ./test-okcc -o test5 test5.o test6.o || continue
      ./test5 || continue
      ./test-okcc -c test5.c || continue
      ./okcc -c test6.c || continue
      ./test-okcc -o test5 test5.o test6.o || continue
      ./test5 || continue
      cp main.c test7.cpp || continue
      cp lib.c test8.cpp || continue
      ./okcpp -c test7.cpp || continue
      ./test-okcpp -c test8.cpp || continue
      ./test-okcpp -o test7 test7.o test8.o || continue
      ./test7 || continue
      ./test-okcpp -c test7.cpp || continue
      ./okcpp -c test8.cpp || continue
      ./test-okcpp -o test7 test7.o test8.o || continue
      ./test7 || continue
      echo "echo '$c $cpp $options'"
      echo "=== `date` === success: $c $cpp $options is ok" >&2
    ;;
  esac
done
exec >/dev/null
chmod 755 okcompilers

case $foundokcc in
  0)
    echo "=== `date` === giving up; no compilers work" >&2
    exit 111
  ;;
esac

exec <archivers
while read a
do
  echo "=== `date` === checking archiver $a" >&2
  (
    echo "#!/bin/sh"
    echo 'PATH="'"$PATH"'"'
    echo 'export PATH'
    echo "$a" '"$@"'
  ) > test-okar
  chmod 755 test-okar
  rm -f test10.a
  cp main.c test9.cpp || continue
  cp lib.c test10.cpp || continue
  ./okcpp -c test10.cpp || continue
  ./test-okar cr test10.a test10.o || continue
  ranlib test10.a || echo "=== `date` === no ranlib; continuing anyway" >&2
  ./okcpp -o test9 test9.cpp test10.a || continue
  ./test9 || continue
  cp -p test-okar okar || continue
  echo "=== `date` === success: archiver $a is ok" >&2
  foundokar=1
  break
done

case $foundokar in
  0)
    echo "=== `date` === giving up; no archivers work" >&2
    exit 111
  ;;
esac

rm -f test*

mkdir -p ../bin
cp -p okcompilers ../bin/okcompilers
cp -p oklibs ../bin/oklibs
cp -p okar ../bin/ar

echo "=== `date` === done" >&2
