:

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

echo UNIX `uname`

if(test -r "CC") then 
   CC=`cat CC`
else

  case `uname` in

  Linux) CC="gcc  -Wstrict-prototypes -Wimplicit  -Wmissing-prototypes -Wunused" 
       ;;
  IRIX)  CC="cc -signed -xansi -fullwarn"
       ;;
  IRIX64)CC="cc -signed -xansi -fullwarn"
       ;;
  HP-UX) CC="cc -Ae -D_HPUX_SOURCE "
       ;;
  AIX)   CC="cc -qchars=signed "
       ;;
  OSF1)  CC="cc -std1 -ieee"
       ;;
     *) CC="gcc  -fsigned-char -Wstrict-prototypes -Wimplicit -Wmissing-prototypes -Wunused";
        $CC  test.c 1>/dev/null 2>/dev/null
        if (test $? -ne 0)then
          echo  Unknown UNIX platform, gcc not found
          CC=cc
        fi
       ;;
  esac
  echo $CC >CC

fi

#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 Improve the ./CC file
  exit 1
fi 


#test for character type
./a.out
if(test $? -ne 0) then
  echo  C compiler uses unsigned char type. 
  echo  Improve ./CC file to have signed char type.
  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
  ./a.out
  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
rm a.out test.*
