#!/bin/sh

###############################################################################
# setup.sh
# DirectAdmin  setup.sh  file  is  the  first  file  to  download  when doing a
# DirectAdmin Install.   It  will  ask  you  for  relevant information and will 
# download  all  required  files.   If  you  are unable to run this script with
# ./setup.sh  then  you probably need to set it's permissions.  You can do this
# by typing the following:
#
# chmod 755 setup.sh
#
# after this has been done, you can type ./setup.sh to run the script.
#
###############################################################################

OS=`uname`;

ADMIN_USER=admin
DB_USER=da_admin
ADMIN_PASS=`perl -le'print map+(A..Z,a..z,0..9)[rand 62],0..7'`;
DB_ROOT_PASS=`perl -le'print map+(A..Z,a..z,0..9)[rand 62],0..7'`;

FTP_HOST=files.directadmin.com
if [ "$OS" = "FreeBSD" ]; then
	WGET_PATH=/usr/local/bin/wget
else
	WGET_PATH=/usr/bin/wget
fi
WGET_10=`$WGET_PATH -V 2>/dev/null | head -n1 | grep -c 1.10`
WGET_OPTION="";
if [ $WGET_10 -eq 1 ]; then
	WGET_OPTION="--no-check-certificate";
fi


CID=0;
LID=0;
HOST=`hostname`;
CMD_LINE=0;
ETH_DEV=eth0;
IP=0

if [ $# -gt 0 ]; then
case "$1" in
	--help|help|\?|-\?|h)
		echo "";
		echo "Usage: $0";
		echo "";
		echo "or";
		echo "";
		echo "Usage: $0 <uid> <lid> <hostname> <ethernet_dev> (<ip>)";
		echo "          <uid> : Your Client ID";
		echo "          <lid> : Your License ID";
		echo "     <hostname> : Your server's hostname (FQDN)";
		echo " <ethernet_dev> : Your ethernet device with the server IP";
		echo "           <ip> : Optional.  Use to override the IP in <ethernet_dev>";
		echo "";
		exit 0;
		;;
esac
	CID=$1;
	LID=$2;
	HOST=$3;
	if [ $# -lt 4 ]; then
		$0 --help
		exit 56;
	fi

	ETH_DEV=$4;
	CMD_LINE=1;
	if [ $# -gt 4 ]; then
		IP=$5;
	fi
fi

B64=0
if [ -e /usr/lib64 ]; then
	#echo "";
	#echo "";
	echo "*** 64-bit OS ***";
	echo "*** that being said, this should be a CentOS system. ***";
	echo "*** We do not support any other OS in 64-bit.        ***";
	echo "*** If this is not CentOS, install a 32-bit OS (i386)***";
	sleep 2;
	B64=1	
fi

if [ -e /usr/local/directadmin ]; then
	echo "";
	echo "";
	echo "*** DirectAdmin already exists ***";
	echo "    Press Ctrl-C within the next 10 seconds to cancel the install";
	echo "    Else, wait, and the install will continue, but will destroy existing data";
	echo "";
	echo "";
	sleep 10;
fi

if [ -e /usr/local/cpanel ]; then
        echo "";
        echo "";
        echo "*** CPanel exists on this system ***";
        echo "    Press Ctrl-C within the next 10 seconds to cancel the install";
        echo "    Else, wait, and the install will continue overtop (as best it can)";
        echo "";
        echo "";
        sleep 10;
fi

OS_VER=;

REDHAT_RELEASE=/etc/redhat-release
DEBIAN_VERSION=/etc/debian_version
DA_PATH=/usr/local/directadmin
SCRIPTS_PATH=$DA_PATH/scripts
PACKAGES=$SCRIPTS_PATH/packages
SETUP=$SCRIPTS_PATH/setup.txt

SERVER=http://files.directadmin.com/services

if [ $OS = "FreeBSD" ]; then
	OS_VER=`uname -r | cut -d- -f1`
elif [ -e /etc/fedora-release ]; then
	OS=fedora
	OS_VER=`cat /etc/fedora-release | cut -d\  -f4`
	if [ "$OS_VER" = "(Moonshine)" ]; then
		OS_VER=`cat /etc/fedora-release | cut -d\  -f3`
	fi
        if [ "$OS_VER" = "(Werewolf)" ]; then
                OS_VER=`cat /etc/fedora-release | cut -d\  -f3`
        fi
elif [ -e /etc/whitebox-release ]; then
	if [ ! -e /etc/redhat-release ]; then
		ln -s /etc/whitebox-release /etc/redhat-release
	fi
	OS_VER=`cat /etc/redhat-release | cut -d\  -f5`
elif [ -e $DEBIAN_VERSION ]; then
	OS=debian
	OS_VER=`cat $DEBIAN_VERSION | head -n1`
	if [ "$OS_VER" = "testing/unstable" ]; then
		OS_VER=3.1
	fi
	if [ "$OS_VER" = "lenny/sid" ]; then
		OS_VER=4.0
	fi
else
	OS_VER=`cat /etc/redhat-release | cut -d\  -f5`
fi

if [ "$OS_VER" = "release" ]; then
	OS=Enterprise
	OS_VER=`cat /etc/redhat-release | cut -d\  -f6`
elif [ "$OS_VER" = "ES" ]; then
	OS=Enterprise
	OS_VER=`cat /etc/redhat-release | cut -d\  -f7`
elif [ "$OS_VER" = "WS" ]; then
	OS=Enterprise
	OS_VER=`cat /etc/redhat-release | cut -d\  -f7`
elif [ "$OS_VER" = "AS" ]; then
	OS=Enterprise
	OS_VER=`cat /etc/redhat-release | cut -d\  -f7`
elif [ "$OS_VER" = "Server" ]; then
	OS=Enterprise
	OS_VER=`cat /etc/redhat-release | cut -d\  -f7`
elif [ "`cat /etc/redhat-release 2>/dev/null| cut -d\  -f1`" = "CentOS" ]; then
	OS=Enterprise
	OS_VER=`cat /etc/redhat-release |cut -d\  -f3`;
elif [ "`cat /etc/redhat-release 2>/dev/null| cut -d\  -f3`" = "Enterprise" ]; then
	OS=Enterprise
	OS_VER=`cat /etc/redhat-release 2>/dev/null| cut -d\  -f7`
fi

# Get the services file name:
# services72.tar.gz
# services73.tar.gz
# services80.tar.gz
# services90.tar.gz
# services_freebsd48.tar.gz

SERVICES="";

if [ "$OS" = "fedora" ]; then

	case "$OS_VER" in
        	1|1.90) SERVICES=services_fedora1.tar.gz
                	;;
	        2|2.0) SERVICES=services_fedora2.tar.gz
			;;
		3|3.0) SERVICES=services_fedora3.tar.gz
			;;
		4|4.0) SERVICES=services_fedora4.tar.gz
			;;
		5|5.0) SERVICES=services_fedora5.tar.gz
			;;
		6|6.0) SERVICES=services_fedora6.tar.gz
			;;
                7|7.0|8|8.0) SERVICES=services_fedora7.tar.gz
                        ;;

	esac

