#!/bin/bash
shopt -s extglob

APPCOUNT=$(grep -w APP[0-9]* $PWD/cfg/config | wc -l)
OPT_APPCOUNT=$(grep -w OPT_APP[0-9]* $PWD/cfg/config | wc -l)
SBINAPPCOUNT=$(grep -w SBINAPP[0-9]* $PWD/cfg/config | wc -l)
OPT_SBINAPPCOUNT=$(grep -w OPT_SBINAPP[0-9]* $PWD/cfg/config | wc -l)
PYMODCOUNT=$(grep -w PYMOD[0-9]* $PWD/cfg/config | wc -l)
OPT_PYMODCOUNT=$(grep -w OPT_PYMOD[0-9]* $PWD/cfg/config | wc -l)
NODECOUNT=$(grep -w NODE[0-9]* $PWD/cfg/config | wc -l)
OPT_NODECOUNT=$(grep -w OPT_NODE[0-9]* $PWD/cfg/config | wc -l)
x=1
ERR_MSG=""

GREEN="\033[01;32m"
RED="\033[01;31m"
WHITE="\033[01;37m"
YELLOW="\033[01;33m"
CYAN="\033[01;36m"
BLUE="\033[01;34m"
MAGENTA="\033[01;35m"
SMOOTHBLUE="\033[01;38;5;111m"

echo -e "\n${GREEN}BashStyle-NG${YELLOW} v7.2 ${MAGENTA}(rev325)"

