#!/bin/bash

bg_process() {
	
	bg_pid=$1
	bg_running=$2

	count=0
	while [ "${bg_running}" = "${bg_pid}" ]
	do
		bg_running=`ps ax | grep ${bg_pid} | grep -v "grep" | awk '{ print $1 }'`

		count=`expr ${count} + 1`

		if [ ${count} = 4 ]; then tput rc; tput el
		else echo -n "."
	fi

	sleep 1
	done
}

os_version() {

	os_display=$1
	
	arch=`uname -a`

	darwin=`echo ${arch} | grep 'Darwin'`
	linux=`echo ${arch} | grep 'Linux'`

	if [ "${darwin}" != "" ]; then

		os_type='OSX'
		os_version=`/usr/bin/sw_vers -productVersion`

	elif [ "${linux}" != "" ]; then

		dist=`cat /etc/*-release`
		
		centos=`echo ${dist} | grep 'CentOS'`
		debian=`echo ${dist} | grep 'Debian'`
		fedora=`echo ${dist} | grep 'Fedora'`
		redhat=`echo ${dist} | grep 'Red Hat'`
		suse=`echo ${dist} | grep 'SUSE'`	
		ubuntu=`echo ${dist} | grep 'Ubuntu'`

		  if [ "${centos}" != "" ]; then os_type='CentOS'
		elif [ "${debian}" != "" ]; then os_type='Debian'
		elif [ "${fedora}" != "" ]; then os_type='Fedora'
		elif [ "${redhat}" != "" ]; then os_type='RedHat'
		elif [ "${suse}" != "" ]; then os_type='SuSE'
		elif [ "${ubuntu}" != "" ]; then os_type='Ubuntu'
		fi

		  if [ "${os_type}" != "" ]; then os_version=`echo ${dist} | sed s/'[a-zA-Z]'//g | sed s/'[_=,\/]'/' '/g | awk '{print $1}'`
		else os_type='Linux'; os_version='NA'
		fi	
	fi

	if [ "${os_display}" = "type" ]; 	then echo "${os_type}"; fi
	if [ "${os_display}" = "version" ]; then echo "${os_version}"; fi
	if [ "${os_display}" = "" ]; 		then echo "${os_type} ${os_version}"; fi
}

password_entry() {

	if [ "$2" ]; then phrase="$2"
	else phrase="Password"
	fi

	/bin/echo -n "${phrase}: "; read -s password_entry
	
	if [ "$1" = "1" ]; then password=${password_entry}; password_entry=''; fi
}


php_version() {

	php_version=`$1 -r 'echo phpversion();' 2> /dev/null`
	php_version=`echo ${php_version} | sed s/'-'/' '/g | awk '{print $1}'` 

	echo ${php_version}
}

php_cli () {

	if [ -f "/etc/php.ini.cli" ]; then 								# LEGACY: OS X 10.4

		php_cli="/opt/local/bin/php"
		php_version=`/opt/local/bin/php -r 'echo phpversion();'`

	elif [ -f "/usr/bin/php" ]; then

		php_cli="/usr/bin/php"
		php_version=`/usr/bin/php -r 'echo phpversion();'`

	elif [ -f "/opt/local/bin/php" ]; then							# LEGACY: OS X 10.4
		
		php_cli="/opt/local/bin/php"
		php_version=`/opt/local/bin/php -r 'echo phpversion();'`

	else

		php_cli=`which php`
		php_version=`${php_cli} -r 'echo phpversion();'`
	fi

	echo ${php_cli}
}

binaries () {

    bin_list="  apachectl \
                apache2ctl \
                chgrp \
                chmod \
                chown \
                genaliases \
                grep \
                mailmanctl \
                mysql \
                mysqldump \
                perl \
                postfix \
                postmap \
                sed"

    for bin in ${bin_list}
    do
        eval bin_${bin}=`which ${bin}`
    done

      if [ "$1" = "all" ];  then /bin/echo ${bin_list}
    elif [ "$1" != "" ];    then eval bin_select=\$bin_$1; /bin/echo ${bin_select}
    fi
}

mysql_version () {

	mysql_version=`mysql --version | sed s/','//g | awk '{print $5}'`

	echo ${mysql_version}
}

root_access() {

	who_am_i=`/usr/bin/whoami`
	if [ ${who_am_i} != "root" ]; then /bin/echo "Requires root access..."; /bin/echo "Exiting..."; /bin/echo ; sleep 2; exit;  fi
}

os_access() {

	grep_path=`which grep`
	
	os_list=$1
	os_current=`/bin/echo $2 | tr [A-Z] [a-z]`
	
	match=`/bin/echo "${os_list}" | ${grep_path} "${os_current}"`
	
	if [ "${match}" = "" ]; then /bin/echo "OS not supported..."; /bin/echo "Exiting..."; /bin/echo ; sleep 2; exit;  fi
}

terminate_shell() {

	/bin/echo -n "Finishing...";
	sleep 1

	/bin/echo
        if [ "$1" = "1" ]; then read -p "Press Enter to Continue..."; fi

	/bin/echo "Done!"
	sleep 1
	
	clear
}

$1 $2 $3 2>/dev/null