elif [ "$OS" = "debian" ]; then

	case "$OS_VER" in
		3.0|3) SERVICES=services_debian30.tar.gz
			;;
		3.1) SERVICES=services_debian31.tar.gz
			;;
		*) SERVICES=services_debian31.tar.gz
			;;
	esac

elif [ "$OS" = "FreeBSD" ]; then

	case "$OS_VER" in
                4.8|4.9|4.10|4.11) SERVICES=services_freebsd48.tar.gz
                        ;;
                5.0|5.1|5.2|5.2.1|5.3|5.4|5.5) SERVICES=services_freebsd51.tar.gz
			;;
		6.0|6.1|6.2|6.3) SERVICES=services_freebsd60.tar.gz
			;;
		7|7.0) SERVICES=services_freebsd70.tar.gz
			;;
	esac
elif [ $B64 -eq 1 ]; then
	if [ "$OS" = "debian" ]; then
		echo "*********************";
		echo "* 64-bit Debian";
		echo "* Note: we do no support 64-bit debian. Please install a 32-bit OS version of debian.";
		echo "* press ctrl-c to abort the install.";
		echo "*********************";
		sleep 10;
	fi

	case "$OS_VER" in
		4.0|4.1|4.2|4.3|4.4|4.5|4.6) SERVICES=services_es41_64.tar.gz
			;;
		5|5.0|5.1) SERVICES=services_es50_64.tar.gz
			;;
	esac
else

	case "$OS_VER" in
		7.2) SERVICES=services72.tar.gz
			;;
		7.3) SERVICES=services73.tar.gz
                	;;
		8.0) SERVICES=services80.tar.gz
	                ;;
		9|9.0) SERVICES=services90.tar.gz
                	;;
		2.1|3|3.0|3.1|3.3|3.4|3.5|3.6|3.7|3.8|3.9) SERVICES=services_es30.tar.gz
			;;
		4|4.0|4.1|4.2|4.3|4.4|4.5|4.6) SERVICES=services_es40.tar.gz
			;;
		5|5.0|5.1) SERVICES=services_es50.tar.gz
			;;
	esac

fi

