#!/bin/bash

check ()
{

        for dir in ${PATH//:/ }; do
                if [[ -e ${dir}/${1} ]]; then
                        echo found && continue 2
                fi
        done

}

check_helper () {

	if [[ ${APPS[@]} != "" ]]; then
		section_message APP
		check_apps "false" "${APPS[@]}"
	fi

	if [[ ${OPT_APPS[@]} != "" ]]; then
		section_message OPT_APP
		check_apps "true" "${OPT_APPS[@]}"
	fi

	if [[ ${SAPPS[@]} != "" ]]; then
		section_message SBINAPP
		check_apps "false" "${SAPPS[@]}"
	fi

	if [[ ${OPT_SAPPS[@]} != "" ]]; then
		section_message OPT_SBINAPP
		check_apps "true" "${OPT_SAPPS[@]}"
	fi

	#check_python

	if [[ ${PYMODS[@]} != "" ]]; then
		section_message PYMOD
		check_pymods "false" "${PYMODS[@]}"
	fi

	if [[ ${OPT_PYMODS[@]} != "" ]]; then
		section_message OPT_PYMOD
		check_pymods "true" "${OPT_PYMODS[@]}"
	fi

	if [[ ${LIBS[@]} != "" ]]; then
		section_message LIB
		check_libs "false" "${LIBS[@]}"
	fi

	if [[ ${OPT_LIBS[@]} != "" ]]; then
		section_message OPT_LIB
		check_libs "false" "${OPT_LIBS[@]}"
	fi

	if [[ ${GIR[@]} != "" ]]; then
		section_message GIR
		check_gir "false" "${GIR[@]}"
	fi

	if [[ ${OPT_GIR[@]} != "" ]]; then
		section_message OPT_GIR
		check_gir "false" "${OPT_GIR[@]}"
	fi

}

check_apps () {

OPT=$1
shift

for ARG in "$@"; do
	APP="${ARG/:*}"
	DESC="${ARG/*:}"
	DEP_RETURN=$(check $APP)
	if [[ $DEP_RETURN == "found" ]]; then
		ok_message "$APP"
	elif [[ $DEP_RETURN != "found" && $OPT == "true" ]]; then
		warn_message $APP "$DESC"
		MISSING+=" $APP"
	else 	fail_message m $APP
		exit 1
	fi
done

}

check_libs () {

OPT=$1
shift

for ARG in "$@"; do

	LIB=$(gawk -F \: '{print $1}' <(echo $ARG))
	VERSION=$(gawk -F \: '{print $2}' <(echo $ARG))
	PC_FILE=$(gawk -F \: '{print $3}' <(echo $ARG))
	DESC=$(gawk -F \: '{print $4}' <(echo $ARG))

	pkg-config --exists $PC_FILE
	EXIST_RETURN=$?

	pkg-config --atleast-version=$VERSION $PC_FILE
	DEP_RETURN=$?

	if [[ $EXIST_RETURN != 0 && ! $OPT == "true" ]]; then
		fail_message m $LIB
		exit 1
	elif [[ $DEP_RETURN == 0 ]]; then
		ok_message $LIB ${VERSION}+
	elif [[ $EXIST_RETURN != 0 && $OPT == "true" ]]; then
		warn_message $LIB "$DESC"
		MISSING+=" $LIB"
	else	fail_message o $LIB $VERSION
		exit 1
	fi

done

}

check_gir () {

OPT=$1
shift

for ARG in "$@"; do

	GIR=$(gawk -F \: '{print $1}' <(echo $ARG))
	VERSION=$(gawk -F \: '{print $2}' <(echo $ARG))
	TYPE_LIB=$(gawk -F \: '{print $3}' <(echo $ARG))
	DESC=$(gawk -F \: '{print $4}' <(echo $ARG))
	FILE=$(basename ${GIRPATH}/$TYPE_LIB* .typelib 2>/dev/null)

	if [[ $FILE != ".typelib" ]]; then
		EXIST_RETURN=0
		XVER=$(gawk -F \- '{print $2}' <(echo $FILE))
		DEP_RETURN=$(echo "$XVER >= $VERSION" | bc)
	fi

	if [[ $EXIST_RETURN != 0 && ! $OPT == "true" ]]; then
		fail_message m $GIR
		exit 1
	elif [[ $DEP_RETURN == 1 ]]; then
		ok_message $GIR ${VERSION}+
	elif [[ $EXIST_RETURN != 0 && $OPT == "true" ]]; then
		warn_message $GIR "$DESC"
		MISSING+=" $GIR"
	else	fail_message o $GIR $VERSION
		exit 1
	fi

done

}

check_python () {

section_message PY

if [[ ! -e $PYTHON ]]; then
	fail_message p
	exit 1
else
	PYMINVER=270
	PYMAXVER=350
	PYVER=$($PYTHON --version 2>&1 | gawk '{ gsub(/\./,""); gsub(/\+/,""); gsub(/rc.*/,""); print $2 }')
	if [[ $PYVER -lt $PYMINVER ]]; then
		fail_message p-
		exit 1
	elif [[ $PYVER -gt $PYMAXVER ]]; then
		fail_message p+
		exit 1
	else
		ok_message Python
	fi
fi

}

check_pymods () {

OPT=$1
shift

for ARG in "$@"; do

	MOD=$(gawk -F \: '{print $1}' <(echo $ARG))
	SMOD=$(gawk -F \: '{print $2}' <(echo $ARG))
	DESC=$(gawk -F \: '{print $3}' <(echo $ARG))

	if [[ $SMOD != "" ]]; then
		DEP="$SMOD ($MOD)"
		${PYTHON} -c "from $MOD import $SMOD" 2>/dev/null
	else
		DEP="$MOD"
		${PYTHON} -c "import imp
imp.find_module('$MOD')" 2>/dev/null
	fi

	DEP_RETURN=$?
	if [[ $DEP_RETURN == 0 ]]; then
		ok_message "$DEP"
	elif [[ $DEP_RETURN != 0 && $OPT == "true" ]]; then
		warn_message $DEP "${DESC}"
		MISSING+=" $DEP"
	else 	fail_message m $DEP
		exit 1
	fi

done

}
