#!/bin/sh

############################################################
# Copyright (c) 2009 by Aleksey Cheusov
#
# See LICENSE file in the distribution.
############################################################

set -e

LC_ALL=C
export LC_ALL

: ${CHECK_COMMON_SH_DIR:=/usr/pkg/libexec/mk-configure}

##################################################

pathpart=endianness
. ${CHECK_COMMON_SH_DIR}/mkc_check_common.sh

trap "rm -f $tmpc $tmpo" 0

CFLAGS="$CFLAGS -D_GNU_SOURCE -D_ALL_SOURCE"

check_me (){
	cat > "$tmpc" <<EOF
$3

int main (int argc, char **argv)
{
  #if $1 == $2
   return 0;
#else
   error
#endif
}
EOF
	if $CC -o "$tmpo" -c $CFLAGS $CPPFLAGS "$tmpc" 2>/dev/null; then
		return 0
	else
		return 1
	fi
}

if check_me BYTE_ORDER LITTLE_ENDIAN '
#include <sys/types.h>
#include <sys/param.h>
';
then
    echo little
    exit 0
fi

if check_me BYTE_ORDER BIG_ENDIAN '
#include <sys/types.h>
#include <sys/param.h>
';
then
    echo big
    exit 0
fi

echo unknown
exit 0