if [ "$SERVICES" = "" ]; then

	yesno="n";
	while [ $yesno = "n" ];
	do
	{

		echo "";
		echo "*** Unable to determine which services pack to use ***";
		echo "";
		echo "Please type in the file name closest to your system from the following list:";
		echo "";
		echo "Redhat:";
		echo "  services72.tar.gz";
		echo "  services73.tar.gz";
		echo "  services80.tar.gz";
		echo "  services90.tar.gz";
		echo "";
		echo "Fedora:";
		echo "  services_fedora1.tar.gz";
		echo "  services_fedora2.tar.gz";
		echo "  services_fedora3.tar.gz";
		echo "  services_fedora4.tar.gz";
		echo "  services_fedora5.tar.gz";
		echo "  services_fedora6.tar.gz";
		echo "  services_fedora7.tar.gz";
		echo "";
		echo "Enterprise/Whitebox/CentOS:";
		echo "  services_es30.tar.gz";
		echo "  services_es40.tar.gz";
		echo "  services_es50.tar.gz";
		echo "  services_es41_64.tar.gz";
		echo "  services_es50_64.tar.gz";
		echo "";
		echo "FreeBSD:";
		echo "  services_freebsd48.tar.gz";
		echo "  services_freebsd49.tar.gz";
		echo "  services_freebsd51.tar.gz";
		echo "  services_freebsd60.tar.gz";
		echo "";
		echo "Debian:";
		echo "  services_debian30.tar.gz";
		echo "  services_debian31.tar.gz";
		echo "";
	
		echo -n "Type the filename: ";
		read SERVICES
	
		echo "";
		echo "Value entered: $SERVICES";
	
	        echo -n "Is this correct? (y,n) : ";
	        read yesno;
	}
	done;

fi

/bin/mkdir -p $PACKAGES

yesno="n";
if [ $CMD_LINE -eq 1 ]; then
	yesno="y";
fi
while [ $yesno = "n" ];
do
{
	echo -n "Please enter your Client ID : ";
	read CID;

	echo -n "Please enter your License ID : ";
	read LID;

	echo -e "Please enter your hostname \(server.domain.com\)";
	echo "It must be a Fully Qualified Domain Name";
	echo "Do *not* use a domain you plan on using for the hostname:";
	echo "eg. don't use domain.com. Use server.domain.com instead.";
	echo "Do not enter http:// or www";
	echo "";
	echo -n "Enter your hostname (FQDN) : ";
	read HOST;

	echo "Client ID:  $CID";
	echo "License ID: $LID";
	echo "Hostname: $HOST";
	echo -n "Is this correct? (y,n) : ";
	read yesno;
}
done;


############

# Get the other info
EMAIL=${ADMIN_USER}@${HOST}

TEST=`echo $HOST | cut -d. -f3`
if [ "$TEST" = "" ]
then
        NS1=ns1.`echo $HOST | cut -d. -f1,2`
        NS2=ns2.`echo $HOST | cut -d. -f1,2`
else
        NS1=ns1.`echo $HOST | cut -d. -f2,3,4,5,6`
        NS2=ns2.`echo $HOST | cut -d. -f2,3,4,5,6`
fi

## Get the ethernet_dev


if [ $OS = "FreeBSD" ]; then

	if [ $CMD_LINE -eq 0 ]; then

		DEVS=`/sbin/ifconfig -a | grep -e "^[a-z]" | cut -d: -f1 |grep -v lp0|grep -v lo0|grep -v tun0|grep -v sl0|grep -v ppp0|grep -v faith0`

		COUNT=0;
		for i in $DEVS; do
		{
			COUNT=$(($COUNT+1));
		};
		done;

		if [ $COUNT -eq 0 ]; then
        		echo "Could not find your ethernet device.";
	        	echo -n "Please enter the name of your ethernet device: ";
	        	read ETH_DEV;
		elif [ $COUNT -eq 1 ]; then
        		echo -n "Is $DEVS your network adaptor with the license IP? (y,n) : ";
		        read yesno;
        		if [ "$yesno" = "n" ]; then
                		echo -n "Enter the name of the ethernet device you wish to use : ";
		                read ETH_DEV;
		        else
	        	        ETH_DEV=$DEVS
		        fi
		else
	        	# more than one
		        echo "The following ethernet devices were found. Please enter the name of the one you wish to use:";
		        echo "";
		        echo $DEVS;
		        echo "";
		        echo -n "Enter the device name: ";
		        read ETH_DEV;
		fi
	fi

	echo "Using $ETH_DEV";

	if [ "$IP" = "0" ]; then

		IP=`/sbin/ifconfig $ETH_DEV | grep 'inet ' | head -n1 | cut -d\  -f2`
	fi

	echo "Using $IP";

	NM_HEX=`/sbin/ifconfig $ETH_DEV | grep 'inet ' | head -n1 | cut -d\  -f4 | cut -dx -f2 | tr '[a-f]' '[A-F]'`

	NMH1=`echo $NM_HEX | awk '{print substr($1,1,2)}'`
	NMH2=`echo $NM_HEX | awk '{print substr($1,3,2)}'`
	NMH3=`echo $NM_HEX | awk '{print substr($1,5,2)}'`
	NMH4=`echo $NM_HEX | awk '{print substr($1,7,2)}'`

	NM1=`echo "ibase=16; $NMH1" | bc`
	NM2=`echo "ibase=16; $NMH2" | bc`
	NM3=`echo "ibase=16; $NMH3" | bc`
	NM4=`echo "ibase=16; $NMH4" | bc`
	
	NM=$NM1.$NM2.$NM3.$NM4;

