#!/bin/bash
# runme.unix.sh version 2007-02-05 pour U**X
# patches | commentaires > whygee@f-cpu.org

cat README.txt
cat README.unix

#essaie de créer une chaine spécifique à l'hôte sans dépendre
#des variables d'environnement, en espérant que sed et cut fonctionnent bien
DIRNAME=sortie-cc.$( uname -a | sed 's/ /_/g; s/\//-/g; s/;\|#\|://g' | cut -c -40 )
rm -rf sortie-cc.*
mkdir -p $DIRNAME

{
  uname -a > $DIRNAME/uname.out
  cc -v > $DIRNAME/cc-v.out 2>&1
  echo '#include <stdio.h>' | cc -E - > $DIRNAME/cc-E.out &&

  if [ -r machine.h ] ; then
    echo "machine.h trouvé : pas d'autoconfiguration"
  else
    echo "machine.h absent : on lance l'autoconfiguration"
    cc machine.c -o machine &&
    ./machine $(date) $(uname -a) | tee machine.h | grep error
    rm machine
  fi
  cp machine.h $DIRNAME &&

  cc -DSTANDALONE_TEST_LFSR4 -Dinline='' lfsr4.c -o lfsr4 &&
  if [ "$( ./lfsr4 | tee $DIRNAME/lfsr4.out )" != \
      '8705035aac711c5cbe5a513f6f2f97cfb7a9b8646ef9765738b99ba809f5' ] ; then
    echo Probleme au niveau du LFSR !
  else
    echo "LFSR4 OK"
    rm lfsr4

    # genere le dump des definitions
    cc -E test_crc16-64.c -Dinline='' > $DIRNAME/out.h &&

    # genere le fichier assembleur
    cc -S test_crc16-64.c -Dinline='' -o $DIRNAME/out.s &&

    # obtiens une signature
    cc test_crc16-64.c -o out -Dinline='' -DPRINTLFSR -DPRINTCRC -DTESTS_TABLE -DTAILLE_CST=30 -DPRINTCRC2 &&
    ./out > $DIRNAME/testcrc.out &&
    {
      if diff -s testcrc.ref.BE $DIRNAME/testcrc.out ; then
        echo "---> Resultat conforme Big Endian"
      else
        if diff -s testcrc.ref.LE $DIRNAME/testcrc.out ; then
          echo "---> Resultat conforme Little Endian"
        else
          echo "Probleme : resultat non conforme a la reference"
        fi
      fi
    }

    # lance le test muet sur 5000 iterations
    cc test_crc16-64.c -o out  -Dinline='' -DTAILLE_CST=5000 &&
    ./out &&
    echo "Test termine avec succes :-)"
  fi

  echo "archivage des fichiers de sortie dans $DIRNAME.tar"
  cp runme.unix.sh $DIRNAME
  tar -cf $DIRNAME.tar $DIRNAME
  gzip $DIRNAME.tar
  rm -f out
} | tee $DIRNAME/messages.out 2>&1
