:

echo UNIX `uname`


case `uname` in

Linux) CC="gcc  -Wstrict-prototypes -Wimplicit  -Wmissing-prototypes -Wunused" 
       F77="f77 -fno-automatic"
       lFort=-lg2c
       lDL="-rdynamic -ldl"
       RANLIB=ranlib
       AOUT=a.out
     ;;
IRIX)  CC="cc -signed -xansi -fullwarn"
       F77=f77
       lFort=-lftn
       lDL=
       RANLIB=ranlib
       AOUT=a.out
     ;;
IRIX64)CC="cc -signed -xansi -fullwarn"
       F77=f77
       lFort=-lftn
       lDL=
       RANLIB=ranlib
       AOUT=a.out
     ;;
HP-UX) CC="cc -Ae -D_HPUX_SOURCE "
       F77="f77 +ppu"
       lFort="-lcl -lisamstub -lc"
       lDL="-ldld -Wl,-E"
       RANLIB=ranlib
       AOUT=a.out
     ;;
OSF1)  CC="cc -std1 -ieee"
       F77="f77 -O0 -fpe2"
       lFort=-lfor
       lDL=-ldl
       RANLIB=ranlib
       AOUT=a.out
     ;;
SunOS) CC="cc"
       F77=f77
       lFort="-lF77 -lM77 -lsunmath"
       lDL=-ldl
       RANLIB=ranlib
       AOUT=a.out
     ;;
Darwin)CC="gcc -fsigned-char"
       F77=g77
       lFort="-L/sw/lib  -lg2c"
       lDL=-ldl
       RANLIB="ranlib -c"
       AOUT=a.out
     ;;
CYGWIN_NT-5.1) CC="gcc  -Wstrict-prototypes -Wimplicit  -Wmissing-prototypes -Wunused"
       F77="f77 -fno-automatic"
       lFort=-lg2c
       lDL="-rdynamic -ldl"
       RANLIB=ranlib
       AOUT=a.exe
     ;;
    *) CC=cc
       F77=f77
       lFort=
       lDL=
       RANLIB=ranlib
       AOUT=a.out
       echo Unknown Unix  
     ;;
esac

if(test -r RL)   then    RANLIB=`cat RL` ;  else  echo $RANLIB> RL;  fi
if(test -r CC)   then    CC=`cat CC` ;  else echo $CC > CC;  fi
if(test -r F77)  then    F77=`cat F77`; else echo $F77> F77; fi
if(test -r lDL)  then    lDL=`cat lDL`; else echo $lDL> lDL; fi
if(test -r lFort)then    lFort=`cat lFort`;
else 
  if(test `uname` = Linux) then
    cat >test.f <<EOF
C TEST
       WRITE 'OK'
       END
EOF

    $F77 -v test.f 1>ldf_out 2>ldf_out
    if(test  -n "`grep lf2c ldf_out`") then lFort=-lf2c; fi
    rm  ldf_out
  fi
  echo $lFort>lFort
fi

# Testing of C-compiler

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


#test for ANSI compiler existence 

$CC test.c 1>/dev/null 2>/dev/null
if(test $? -ne 0)then
  echo C compiler not found or have wrong options or does not understand ANSI headers.
  echo $CC > CC
  echo Improve the ./CC file
  exit 1
fi 


#test for character type
./$AOUT
if(test $? -ne 0) then
  echo  C compiler uses unsigned char type. 
  echo  Improve ./CC file to have signed char type.
  echo  $CC > CC 
  exit 1
fi 

# check  of user defined  interger  type for symbolic calculation 
num="OFF"
CC2=$CC
CC=
for option in $CC2 
do
  if(test $option = "-DNUM_DOUBLE" -o $option = "-DNUM_LONG_LONG" \
      -o  $option = "-DNUM_LONG") then
     num=$option
  else
    CC="$CC $option"
  fi
done

echo "void main(void){exit(sizeof(long long  ));}" >test.c
if(test $num = "-DNUM_LONG_LONG") then
  $CC  test.c 1>/dev/null 2>/dev/null
  if(test $? -ne 0) then
     echo -DNUM_LONG_LONG option is refused
     num="OFF"
  fi
fi

if(test "$num" = "OFF") then
# selection of integer type for symbolic calculation 
 
  echo "void main(void){exit(sizeof(long ));}" >test.c

  $CC test.c 1>/dev/null 2>/dev/null
  ./$AOUT
  if(test $? -ne 8) then
     num="-DNUM_DOUBLE"
     echo the double type will be  used for symbolic calculations
  else
     num="-DNUM_LONG"
     echo the long type will be used for symbolic calculations
  fi
fi

CC="$CC $num"
echo $CC > CC 

#================search for  X11 ==========================

cat >test.c<<EOF
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
void main(void)
{
  static Display *display;
  display = XOpenDisplay (NULL);
}
EOF


x_includes=NO
x_libraries=NO

# I) test default