else
	if [ $CMD_LINE -eq 0 ]; then
	        echo -n "Is $ETH_DEV your network adaptor with the license IP? (y,n) : ";
        	read yesno;
	        if [ "$yesno" = "n" ]; then
        	        echo -n "Enter the name of the ethernet device you wish to use : ";
                	read ETH_DEV;
	        fi
	fi

	if [ "$IP" = "0" ]; then
		IP=`/sbin/ifconfig $ETH_DEV | grep 'inet addr:' | cut -d: -f2 | cut -d\  -f1`;
	fi

	NM=`/sbin/ifconfig $ETH_DEV | grep 'Mask:' | cut -d: -f4`;	
fi

if [ $CMD_LINE -eq 0 ]; then

	if [ "$IP" = "" ]; then
		yesno="n";
	else
		echo -n "Is $IP the IP in your license? (y,n) : ";
		read yesno;
	fi

	if [ "$yesno" = "n" ]; then
		echo -n "Enter the IP used in your license file : ";
		read IP;
	fi

	if [ "$IP" = "" ]; then
		echo "The IP entered is blank.  Please try again, and enter a valid IP";
	fi
fi

############

echo "";
echo "DirectAdmin will now be installed on: $OS $OS_VER";

if [ $CMD_LINE -eq 0 ]; then
	echo -n "Is this correct? (must match license) (y,n) : ";
	read yesno;

	if [ "$yesno" = "n" ]; then
		echo -e "\nPlease change the value in your license, or install the correct operating system\n";
		exit 1;
	fi
fi


################

if [ $CMD_LINE -eq 0 ]; then
        echo "";
        echo "You now have 2 options for your apache/php setup.";
        echo "";
        echo "1: customapache: older, more tested. Includes Apache 1.3, php 4 and frontpage.";
        echo "2: custombuild 1.1:  newer, less tested. Includes any Apache version, php 4, 5, or both in cli and/or suphp. Frontpage not available with Apache 2.x.";
	#echo "3: custombuild 1.2 BETA:  newer, less tested. Includes any Apache version, php 5, 6, or both in cli and/or suphp. Frontpage not available with Apache 2.x.";
	echo "                 Post any issues with custombuild to the forum: http://www.directadmin.com/forum/forumdisplay.php?f=61";
        echo "";
        echo -n "Enter your choice (1 or 2): ";
	#echo -n "Enter your choice (1, 2 or 3): ";

        read onetwo;

        if [ "$onetwo" = "2" ]; then
                echo "You have chosen custombuild 1.1.";
		echo "1.1" > /root/.custombuild

                echo "";
                echo -n "Would you like the default settings of apache 2.2 and php 5 cli? (y/n): ";
                read yesno;
                if [ "$yesno" = "n" ]; then
                        echo "You have chosen to customize the custombuild options.  Please wait while options configurator is downloaded... ";
                        echo "";

                        CBPATH=$DA_PATH/custombuild
                        mkdir -p $CBPATH
                        if [ $OS = "FreeBSD" ]; then
                                fetch -o $CBPATH/options $SERVER/custombuild/options-1.1
                        else
                                $WGET_PATH -O $CBPATH/options $SERVER/custombuild/options-1.1
                        fi

                        if [ -e $CBPATH/options ]; then
                                chmod 755 $CBPATH/options
                                $CBPATH/options
                        else
                                echo "unable to download the options file.  Using defaults instead.";
                        fi
                else
                        echo "Using the default settings for custombuild.";
                fi

        elif [ "$onetwo" = "3" ]; then
		echo "You have chosen custombuild 1.2.";
		echo "1.2" > /root/.custombuild

		echo "";
		echo -n "Would you like the default settings of apache 2.2 and php 6 cli? (y/n): ";
		read yesno;
		if [ "$yesno" = "n" ]; then
			echo "You have chosen to customize the custombuild options.  Please wait while options configurator is downloaded... ";
			echo "";

			CBPATH=$DA_PATH/custombuild
			mkdir -p $CBPATH
			if [ $OS = "FreeBSD" ]; then
				fetch -o $CBPATH/options $SERVER/custombuild/options-1.2
			else
				$WGET_PATH -O $CBPATH/options $SERVER/custombuild/options-1.2
			fi

			if [ -e $CBPATH/options ]; then
				chmod 755 $CBPATH/options
				$CBPATH/options
			else
				echo "unable to download the options file. Using defaults instead.";
			fi
		else
			echo "Using the default settings for custombuild.";
		fi

        else
                echo "Using customapache with apache 1.3 and php 4.";
        fi

        sleep 2
fi

##########

echo "beginning pre-checks, please wait...";

# Things to check for:
#
# bison
# flex
# webalizer
# bind (named)
# patch
# openssl-devel
# wget

BIN_DIR=/usr/bin
LIB_DIR=/usr/lib
if [ $OS = "FreeBSD" ]; then
	BIN_DIR=/usr/local/bin
	LIB_DIR=/usr/local/lib
fi

checkFile()
{
        if [ -e $1 ]; then
                echo 1;
        else
                echo 0;
        fi
}

