#!/bin/sh

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

set -e

LC_ALL=C
export LC_ALL

##################################################
# options
usage (){
    cat <<'EOF'
mkc_check_custom - tries to compile source file specified by user,
   optionally builds and runs an application, and
   returns the result (1 - build succeded, 0 - build failed,
   other value returned by built application)

Usage:
 mkc_check_custom [OPTIONS] source_file
 mkc_check_custom [OPTIONS] cmd [args...]

OPTIONS:
   -h|--help            display this help
   -r                   build application and run it
   -p                   a part of cache filename, defaults to "custom"
   -n                   a part of cache filename, defaults to
                        `basename <source_file>` without extension
   -m                   A part of verbose message, defaults to -n args
   -s                   exit status of executable will be check
Examples:
   mkc_check_custom my_custom_test.c
   mkc_check_custom -r mmap_works_perfectly.c
EOF
}

if test $# -eq 0; then
    usage
    exit 1
fi
if test "_$1" = '_-h' -o "_$1" = '_--help'; then
    usage
    exit 0
fi

while test $# -ne 0; do
    case "$1" in
	-r)
	    runit=1;;
	-p)
	    pref="$2"
	    shift;;
	-n)
	    basefn="$2"
	    shift;;
	-m)
	    msg="$2"
	    shift;;
	-s)
	    check_status=1;;
	-*)
	    echo "Bad option $1" 1>&2
	    exit 1;;
	*)
	    break;
    esac
    shift
done

if test $# -lt 1; then
    usage
    exit 1
fi

##################################################
# initializing
if test -z "$basefn"; then
    basefn=`basename $1 | sed 's|[.][^.]*$||'`
fi
pathpart="${pref-custom}_$basefn"

. mkc_check_common.sh

src_or_exe="$1"

shquote (){
    __cmd=`printf '%s\n' "$1" | sed "s|'|'\\\\\''|g"`
    printf "%s\n" "'$__cmd'"
}

for i in "$@"; do
    cmd="$cmd "`shquote "$1"`
    shift
done

##################################################
# functions

compile (){
    if $CC -c -o "$tmpo" $CPPFLAGS $CFLAGS "$src_or_exe" 2>"$tmperr"; then
	echo 1
    else
	echo 0
    fi
}

check_itself (){
    if test -x "$src_or_exe"; then
	if test -n "$check_status"; then
	    set +e # workaround for buggy FreeBSD shell
	    if eval "$cmd"; then
		echo 1
	    else
		echo 0
	    fi
	    set -e # workaround for buggy FreeBSD shell
	else
	    eval "$cmd"
	fi
	return 0
    else
	case "$src_or_exe" in
	    *.c)
		compiler="$CC"
		flags="-c -o $tmpo $CFLAGS $CPPFLAGS $src_or_exe";;
	    *.cc|*.C|*.cxx|*.cpp)
		compiler="$CXX"
		flags="-c -o $tmpo $CXXFLAGS $CPPFLAGS $src_or_exe";;
	    *.f)
		compiler="$FC"
		flags="-c -o $tmpo $FFLAGS $src_or_exe";;
	    *)
		echo 'Bad filename for custom check. What to do?' 1>&2
		return 1
	esac
    fi

    if test -z "$compiler"; then
	echo "Bad compiler for $src_or_exe. What to do?" 1>&2
	return 1
    fi

    if $compiler $flags; then
	echo 1
    else
	echo 0
    fi
}

##################################################
# test
msg=${msg-"custom test $basefn"}
check_and_cache "checking for ${msg}" "$cache"

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

KEEP_SOURCE=1 # do not delete user's source file!
cleanup

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

case "$ret" in
    1)
	printme '1 (yes)\n' 1>&2;;
    0)
	printme '0 (no)\n' 1>&2;;
    *)
	printme '%s\n' "$ret" 1>&2;;
esac

echo $ret
