#!/bin/sh
# runme.sh version 2007-02-05  version gcc/gnu/linux
# patches | commentaires > whygee@f-cpu.org

cat README.txt

#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.$( uname -a | sed 's/ /_/g; s/\//-/g; s/;\|#\|://g' | cut -c -40 )
rm -rf sortie.*
mkdir -p $DIRNAME

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

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

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

    if [ -e /proc/cpuinfo ] ; then
      echo "Affinage du type de processeur"
      cat /proc/cpuinfo > $DIRNAME/cpuinfo.out

      # options Alpha 21264 :
      if grep -l ' EV6' /proc/cpuinfo ; then
        OPTIONS="$OPTIONS -mcpu=ev6"
      else
        # options x86 :
        if grep -l ' mmx ' /proc/cpuinfo ; then
          OPTIONS="$OPTIONS -momit-leaf-frame-pointer -mmmx"
          if grep -l ' sse ' /proc/cpuinfo ; then
            OPTIONS="$OPTIONS -msse -march=pentium3"
          fi
        fi
      fi
    fi
    echo options : $OPTIONS

    # genere le dump des definitions
    gcc -E -dM -Os -W -Wall $OPTIONS test_crc16-64.c | sort > $DIRNAME/out.h &&

    # genere le fichier assembleur
    gcc -S -Os -W -Wall $OPTIONS test_crc16-64.c -o $DIRNAME/out.s &&

    # obtiens une signature
    gcc -Os -W -Wall $OPTIONS test_crc16-64.c -o out \
        -DPRINTLFSR -DPRINTCRC -DTESTS_TABLE -DTAILLE_CST=30 -DPRINTCRC2 &&
    ./out > $DIRNAME/testcrc.out &&
    {
      if diff -q -s testcrc.ref.BE $DIRNAME/testcrc.out ; then
        echo "---> Resultat conforme Big Endian"
      else
        if diff -q -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
    gcc -Os -W -Wall $OPTIONS test_crc16-64.c -o out -DTAILLE_CST=5000 &&
    ./out &&
    echo "Test termine avec succes :-)"
  fi

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