if [ $OS = "FreeBSD" ]; then
        PERL=`pkg_info | grep -ce '^perl'`;
else
        PERL=`checkFile /usr/bin/perl`;
fi
BISON=`checkFile $BIN_DIR/bison`;
FLEX=`checkFile /usr/bin/flex`;
WEBALIZER=`checkFile $BIN_DIR/webalizer`;
BIND=`checkFile /usr/sbin/named`;
PATCH=`checkFile /usr/bin/patch`;
SSL_DEVEL=`checkFile /usr/include/openssl/ssl.h`;
WGET=`checkFile $BIN_DIR/wget`;
WGET_PATH=$BIN_DIR/wget;
KRB5=`checkFile /usr/kerberos/include/krb5.h`;
KILLALL=`checkFile /usr/bin/killall`;
if [ $KRB5 -eq 0 ]; then
	KRB5=`checkFile /usr/include/krb5.h`;
fi
if [ $OS = "FreeBSD" ]; then
	GD=`checkFile $LIB_DIR/libgd.so.2`;
else
	GD=`checkFile $LIB_DIR/libgd.so.1.8.4`;
fi
E2FS=1;
E2FS_DEVEL=1;
if [ -e /etc/fedora-release ]; then
	E2FS=`checkFile /lib/libcom_err.so.2`;
	E2FS_DEVEL=`checkFile /usr/include/et/com_err.h`;
fi
if [ "$OS" = "Enterprise" ]; then
	if [ $B64 -eq 1 ]; then
		E2FS=`checkFile /lib64/libcom_err.so.2`;
	else
	        E2FS=`checkFile /lib/libcom_err.so.2`;
	fi
        E2FS_DEVEL=`checkFile /usr/include/et/com_err.h`;
fi



###############################################################################
###############################################################################

# We now have all information gathered, now we need to start making decisions

if [ "$OS" = "debian" ]; then
	if [ -e /bin/bash ] && [ -e /bin/dash ]; then
		COUNT=`ls -la /bin/sh | grep -c dash`
		if [ "$COUNT" -eq 0 ]; then
			ln -sf bash /bin/sh
		fi
	fi
fi

if [ "$OS" = "debian" ] && [ "$OS_VER" = "3.0" ]; then
	COUNT=`cat /etc/apt/sources.list |grep backports |grep -c debconf`
	if [ "$COUNT" -eq 0 ]; then
		echo "deb http://www.backports.org/debian/ woody debconf" >> /etc/apt/sources.list
	fi
fi

if [ $WGET -eq 0 ]; then
	if [ "$OS" = "FreeBSD" ]; then
		echo "wget not found: Attempting to install wget ... ";
		case "$OS_VER" in
			7.0) pkg_add -r http://$FTP_HOST/services/packages-7-stable/Latest/wget.tbz
				;;
			6.0|6.1|6.2|6.3) pkg_add -r http://$FTP_HOST/services/packages-6-stable/Latest/wget.tbz
				;;
			5.3|5.4|5.5) pkg_add -r http://$FTP_HOST/services/packages-5.3-release/Latest/wget.tbz
				;;
			5.2.1) pkg_add -r http://$FTP_HOST/services/packages-5.2.1-release/Latest/wget.tbz
				;;
			4.8|4.9) pkg_add -r http://$FTP_HOST/services/packages-4-stable/Latest/wget.tgz
				;;
			4.10|4.11) pkg_add -r http://$FTP_HOST/services/packages-4.10-release/Latest/wget.tgz
				;;
			*) pkg_add -r http://$FTP_HOST/services/packages-5-stable/Latest/wget.tbz
				;;
		esac
	elif [ "$OS" = "debian" ]; then
		echo "wget not found: Attempting to install wget ... ";
		apt-get install wget

		#the default wget from apt-get doesn't have https support
		if [ -e $WGET_PATH ]; then
			$WGET_PATH -O $WGET_PATH.new $SERVER/debian_${OS_VER}/wget
			mv -f $WGET_PATH $WGET_PATH.old
			mv -f $WGET_PATH.new $WGET_PATH
			chmod 755 $WGET_PATH
		fi			
	else
		echo "*** wget not found: you *must* install wget (yum -y install wget)";
		exit 2;
	fi

	WGET_10=`$WGET_PATH -V 2>/dev/null | head -n1 | grep -c 1.10`
	WGET_OPTION="";
	if [ $WGET_10 -eq 1 ]; then
	        WGET_OPTION="--no-check-certificate";
	fi


fi


