#!/bin/bash
shopt -s extglob

export OLDPATH=$PATH
export PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin"

APPCOUNT=$(grep -w APP[0-9]* configure.checks | wc -l)
OPT_APPCOUNT=$(grep -w OPT_APP[0-9]* configure.checks | wc -l)
SBINAPPCOUNT=$(grep -w SBINAPP[0-9]* configure.checks | wc -l)
OPT_SBINAPPCOUNT=$(grep -w OPT_SBINAPP[0-9]* configure.checks | wc -l)
PYMODCOUNT=$(grep -w PYMOD[0-9]* configure.checks | wc -l)
OPT_PYMODCOUNT=$(grep -w OPT_PYMOD[0-9]* configure.checks | wc -l)
NODECOUNT=$(grep -w NODE[0-9]* configure.checks | wc -l)
OPT_NODECOUNT=$(grep -w OPT_NODE[0-9]* configure.checks | 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}SplashyCreator${YELLOW} v2.2 ${MAGENTA}(Sonic Empire)"

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

while (( x <= $APPCOUNT )); do
	APP=$(grep -w APP$x configure.checks | 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 configure.checks | 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 configure.checks | 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 configure.checks | 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_SBINAPPCOUNT != 0 ]]; then
echo -e "\n${BLUE}Checking Optional SuperUser Applications:\n"

while (( x <= $OPT_SBINAPPCOUNT )); do
	APP=$(grep -w OPT_SBINAPP$x configure.checks | gawk '{print $2}')
	which ${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 configure.checks | 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 configure.checks | 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 configure.checks | 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 configure.checks | 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 configure.checks | 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 configure.checks | 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 configure.checks | gawk -F _ '{ for (x=3; x <= NF; x++) { print $x; } }' | sed -e 's/\"//g' )
	fi
	x=$((x+1)) ; done

x=1
fi

cp splashy-creator.in splashy-creator
cp python-gtk/splashy-creator.in python-gtk/splashy-creator
cp Makefile.in Makefile

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/splashy-creator
		sed -e "s#@PREFIX@#$prefix#g" -i splashy-creator
		echo -e "\n${WHITE}Prefix:${BLUE} $prefix"
	;;

	*)
		sed -e 's#@PREFIX@#/usr#g' -i Makefile
		sed -e 's#@PREFIX@#/usr#g' -i python-gtk/splashy-creator
		sed -e 's#@PREFIX@#/usr#g' -i splashy-creator
		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

export PATH=$OLDPATH

tput sgr0
