:

findMAKE()
{
if(test -x "`which gmake`") then
    MAKE=gmake
else
    MAKE=make
fi
} 


findMakeVar()
{  
cat >Makefile <<EOF
all:
	@echo \$($1)
EOF
$MAKE
}

defaultFlags()
{
echo UNIX `uname`
case `uname` in

Linux)
       CC=gcc
       CFLAGS="-g -fsigned-char -Wstrict-prototypes -Wimplicit -Wmissing-prototypes -std=gnu99"
       FC=gfortran
       FFLAGS="-fno-automatic"       
       RANLIB=ranlib
     ;;

OSF1)
       CC=cc
       CFLAGS="-g -std1 -ieee -std=gnu99"
       FC=f77
       FFLAGS="-O0 -fpe2" 
       RANLIB=ranlib
     ;;
SunOS)
       CC=cc
       CFLAGS="-g -std=gnu99"
       FC=f77
       FFLAGS=
       RANLIB=ranlib
     ;;
Darwin)
       CC=gcc   
       CFLAGS="-g -fsigned-char -std=gnu99 "
       FC=gfortran
       FFLAGS= 
       RANLIB="ranlib -c"
     ;;
CYGWIN*)
       CC=gcc
       CFLAGS="-g -fsigned-char -std=gnu99"
       FC=g77
       FFLAGS=-fno-automatic 
       RANLIB="ranlib"
     ;;


   *) CC=gcc
      CFLAGS="-g -std=gnu99"

      FC=f77
      lFort=
      RANLIB=ranlib
      echo Unknown Unix  
     ;;
esac

}

writeFlags()
{

echo "# C compiler
CC=\"$CC\"
CFLAGS=\"$CFLAGS\"

# Fortran compiler
FC=\"$FC\"
FFLAGS=\"$FFLAGS\"

# RANLIB 
RANLIB=\"$RANLIB\"

# MAKE 
MAKE=$MAKE        

export CC CFLAGS FC FFLAGS RANLIB  MAKE
" > ../FlagsForSh    
}

#MAKE
writeFlagsForMake()
{
echo "# compiler Flags
CC = $CC
CFLAGS=$CFLAGS
FC = $FC 
FFLAGS = $FFLAGS
RANLIB = $RANLIB
export  CC CFLAGS  FC FFLAGS RANLIB 

" > FlagsForMake
}

testCC()
{
# Testing of C-compiler

if(test ! -x "`which $CC`") then 
  echo Can not find C compiler  $CC
  echo  ... trying  gcc ...
  if(test -x "`which gcc`") then
     CC=gcc
     CFLAGS=-fsigned-char
  else   
     echo gcc also is not detected ...
     echo Write down the compile name  and its options in FlagsForSh file.
     echo "     CC=\".....\""
     echo "     CFLAGS=\".....\""
     return 1
  fi
fi  

cat >test.c <<EOF
int main(void)
{ char a,b;
  a=1;
  b=-a;
  if(a>b) return 0;else return 1;
}
EOF

$CC -o a.out test.c  1>/dev/null 2>/dev/null
if(test $? -ne 0)then
  echo  Fatal problem with C compiler.
  return 1
fi 


$CC -o a.out test.c $CFLAGS 1>/dev/null 2>/dev/null
if(test $? -ne 0)then
  echo  Wrong compiler options for $CC
  return 1
fi 


#test for character type
./a.out
if(test $? -ne 0) then
  echo  C compiler uses unsigned char type. 
  echo  Improve CFLAGS  in FlagsForSh file to have char signed.
  return  1
fi 

echo C compiler detected
return 0  

}


testSO()
{
cat >test.c<<EOF
#include<stdlib.h>
double *Q=NULL;
void xxx(void)
{ int i;
  Q=(double*) realloc(Q,10*sizeof(double));
  for(i=0;i<10;i++) Q[i]=0;
}
EOF
                                                                                
$CC $CFLAGS -shared -o test.$SO  test.c  1>/dev/null 2>/dev/null
if(test $? -ne 0) then
  echo can not generate shared  library
#  if(test $CC = gcc) then 
     echo tring to add -fPIC option 
     CFLAGS="$CFLAGS -fPIC"
     $CC $CFLAGS -shared -o test.$SO  test.c  1>/dev/null 2>/dev/null
     if(test $? -ne 0) then
       echo .. it does not help
       writeFlags 
       return 1
     else 
       writeFlags
     fi   
#  fi
fi   

echo : shared liblary generation OK
return 0
}





testFC()
{  
cat >test.f <<EOF
C     TEST     
      WRITE(*,*) 'test program'
      END
EOF

  if(test ! -x "`which $FC`") then 
     echo Can not find Fortran compiler  $FC
 
      if(test -x "`which gfortran`") then
         echo gfortran is detected!
         FC=gfortran
         FFLAGS=
      else   
      if(test -x "`which g77`") then
         echo g77 is detected! 
         FC=g77
         FFLAGS=-fno-automatic
         lFort=-lg2c
      else   
      if(test -x "`which f77`") then
         echo f77 is detected! 
         FC=f77
         echo default compiler flags are used for F77
         FFLAGS=
         lFort=
      else    
          echo Fortran compiler is not detected. 
          FC=
          FFLAGS=
          return 0
      fi
      fi
      fi
  fi  

  echo Fortran compiler is detected

  $FC  -o a.out test.f  1>/dev/null 2>/dev/null
  if(test $? -ne 0) then
    echo Fatal problem in  fortran compiler
    return 1
  else 
    echo $FC OK
  fi 


  $FC $FFLAGS -o a.out test.f  1>/dev/null 2>/dev/null
  if(test $? -ne 0) then
    echo Unrecognized flags of fortran compiler
    echo improve FFLAGS in calchep/FlagsForSh 
    return 1
  else 
    echo FFLAGS OK
  fi 
  rm -f a.out 
  return 0
}


#==================== Starting point ============
rm -rf fTest
mkdir fTest
cd fTest


if(test -r ../FlagsForSh) then
  . ../FlagsForSh
else 
  findMAKE
echo MAKE= $MAKE
  defaultFlags
  writeFlags  
fi




# FORTRAN COMPILER

testCC;  if(test $? -ne 0  ) then  exit 1; fi
testSO;  if(test $? -ne 0  ) then  exit 1; fi
testFC;  if(test $? -ne 0 )  then  exit 1; fi

writeFlags
cd ..
rm -fr fTest

writeFlagsForMake
exit 0