# Download the file that has the paths to all the relevant files.
FILES=$SCRIPTS_PATH/files.sh
if [ "$OS" != "FreeBSD" ]; then	
	FILES_PATH=$OS_VER
	if [ "$OS" = "debian" ]; then
		FILES_PATH=debian_${OS_VER}
	elif [ "$OS" = "fedora" ]; then
	        case "$OS_VER" in
        	        1|1.90) FILES_PATH=fedora_1
                	        ;;
	                2|2.0) FILES_PATH=fedora_2
        	                ;;
                	3|3.0) FILES_PATH=fedora_3
				;;
			4|4.0) FILES_PATH=fedora_4
				;;
			5|5.0) FILES_PATH=fedora_5
				;;
			6|6.0) FILES_PATH=fedora_6
				;;
			7|7.0) FILES_PATH=fedora_7
				;;
			8|8.0) FILES_PATH=fedora_8
				;;
	        esac
	elif [ $B64 -eq 1 ]; then
		case "$OS_VER" in
			4|4.0|4.1|4.2|4.3|4.4|4.5|4.6) FILES_PATH=es_4.1_64
				;;
			5|5.0|5.1) FILES_PATH=es_5.0_64
				;;
		esac
	elif [ "$OS" = "Enterprise" ]; then
		if [ "$OS_VER" = "5" ] || [ "$OS_VER" = "5.1" ]; then
			FILES_PATH=es_5.0
		fi
	fi

	wget -O $FILES $SERVER/$FILES_PATH/files.sh
	if [ ! -e $FILES ]; then
		echo "*** Unable to download files.sh";
		exit 3;
	fi
	chmod 755 $FILES;
	. $FILES
fi



addPackage()
{
	echo "adding $1 ...";
	if [ "$OS" = "FreeBSD" ]; then

                case "$OS_VER" in
			7|7.0) pkg_add -r http://$FTP_HOST/services/packages-7-stable/Latest/${1}.tbz
				;;
			6.0|6.1|6.2|6.3) pkg_add -r http://$FTP_HOST/services/packages-6-stable/Latest/${1}.tbz
				;;
                        5.3|5.4|5.5) pkg_add -r http://$FTP_HOST/services/packages-5.3-release/Latest/${1}.tbz
                                ;;
			5.2.1) pkg_add -r http://$FTP_HOST/services/packages-5.2.1-release/Latest/${1}.tbz
				;;
                        4.8|4.9) pkg_add -r http://$FTP_HOST/services/packages-4-stable/Latest/${1}.tgz
                                ;;
			4.10|4.11) pkg_add -r http://$FTP_HOST/services/packages-4.10-release/Latest/${1}.tgz
				;;
                        *) pkg_add -r http://$FTP_HOST/services/packages-5-stable/Latest/${1}.tbz
                                ;;
                esac
	elif [ "$OS" = "debian" ]; then

		if [ "$3" = "0" ]; then
			return;
		fi

		if [ "$3" = "" ]; then
			apt-get -y install $1
		else
			apt-get -y install $3
		fi
	else

		if [ "$2" = "" ]; then
			echo "";
			echo "*** the value for $1 is empty.  It needs to be added manually ***"
			echo "";
			return;
		fi

		wget -O $PACKAGES/$2 $SERVER/$FILES_PATH/$2
		if [ ! -e $PACKAGES/$2 ]; then
			echo "Error downloading $SERVER/$FILES_PATH/$2";
		fi
		
		rpm -Uvh --nodeps --force $PACKAGES/$2
	fi
}

#######
# Ok, we're ready to go.

if [ $PERL -eq 0 ]; then
	case "$OS_VER" in
		5.3|5.4|5.5|6.0|6.1|6.2|6.3|7|7.0)	addPackage perl $perl;
			;;
		5.0|5.1|5.2)		addPackage perl5.6 $perl;
			;;
		*)			addPackage perl5.8 $perl;
			;;
        esac
	rehash
	use.perl port 2> /dev/null > /dev/null
	ADMIN_PASS=`/usr/bin/perl -le'print map+(A..Z,a..z,0..9)[rand 62],0..7'`;
	DB_ROOT_PASS=`/usr/bin/perl -le'print map+(A..Z,a..z,0..9)[rand 62],0..7'`;

fi

if [ ! -e /usr/bin/perl ]; then
	ln -s /usr/local/bin/perl /usr/bin/perl
fi

#this bit is for exim and fedora 1.
if [ "$OS_VER" = "1" ]; then
	echo "/usr/lib/perl5/5.8.1/i386-linux-thread-multi/CORE" >> /etc/ld.so.conf
	ldconfig
fi

if [ ! -e /etc/ld.so.conf ] || [ "`grep -c -E '/usr/local/lib$' /etc/ld.so.conf`" = "0" ]; then
        echo "/usr/local/lib" >> /etc/ld.so.conf
        ldconfig
fi

if [ $BISON -eq 0 ]; then
	addPackage bison $bison
fi

if [ $FLEX -eq 0 ]; then
	#flex doesn't exist for pkg_add on FreeBSD...
        addPackage flex $flex
fi

if [ $GD -eq 0 ]; then
	addPackage gd $gd
fi

