#!/bin/bash
shopt -s extglob

if [[ $1 != *mm ]]; then

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=""

fi

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"

if [[ $TERM != *xterm* ]]; then
	SMOOTHBLUE=$BLUE
	ORANGE=$YELLOW
	CREAM=$MAGENTA
else
	SMOOTHBLUE="\033[01;38;5;111m"
	ORANGE="\033[01;38;5;714m"
	CREAM="\033[01;38;5;5344m"
fi

if [[ $1 == *help ]]; then
	echo -e "\n${GREEN}SplashyCreator ${YELLOW}(configure) ${CYAN}help"
	echo -e "\n${WHITE}Environment Variables:\n"
	echo -e "\n${ORANGE}VERBOSE=1${WHITE} |${GREEN} Print output of checks\
	\n${ORANGE}VERBOSE=2${WHITE} |${GREEN} Like VERBOSE=1 plus return code" | column -t
	echo -e "\n${WHITE}Options:\n"
	echo -e "\n${ORANGE}--help${WHITE} |${GREEN} Display this help message\
	\n${ORANGE}--mm${WHITE} |${GREEN} Enter Maintainer Mode (disable checks)\
	\n${ORANGE}--prefix=${WHITE} |${GREEN} Change Installation Prefix (default /usr)" | column -t
	echo ""
	tput sgr0
	exit 0
fi

if [[ $1 != *mm ]]; then

echo -e "\n${GREEN}SplashyCreator${YELLOW} v3.1 ${CYAN}(Alaska)"

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

while (( x <= $APPCOUNT )); do
	APP=$(grep -w APP$x configure.checks | gawk '{print $2}')
	if [[ $VERBOSE == 1 ]]
	then	which ${APP}
	elif [[ $VERBOSE == 2 ]]
	then	which ${APP}
		echo "Which returned: $?"
	else	which ${APP} &>/dev/null
	fi
	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!"
		tput sgr0
		exit 2 &>/dev/null
	fi
	x=$((x+1)) ; done
x=1
fi

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

while (( x <= $OPT_APPCOUNT )); do
	APP=$(grep -w OPT_APP$x configure.checks | gawk '{print $2}')
	if [[ $VERBOSE == 1 ]]
	then	which ${APP}
	elif [[ $VERBOSE == 2 ]]
	then	which ${APP}
		echo "Which returned: $?"
	else	which ${APP}&>/dev/null
	fi
	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 ${ORANGE}Required ${BLUE}SuperUser Applications:\n"

while (( x <= $SBINAPPCOUNT )); do
	APP=$(grep -w APP$x configure.checks | gawk '{print $2}')
	if [[ $VERBOSE == 1 ]]
	then	which ${APP}
	elif [[ $VERBOSE == 2 ]]
	then	which ${APP}
		echo "Which returned: $?"
	else 	which ${APP} &>/dev/null
	fi
	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!"
		tput sgr0
		exit 2 &>/dev/null
	fi
	x=$((x+1)) ; done
x=1
fi

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

while (( x <= $OPT_SBINAPPCOUNT )); do
	APP=$(grep -w OPT_SBINAPP$x configure.checks | gawk '{print $2}')
	if [[ $VERBOSE == 1 ]]
	then 	which ${APP}
	elif [[ $VERBOSE == 2 ]]
	then	which ${APP}
		echo "Which returned: $?"
	else	which ${APP} &>/dev/null
	fi
	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 ${ORANGE}Required ${BLUE}Python Modules:\n"

while (( x <= $PYMODCOUNT )); do
	PYMOD=$(grep -w PYMOD$x configure.checks | gawk '{print $2}')
	if [[ $VERBOSE == 1 ]]
	then	python -c "import $PYMOD"
	elif [[ $VERBOSE == 2 ]]
	then	python -c "import $PYMOD"
		echo "Python returned: $?"
	else 	python -c "import $PYMOD" &>/dev/null
	fi
	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!"
		tput sgr0
		exit 2 &>/dev/null
	fi
	x=$((x+1)) ; done
x=1
fi

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

while (( x <= $OPT_PYMODCOUNT )); do
	PYMOD=$(grep -w OPT_PYMOD$x configure.checks | gawk '{print $2}')
	if [[ $VERBOSE == 1 ]]
	then	python -c "import $PYMOD"
	elif [[ $VERBOSE == 2 ]]
	then	python -c "import $PYMOD"
		echo "Python returned: $?"
	else 	python -c "import $PYMOD" &>/dev/null
	fi
	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 ${CREAM}Required ${BLUE}Device Nodes:\n"

while (( x <= $NODECOUNT )); do
	NODE=$(grep -w NODE$x configure.checks | gawk '{print $2}')
	if [[ -e /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!"
		tput sgr0
		exit 2 &>/dev/null
	fi
	x=$((x+1)) ; done
x=1
fi

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

while (( x <= $OPT_NODECOUNT )); do
	NODE=$(grep -w OPT_NODE$x configure.checks | gawk '{print $2}')
	if [[ -e /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

fi

cp Makefile.in Makefile
cp code/splashy-creator.in code/splashy-creator
cp data/splashy-creator.in data/splashy-creator
cp code/splashy-manager.in code/splashy-manager
cp data/splashy-manager.in data/splashy-manager

case $@ in
	*--prefix=*)
		if [[ $1 == "mm" ]]; then
			prefix=$(echo -e $2 | sed -e 's/.*=//g')
		else	prefix=$(echo -e $1 | sed -e 's/.*=//g')
		fi
		sed -e "s#@PREFIX@#$prefix#g" -i Makefile
		sed -e "s#@PREFIX@#$prefix#g" -i code/splashy-creator
		sed -e "s#@PREFIX@#$prefix#g" -i data/splashy-creator
		sed -e "s#@PREFIX@#$prefix#g" -i code/splashy-manager
		sed -e "s#@PREFIX@#$prefix#g" -i data/splashy-manager
		echo -e "\n${WHITE}Prefix:${BLUE} $prefix"
	;;

	*)
		sed -e 's#@PREFIX@#/usr#g' -i Makefile
		sed -e 's#@PREFIX@#/usr#g' -i code/splashy-creator
		sed -e 's#@PREFIX@#/usr#g' -i data/splashy-creator
		sed -e 's#@PREFIX@#/usr#g' -i code/splashy-manager
		sed -e 's#@PREFIX@#/usr#g' -i data/splashy-manager
		echo -e "\n${WHITE}Prefix:${BLUE} /usr"
	;;
esac

if [[ $1 != *mm ]]; then

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

export PATH=$OLDPATH

else	echo -e "\n${RED}Maintainer Mode ${WHITE}/ ${GREEN}Disabled Checks\n"
fi

tput sgr0