if [[ $APPCOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Required Applications:\n"

while (( x <= $APPCOUNT )); do
	APP=$(grep -w APP$x cfg/config | gawk '{print $2}')
	which ${APP} &>/dev/null
	if (( $? == 0 )) ; then
		echo -e "${WHITE}Searching for application: ${CYAN}${APP}${MAGENTA} >${GREEN} O.K.${SMOOTHBLUE} ( $(which ${APP}) )"
	else
		echo -e "${WHITE}Searching for application: ${CYAN}${APP}${MAGENTA} >${RED} !MISSING!"
		exit 2 &>/dev/null
	fi
	x=$((x+1)) ; done

x=1
fi

if [[ $OPT_APPCOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Optional Applications:\n"

while (( x <= $OPT_APPCOUNT )); do
	APP=$(grep -w OPT_APP$x cfg/config | gawk '{print $2}')
	which ${APP} &>/dev/null
	if (( $? == 0 )) ; then
		echo -e "${WHITE}Searching for application: ${CYAN}${APP}${MAGENTA} >${GREEN} O.K.${SMOOTHBLUE} ( $(which ${APP}) )"
	else
		echo -e "${WHITE}Searching for application: ${CYAN}${APP}${MAGENTA} >${YELLOW} MISSING (Optional)"
		ERR_MSG+=$(grep -w OPT_APP$x $PWD/cfg/config | gawk -F _ '{ for (x=3; x <= NF; x++) { print $x; } }' | sed -e 's/\"//g' )
	fi
	x=$((x+1)) ; done

x=1
fi

if [[ $SBINAPPCOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Required SuperUser Applications:\n"

while (( x <= $SBINAPPCOUNT )); do
	APP=$(grep -w APP$x cfg/config | gawk '{print $2}')
	test -e /sbin/$APP || test -e /usr/sbin/$APP &>/dev/null
	if (( $? == 0 )) ; then
		echo -e "${WHITE}Searching for application: ${CYAN}${APP}${MAGENTA} >${GREEN} O.K.${SMOOTHBLUE} ( $(which ${APP}) )"
	else
		echo -e "${WHITE}Searching for application: ${CYAN}${APP}${MAGENTA} >${RED} !MISSING!"
		exit 2 &>/dev/null
	fi
	x=$((x+1)) ; done

x=1
fi

if [[ $OPT_SBINAPPCOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Optional SuperUser Applications:\n"

while (( x <= $OPT_SBINAPPCOUNT )); do
	APP=$(grep -w OPT_SBINAPP$x cfg/config | gawk '{print $2}')
	test -e /usr/sbin/$APP &>/dev/null
	if (( $? == 0 )) ; then
		echo -e "${WHITE}Searching for application: ${CYAN}${APP}${MAGENTA} >${GREEN} O.K.${SMOOTHBLUE} ( /usr/sbin/$APP )"
	else
		echo -e "${WHITE}Searching for application: ${CYAN}${APP}${MAGENTA} >${YELLOW} MISSING (Optional)"
		ERR_MSG+=$(grep -w OPT_SBINAPP$x $PWD/cfg/config | gawk -F _ '{ for (x=3; x <= NF; x++) { print $x; } }' | sed -e 's/\"//g' )
	fi
	x=$((x+1)) ; done

x=1
fi

if [[ $PYMODCOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Required Python Modules:\n"

while (( x <= $PYMODCOUNT )); do
	PYMOD=$(grep -w PYMOD$x cfg/config | gawk '{print $2}')
	python -c "import $PYMOD" &>/dev/null
	if (( $? == 0 )) ; then
		echo -e "${WHITE}Searching for python module: ${CYAN}${PYMOD}${MAGENTA} >${GREEN} O.K."
	else
		echo -e "${WHITE}Searching for python module: ${CYAN}${PYMOD}${MAGENTA} >${RED} !MISSING!"
		exit 2 &>/dev/null
	fi
	x=$((x+1)) ; done

x=1
fi

if [[ $OPT_PYMODCOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Optional Python Modules:\n"

while (( x <= $OPT_PYMODCOUNT )); do
	PYMOD=$(grep -w OPT_PYMOD$x cfg/config | gawk '{print $2}')
	python -c "import $PYMOD" &>/dev/null
	if (( $? == 0 )) ; then
		echo -e "${WHITE}Searching for python module: ${CYAN}${PYMOD}${MAGENTA} >${GREEN} O.K."
	else
		echo -e "${WHITE}Searching for python module: ${CYAN}${PYMOD}${MAGENTA} >${YELLOW} MISSING (Optional)"
		ERR_MSG+=$(grep -w OPT_PYMOD$x $PWD/cfg/config | gawk -F _ '{ for (x=3; x <= NF; x++) { print $x; } }' | sed -e 's/\"//g' )
	fi
	x=$((x+1)) ; done

x=1
fi

if [[ $NODECOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Optional Device Nodes:\n"

while (( x <= $NODECOUNT )); do
	NODE=$(grep -w NODE$x cfg/config | gawk '{print $2}')
	if [[ -a /dev/$NODE ]] ; then
		echo -e "${WHITE}Searching for node: ${CYAN}${NODE}${MAGENTA} >${GREEN} O.K."
	else
		echo -e "${WHITE}Searching for node: ${CYAN}${NODE}${MAGENTA} >${RED} !MISSING!"
		exit 2 &>/dev/null
	fi
	x=$((x+1)) ; done

x=1
fi

if [[ $OPT_NODECOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Optional Device Nodes:\n"

while (( x <= $OPT_NODECOUNT )); do
	NODE=$(grep -w OPT_NODE$x cfg/config | gawk '{print $2}')
	if [[ -a /dev/$NODE ]] ; then
		echo -e "${WHITE}Searching for node: ${CYAN}${NODE}${MAGENTA} >${GREEN} O.K."
	else
		echo -e "${WHITE}Searching for node: ${CYAN}${NODE}${MAGENTA} >${YELLOW} MISSING (Optional)"
		ERR_MSG+=$(grep -w OPT_NODE$x $PWD/cfg/config | gawk -F _ '{ for (x=3; x <= NF; x++) { print $x; } }' | sed -e 's/\"//g' )
	fi
	x=$((x+1)) ; done

x=1
fi

cp bashstyle.in bashstyle
cp python-gtk/bashstyle.in python-gtk/bashstyle
cp Makefile.in Makefile
cp bs-ng.desktop.in bs-ng.desktop
cp system/nx-rc.in system/nx-rc
cp support/bswitch.in support/bswitch

case $1 in
	--prefix=*)
		prefix=$(echo -e $1 | sed -e 's/.*=//g')
		sed -e "s#@PREFIX@#$prefix#g" -i Makefile
		sed -e "s#@PREFIX@#$prefix#g" -i python-gtk/bashstyle
		sed -e "s#@PREFIX@#$prefix#g" -i bashstyle
		sed -e "s#@PREFIX@#$prefix#g" -i bs-ng.desktop
		sed -e "s#@PREFIX@#$prefix#g" -i system/nx-rc
		sed -e "s#@PREFIX@#$prefix#g" -i support/bswitch
		echo -e "\n${WHITE}Prefix:${BLUE} $prefix\n"
	;;

	*)
		sed -e 's#@PREFIX@#/usr#g' -i Makefile
		sed -e 's#@PREFIX@#/usr#g' -i python-gtk/bashstyle
		sed -e 's#@PREFIX@#/usr#g' -i bashstyle
		sed -e 's#@PREFIX@#/usr#g' -i bs-ng.desktop
		sed -e "s#@PREFIX@#/usr#g" -i system/nx-rc
		sed -e "s#@PREFIX@#/usr#g" -i support/bswitch
		echo -e "\n${WHITE}Prefix:${BLUE} /usr"
	;;
esac

if [[ $ERR_MSG != "" ]];  then
	echo -e "\n${BLUE}Important Informations:\n"
	echo -e "${YELLOW}${ERR_MSG}"
fi

tput sgr0
