#!/bin/sh

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

set -e

LC_ALL=C
export LC_ALL

: ${CHECK_COMMON_SH_DIR:=@mkc_libexecdir@}

##################################################
# options
usage (){
    cat <<EOF
mkc_check_header detects presense of header file
by compiling a test program.

Usage: mkc_check_header [OPTIONS] <headers>

OPTIONS:
   -h        display this screen
   -d        delete cache file
   -e        check for existance of header file (cc -E vs. cc -c)
Examples:
   mkc_check_header stdint.h
   mkc_check_header getopt.h
EOF
}

CC_OPTS='-c'
partpath='header'
msg_addon="using ${CC} -c"

while test $# -ne 0; do
    case "$1" in
	-h) usage; exit 0;;
	-d) delcache=1;;
	-e) partpath=hdr; CC_OPTS='-E'; msg_addon="using ${CC} -E";;
	*) break;;
    esac
    shift
done

if test $# -eq 0; then
    usage
    exit 1
fi

##################################################
# initializing

pathpart="$partpath"_`echo "$1" | sed 's/^.*,//' | tr /. __`

. ${CHECK_COMMON_SH_DIR}/mkc_check_common.sh

##################################################
# test

check_itself (){
    get_includes "$@" > "$tmpc"

    cat >> "$tmpc" <<EOF
int __fake_variable;
EOF

    if $CC $CC_OPTS -o "${tmpo}" $CPPFLAGS $CFLAGS "${tmpc}" 2>"${tmperr}"
    then
	echo 1
    else
	echo 0
    fi
}

check_and_cache "checking $1 $msg_addon" "$cache" "$@"

##################################################
# clean-ups

cleanup

##################################################
# finishing

if test "$ret" -eq 1; then
    printme 'yes\n' 1>&2
else
    printme 'no\n' 1>&2
fi

echo $ret
