#!/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:=/usr/pkg/libexec/mk-configure}

##################################################
# options
usage (){
    cat <<EOF
mkc_check_prog detects presense of program file.

Usage: mkc_check_prog [OPTION] progname

OPTIONS:
   -h                   display this screen
   -i <progid>          program id, a part of _mkc_* cache file
   -d                   delete cache files and temporary files
   -D                   delete cache files
Examples:
   mkc_check_prog -h
   mkc_check_prog lua
   mkc_check_prog gawk
   mkc_check_prog -i gxx g++
EOF
}

while test $# -ne 0; do
    case "$1" in
	-h)
	    usage
	    exit 0;;
	-i)
	    pathpart=prog_$2
	    shift;;
	-d)
	    delcache=result;;
	-D)
	    delcache=all;;
	--)
	    shift
	    break;;
	-*)
	    echo "Unknown option $1" 1>&2
	    exit 1;;
	*)
	    break;;
    esac
    shift
done

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

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

pathpart=${pathpart-prog_`echo $1 | tr /. __`}

. ${CHECK_COMMON_SH_DIR}/mkc_check_common.sh

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

check_itself (){
    mkc_which -x "$1" 2>"${tmperr}"
}

check_and_cache "checking for program $1" "$cache" "$1"

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

cleanup

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

if test -n "$ret"; then
    printme "$ret\n" 1>&2
else
    printme 'NOT FOUND\n' 1>&2
fi

echo $ret