$CC -c test.c 1>/dev/null 2>/dev/null
if(test $? -eq 0) then 
  x_includes=  
  $CC test.o -lX11 1>/dev/null 2>/dev/null
  if(test $? -eq 0) then x_libraries=; fi
fi


if(test  "$x_includes" = "NO" -o "$x_libraries" = "NO"   ) then
# II) search by xmkmf
  cat > Imakefile <<'EOF'
findx:
	@echo 'im_incroot="${INCROOT}"; im_usrlibdir="${USRLIBDIR}"; im_libdir="${LIBDIR}"'
EOF
  if (xmkmf) >/dev/null 2>/dev/null && test -f Makefile; then 
    eval `make findx 2>/dev/null | grep -v make`
    if(test  "$x_includes" = "NO") then
      $CC -c test.c -I$im_incroot 1>/dev/null 2>/dev/null 
      if(test $? -eq 0) then  
         x_includes=$im_incroot;
         $CC test.o  -lX11 1>/dev/null 2>/dev/null
         if(test $? -eq 0) then 
           x_libraries= ; 
         else 
           $CC test.o -L$im_usrlibdir -lX11 1>/dev/null 2>/dev/null
           if(test $? -eq 0) then x_libraries=$im_usrlibdir; fi
         fi
      fi
    else 
      $CC test.o -L$im_usrlibdir -lX11 1>/dev/null 2>/dev/null
      if(test $? -eq 0) then  x_libraries=$im_usrlibdir; fi
    fi
  fi
  if(test -f Imakefile) then  rm Imakefile; fi
  if(test -f  Makefile) then  rm  Makefile; fi
fi



XLIST="X11R6/Z X11R5/Z X11R4/Z Z/X11R6 Z/X11R5 Z/X11R4 local/X11R6/Z
local/X11R5/Z local/X11R4/Z local/Z/X11R6 local/Z/X11R5 local/Z/X11R4
X11/Z Z/X11 local/X11/Z local/Z/X11 X386/Z x386/ Z local/Z  openwin/Z 
openwin/share/Z"

#============== search x-includes from XLIST =====================
if(test "$x_includes" = "NO") then
  for dir in $XLIST
  do
    test_dir=/usr/`echo $dir|sed s/Z/include/`
    if( test -r "$test_dir/X11/Xlib.h") then
      $CC -c test.c -I$test_dir 1>/dev/null 2>/dev/null
      if(test $? -eq 0) then
        x_includes=$test_dir
        $CC test.o  -lX11 1>/dev/null 2>/dev/null
        if(test $? -eq 0) then
          x_libraries= ;
        else 
          test_dir=/usr/`echo $dir|sed s/Z/lib/`
          $CC test.o -L$test_dir -lX11 1>/dev/null 2>/dev/null 
          if(test $? -eq 0) then 
            x_libraries=$test_dir; 
            break
          fi
        fi
      fi
    fi
  done
fi

#============== search x-includes from XLIST =====================
if(test "$x_includes" != "NO"  -a "$x_libraries" = "NO" ) then
  for dir in $XLIST
  do
    test_dir=/usr/`echo $dir|sed s/Z/lib/`
    $CC test.o -L$test_dir -lX11 1>/dev/null 2>/dev/null 
    if(test $? -eq 0) then 
      x_libraries=$test_dir; 
      break
    fi
  done
fi


if(test "$x_includes" = "NO"  -o "$x_libraries" = "NO" ) then
  echo X11 is not detected
  exit 1
fi


if( test "$x_includes") then  
  CC="$CC -I$x_includes" 
  echo X-includes=$x_includes
else 
  echo default  X-includes
fi


if(test "$x_libraries") then
  CC="$CC -L$x_libraries"
  echo X-libraries=$x_libraries
else
  echo default X-libraries 
fi

echo $CC > CC

echo C-compiler OK

# FORTRAN COMPILER

cat >test.f <<EOF
C     TEST     
      INTEGER FUNCTION XXXX()
      WRITE(*,*) 'test program'
      XXXX=5
      RETURN
      END
EOF


$F77 -c test.f  1>/dev/null 2>/dev/null
if(test $? -ne 0) then
   echo Can not define Fortran compiler. 
   echo Write down the compile name and its options in F77 file.
   exit 1
fi

if(test  -z  "`nm test.o |grep xxxx_`") then
   echo Wrong F77 options. 'nm test.o' should contain xxxx_ .
   echo Write down the compile name and its options in F77 file.
   exit 1
fi

echo FORTRAN COMPILER OK 

# Check possibility of C-FORTRAN linking
cat >ctest.c <<EOF
extern int xxxx(void);
int main(void) {  return xxxx_(); }
EOF

$CC ctest.c test.o  $lFort 1>/dev/null 2>/dev/null
if(test $? -ne 0) then
   echo Promlem with C-FORTRAN linking
   echo Write down needed option in lFort file
   echo Help: $F77 -v <file>.f should write names of needed libraties.
   exit 1
fi

rm -f $AOUT ctest.c  main.c  test.c  test.f  test.o
echo C-FORTRAN linking OK