if [ $WEBALIZER -eq 0 ]; then

	WEBALIZER_FILE=/usr/bin/webalizer

	if [ "$OS" = "FreeBSD" ]; then
		WEBALIZER_FILE=/usr/local/bin/webalizer
	
		case "$OS_VER" in
		        4.8|4.9|4.10|4.11) wget -O $WEBALIZER_FILE $SERVER/freebsd4.8/webalizer
                		;;
		        5.0|5.1|5.2|5.2.1|5.3|5.4|5.5) wget -O $WEBALIZER_FILE $SERVER/freebsd5.1/webalizer
				;;
		esac

	else

		wget -O $WEBALIZER_FILE $SERVER/${filesh_path}/webalizer
	fi

	chmod 755 $WEBALIZER_FILE
fi

if [ $BIND -eq 0 ]; then
	addPackage bind-utils $bind_utils 0
        addPackage bind $bind bind9
	addPackage bind-libs $bind_libs 0
fi
if [ "$OS" != "FreeBSD" ] && [ "$OS" != "debian" ]; then
	mv -f /etc/init.d/named /etc/init.d/named.back
	wget -O /etc/init.d/named http://www.directadmin.com/named
	chmod 755 /etc/init.d/named
	/sbin/chkconfig named reset

        RNDCKEY=/etc/rndc.key
        if [ -e $RNDCKEY ]; then
                if [ `cat $RNDCKEY | grep -c secret` -eq 0 ]; then
                        SECRET=`/usr/sbin/rndc-confgen | grep secret | head -n 1`
                        STR="perl -pi -e 's#hmac-md5;#hmac-md5;\n\t$SECRET#' $RNDCKEY;"
                        eval $STR;
                fi
        fi
fi

if [ "$OS" = "FreeBSD" ]; then
	if [ ! -e /etc/namedb/rndc.key ]; then
		rndc-confgen -a -s $IP	
	fi
	COUNT=`cat /etc/namedb/named.conf | grep -c listen`
	if [ $COUNT -ne 0 ]; then
		wget -O /etc/namedb/named.conf http://www.directadmin.com/named.conf.freebsd
	fi
fi

if [ "$OS" = "debian" ]; then
	if [ ! -e /etc/init.d/named ]; then
		if [ -e /etc/init.d/bind ]; then
			ln -s bind /etc/init.d/named
		else
			ln -s bind9 /etc/init.d/named
		fi
	fi
	if [ ! -e /bin/nice ]; then
		ln -s /usr/bin/nice /bin/nice
	fi
	
	if [ "$KILLALL" -eq 0 ]; then
		addPackage psmisc nothing psmisc
	fi
fi

if [ -e /etc/sysconfig/named ]; then
        /usr/bin/perl -pi -e 's/^ROOTDIR=.*/ROOTDIR=/' /etc/sysconfig/named
fi


if [ $PATCH -eq 0 ]; then
        addPackage patch $patch
fi

if [ $SSL_DEVEL -eq 0 ]; then
        addPackage openssl $openssl
	addPackage openssl-devel $openssl_devel libssl-dev
fi

if [ $OS != "FreeBSD" ]; then
	groupadd apache >/dev/null 2>&1
	if [ "$OS" = "debian" ]; then
		useradd -d /var/www -g apache -s /bin/false apache >/dev/null 2>&1
	else
		useradd -d /var/www -g apache -r -s /bin/false apache >/dev/null 2>&1
	fi
	mkdir -p /etc/httpd/conf >/dev/null 2>&1

	if [ $KRB5 -eq 0 ]; then
		addPackage krb5-libs $krb5_libs libkrb53
		addPackage krb5-devel $krb5_devel libkrb5-dev
	fi
fi

#this is for exim.
if [ "$OS" = "fedora" ] && [ "$OS_VER" = "8" ]; then
	if [ -e /lib/libdb-4.6.so ] && [ ! -e /lib/libdb-4.5.so ]; then
		ln -s libdb-4.6.so /lib/libdb-4.5.so
	fi

	if [ ! -e /usr/lib/mysql/libmysqlclient.so ] && [ -e /usr/lib/libmysqlclient.so ]; then
		ln -s /usr/lib/libmysqlclient.so /usr/lib/mysql/libmysqlclient.so
	fi

fi

if [ $OS = "FreeBSD" ]; then
	if [ ! -e /usr/lib/libc.so.4 ]; then
		pkg_add -r compat4x >/dev/null 2>&1
	fi

	if [ -e /lib/libm.so.3 ]; then
		if [ ! -e /lib/libm.so.2 ]; then
			ln -s libm.so.3 /lib/libm.so.2
		fi
	fi
fi

if [ $E2FS -eq 0 ]; then
	addPackage e2fsprogs $e2fsprogs	0
fi

if [ $E2FS_DEVEL -eq 0 ]; then
	addPackage e2fsprogs-devel $e2fsprogs_devel 0
fi

if [ $B64 -eq 1 ]; then
	COUNT=`rpm -qa | grep -c perl-DBI`
	if [ $COUNT -eq 0 ]; then
		addPackage perl-DBI $perl_dbi 0
	fi
fi

if [ "$OS" = "debian" ] && [ ! -e /sbin/quotacheck ]; then
	echo "Couldn't find quotas. Installing them.";
	addPackage quota $quota quota
fi

if [ -e /etc/selinux/config ]; then
	perl -pi -e 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
	perl -pi -e 's/SELINUX=permissive/SELINUX=disabled/' /etc/selinux/config
fi

if [ -e /selinux/enforce ]; then
	echo "0" > /selinux/enforce
fi

if [ -e /usr/bin/rdate ]; then
	/usr/bin/rdate -s time-b.nist.gov
fi

#test and make sure wget works on debian.
if [ "$OS" = "debian" ]; then

	$BIN_DIR/wget -O /dev/null https://www.directadmin.com
	RET=$?
	if [ $RET -eq 1 ]; then

		echo "*******************";
		echo "";
		echo "wget does not support https.  Downloading a new wget for you from http://files.directadmin.com/services/debian_${OS_VER}/wget";
		echo "";
		echo "*******************";

		$BIN_DIR/wget -O $BIN_DIR/wget2 http://files.directadmin.com/services/debian_${OS_VER}/wget
		RET=$?
		if [ $RET -eq 0 ]; then
			mv $BIN_DIR/wget $BIN_DIR/wget.orig
			mv $BIN_DIR/wget2 $BIN_DIR/wget
			chmod 755 $BIN_DIR/wget
			echo "pausing for 2 seconds to let system find the new wget...";
			sleep 2
		else

			echo "";
			echo "wget does not appear to be functioning with https.";
			echo "run the following to get a new wget binary:";
			echo "  cd /usr/bin";
			echo "  wget -O wget2 http://files.directadmin.com/services/debian_${OS_VER}/wget";
			echo "  mv wget wget.orig";
			echo "  mv wget2 wget";
			echo "  chmod 755 wget";
		fi
	fi
fi

#setup a basic my.cnf file.
MYCNF=/etc/my.cnf
if [ -e $MYCNF ]; then
	mv -f $MYCNF $MYCNF.old
fi

echo "[mysqld]" > $MYCNF;
echo "local-infile=0" >> $MYCNF;


###############################################################################
###############################################################################

# Assuming everything got installed correctly, we can now begin the install:

$BIN_DIR/wget $WGET_OPTION -O $DA_PATH/update.tar.gz --bind-address=$IP https://www.directadmin.com/cgi-bin/daupdate?uid=$CID\&lid=$LID

if [ ! -e $DA_PATH/update.tar.gz ]; then
	echo "Unable to download $DA_PATH/update.tar.gz";
	exit 3;
fi

COUNT=`head -n 4 $DA_PATH/update.tar.gz | grep -c "* You are not allowed to run this program *"`;
if [ $COUNT -ne 0 ]; then
	echo "";
	echo "You are not authorized to download the update package with that client id and license id for this IP address. Please email sales@directadmin.com";
	exit 4;
fi

cd $DA_PATH;
tar xzf update.tar.gz

if [ ! -e $DA_PATH/directadmin ]; then
	echo "Cannot find the DirectAdmin binary.  Extraction failed";

        echo "";
        echo "If you see an error that says to add --no-check-certificate, recompile wget:";
        echo "http://help.directadmin.com/item.php?id=119";
        echo "";

	exit 5;
fi




###############################################################################

# write the setup.txt

echo "hostname=$HOST"        >  $SETUP;
echo "email=$EMAIL"          >> $SETUP;
echo "mysql=$DB_ROOT_PASS"   >> $SETUP;
echo "mysqluser=$DB_USER"    >> $SETUP;
echo "adminname=$ADMIN_USER" >> $SETUP;
echo "adminpass=$ADMIN_PASS" >> $SETUP;
echo "ns1=$NS1"              >> $SETUP;
echo "ns2=$NS2"              >> $SETUP;
echo "ip=$IP"                >> $SETUP;
echo "netmask=$NM"           >> $SETUP;
echo "uid=$CID"              >> $SETUP;
echo "lid=$LID"              >> $SETUP;
echo "services=$SERVICES"    >> $SETUP;

CFG=$DA_PATH/data/templates/directadmin.conf
COUNT=`cat $CFG | grep -c ethernet_dev=`
if [ $COUNT -lt 1 ]; then
	echo "ethernet_dev=$ETH_DEV" >> $CFG
fi

chmod 600 $SETUP

###############################################################################
###############################################################################

# Install it

cd $SCRIPTS_PATH;

./install.sh $CMD_LINE



if [ ! -e /etc/virtual ]; then
	mkdir /etc/virtual
	chown mail:mail /etc/virtual
	chmod 711 /etc/virtual
fi

#ok, yes, This totally doesn't belong here, but I'm not in the mood to re-release 13 copies of DA (next release will do it)
for i in blacklist_domains whitelist_from use_rbl_domains bad_sender_hosts blacklist_senders whitelist_domains whitelist_hosts whitelist_senders; do
	touch /etc/virtual/$i;
        chown mail:mail /etc/virtual/$i;
        chmod 644 /etc/virtual/$i;
done


