VCL_VERSION=2.4-test
DB_USERNAME=vcluser

WORKPATH=$(pwd)

if [[ -f NOTICE && -f LICENSE && -d managementnode && -d web && -d mysql ]]; then
	WORKPATH=$(dirname `pwd`)
fi

# TODO
#tmp=$(dirname $0)
#pushd $tmp > /dev/null
#SCRIPTPATH=$(pwd)
#popd > /dev/null
#echo $SCRIPTPATH

function print_break () {
	echo "------------------------------------------------------------------------------------------"
}

function random_string () {
	local string_length
	if [[ -n $1 ]]; then
		string_length=$1
	else
		string_length=8
	fi
	
	random_string=</dev/urandom tr -dc A-Za-z0-9 | head -c $string_length
	echo $random_string
}

# ------------------------- variables -------------------------------
DB_PASSWORD=`random_string 15`
CRYPTKEY=`random_string 20`
PEMKEY=`random_string 20`
ARCHIVE=apache-VCL-$VCL_VERSION.tar.bz2
#ARCHIVEURLPATH=http://vcl.apache.org/downloads/download.cgi?action=download&filename=%2Fvcl%2F
ARCHIVEURLPATH=http://people.apache.org/~jfthomps/tmp/ # TODO
#SIGPATH=http://www.apache.org/dist/vcl/
SIGPATH=http://people.apache.org/~jfthomps/tmp/ # TODO

# ------------------- checks for existing installation -----------------------
echo ""
echo "This script will exit if any existing parts of VCL are found. If they exist, you"
echo "must manually clean them up before using this script to install VCL. Checking"
echo "for existing VCL components..."
echo ""
# database
mysql -e "use vcl;" &> /dev/null
if [ $? -eq 0 ]; then echo "Existing vcl database found, exiting"; exit 1; fi
# web code
if [ -d /var/www/html/vcl ]; then echo "Existing web code found at /var/www/html/vcl, exiting"; exit 1; fi
# management code
if [ -d /usr/local/vcl ]; then echo "Existing management node code found at /usr/local/vcl, exiting"; exit 1; fi
echo "no existing VCL components found"

# ------------------------------ NOTICES -------------------------------------
print_break
echo ""
echo "NOTICE: Later in this process, you will be prompted to download and install"
echo "Linux packages and Perl modules. At that time, if you agree with the license"
echo "terms, enter YES to install them. Otherwise, enter NO to exit and abort the "
echo "installation."
echo ""
echo "(Press Enter to continue)"
read tmp

# -------------------------- admin password ----------------------------------
print_break
echo ""
echo "Enter the password you would like to use for the VCL admin user. This can be changed"
echo "later by running '/usr/local/vcl/bin/vcld --setup'"
echo -n "Password: "
IFS= read ADMIN_PASSWORD

while [[ $ADMIN_PASSWORD = ^[[:space:]]+$ || $ADMIN_PASSWORD = "" ]]; do
	echo "Password cannot be empty or contain only whitespace. Please enter the password."
	echo -n "Password: "
	IFS= read ADMIN_PASSWORD
done

# --------------------- public/private address selection ---------------------
print_break;
echo ""
echo "VCL requires two networks to operate (referred to as public and private"
echo "networks). The following network adapters and addresses were found. Please"
echo "enter the number next to the adapter/address you would like to use for the"
echo "specified network."
echo ""
ifcnt=0
while read line; do
	((ifcnt++))
	addr[$ifcnt]=$(echo $line | awk '{print $2}' | awk -F'/' '{print $1}')
	if [[ ${addr[$ifcnt]} = '' ]]; then echo "Error: Failed to parse network address data"; exit 1; fi
	if[$ifcnt]=$(echo $line | awk '{print $(NF)}')
	if [[ ${if[$ifcnt]} = '' ]]; then echo "Error: Failed to parse network address data"; exit 1; fi
done < <(ip addr list | grep inet | grep -v inet6)

i=0
while [[ $i < $ifcnt ]]; do
	((i++))
	echo "$i: ${if[$i]} ${addr[$i]}"
done
echo ""
echo -n "Private adapter/address: "
read privnum
while [[ ! $privnum =~ ^[0-9]+$ || $privnum < 1 || $privnum > $ifcnt ]]; do
	echo "Invalid selection. Please enter the number next to the adapter/address you would"
	echo "like to use for the private network."
	echo -n "Private adapter/address: "
	read privnum
done
PRIVIP=${addr[$privnum]}
echo ""

i=0
while [[ $i < $ifcnt ]]; do
	((i++))
	echo "$i: ${if[$i]} ${addr[$i]}"
done
echo ""
echo -n "Public adapter/address: "
read pubnum
while [[ ! $pubnum =~ ^[0-9]+$ || $pubnum < 1 || $pubnum > $ifcnt ]]; do
	echo "Invalid selection. Please enter the number next to the adapter/address you would"
	echo "like to use for the public network."
	echo -n "Public adapter/address: "
	read pubnum
done
PUBIP=${addr[$pubnum]}
if [[ $PUBIP = "" || $PRIVIP = "" ]]; then echo "Error: Failed to save network addresses"; exit 1; fi
echo ""
echo "Private address selected: $PRIVIP"
echo "Public address selected: $PUBIP"

# --------------------- prompt for installing dhcpd ----------------------------
print_break
echo "This script can install and configure dhcpd for you. VCL requires that VMs"
echo "always have the same private IP address assigned to them via dhcp. If you prefer"
echo "to install and configure dhcpd manually, answer NO to the following question."
echo "If you enter NO, you will have to set up dhcpd *manually* for VCL to work."
echo ""
echo -n "Install dhcpd? [yes] "
read DODHCP
DODHCP=$(echo $DODHCP | tr '[:upper:]' '[:lower:]')
if [[ $DODHCP = '' ]]; then DODHCP=yes; fi

while [[ ! $DODHCP =~ ^(yes|no)$ ]]; do
	echo -n "Please enter 'yes' or 'no': [yes] "
	read DODHCP
	DODHCP=$(echo $DODHCP | tr '[:upper:]' '[:lower:]')
	if [[ $DODHCP = '' ]]; then DODHCP=yes; fi
done

if [[ $DODHCP = 'yes' ]] && grep -q $PRIVIP /etc/dhcp/dhcpd.conf &> /dev/null; then
	echo ""
	echo "/etc/dhcp/dhcpd.conf appears to have been configured for VCL already, exiting"
	exit 1
fi
if [[ $DODHCP = 'yes' ]] && grep -q ${if[$privnum]} /etc/sysconfig/dhcpd &> /dev/null; then
	echo ""
	echo "/etc/sysconfig/dhcpd appears to have been configured for VCL already, exiting"
	exit 1
fi

# ------------------------- install basic required packages --------------------
print_break
echo "Installing Linux packages..."
yum -q -y install openssh-clients perl wget
if [ $? -ne 0 ]; then "Error: Failed to install required linux packages (openssh-client, perl, and wget)"; exit 1; fi;

# ------------------------------------ functions -------------------------------

function set_localauth_password () {
	local username=$1
	local password=$2
	
	#echo "Setting localauth password..."
	#echo "Username: $username"
	#echo "Password: $password"
	
	salt=$(random_string 8)
	#echo "Password salt: $salt"
	passhash=$(echo -n $password$salt | sha1sum | awk '{print $1}')
	#echo "Password hash: $passhash"
	mysql -e "UPDATE localauth SET passhash = '$passhash', salt = '$salt', lastupdated = NOW() WHERE localauth.userid = (SELECT id FROM user WHERE unityid = '$username');" vcl
	if [ $? -ne 0 ]; then
		echo "Error: Failed to set $username password to '$password'";
		exit 1;
	else
		echo "Successfully set $username password to '$password'"
		echo
	fi;
}

function download_archive () {
	wget -q "$ARCHIVEURLPATH$ARCHIVE" -O $ARCHIVE
}

function validate_archive_sha1 () {
	echo "Downloading sha1 file for $VCL_VERSION..."
	wget -q $SIGPATH$ARCHIVE.sha1
	echo "validating $ARCHIVE"
	sha1sum -c $ARCHIVE.sha1
	return $?
}

function validate_archive_gpg () {
	echo "Downloading GPG file for $VCL_VERSION..."
	wget -q $SIGPATH$ARCHIVE.asc
	echo "Downloading KEYS file for ASF VCL..."
	wget -q https://svn.apache.org/repos/asf/vcl/KEYS
	echo "Importing KEYS..."
	gpg -q --import KEYS
	/bin/rm -f KEYS
	echo "validating $ARCHIVE..."
	gpg -q --verify $ARCHIVE.asc 2>&1 | grep 'Good signature'
	return $?
}

function generic_error () {
	if [[ -n $1 ]]; then
		echo "$1; correct any errors listed above and try again"
	else
		echo "installation failed; correct any errors listed above and try again"
	fi
}

# ------------------- download/validate arvhice ---------------------
print_break
cd $WORKPATH
if [[ ! -f $ARCHIVE ]]; then
	echo "Downloading VCL $VCL_VERSION..."
	download_archive
	validate_archive_sha1
	if [ $? -ne 0 ]; then generic_error "failed to validate $ARCHIVE"; exit 1; fi;
	validate_archive_gpg
	if [ $? -ne 0 ]; then generic_error "failed to validate $ARCHIVE"; exit 1; fi;
else
	dir=`pwd`
	echo "archive for $VCL_VERSION found at $dir/$ARCHIVE"
	validate_archive_sha1
	if [ $? -ne 0 ]; then
		echo "failed to validate $ARCHIVE; downloading again..."
		/bin/mv -f $ARCHIVE $ARCHIVE.old
		download_archive
		validate_archive_sha1
		if [ $? -ne 0 ]; then generic_error "failed to validate $ARCHIVE"; exit 1; fi;
		validate_archive_gpg
		if [ $? -ne 0 ]; then generic_error "failed to validate $ARCHIVE"; exit 1; fi;
	else
		validate_archive_gpg
		if [ $? -ne 0 ]; then generic_error "failed to validate $ARCHIVE"; exit 1; fi;
	fi;
fi

# ------------------------ extract archive ---------------------------
echo "Extracting $ARCHIVE"
tar -xf $ARCHIVE
if [ $? -ne 0 ]; then generic_error "failed to extract $ARCHIVE"; exit 1; fi;

# ------------------- run install_perl_libs.pl ------------------------
print_break
echo "Installing Linux and PERL system requirements (this takes a while)"
sleep 1
yum -q -y install perl-CPAN
if [ $? -ne 0 ]; then echo "Error: Failed to install perl-CPAN"; exit 1; fi;
perl apache-VCL-$VCL_VERSION/managementnode/bin/install_perl_libs.pl
rc=$?
if [ $rc -eq 2 ]; then
	echo "License terms not accepted; aborting installation"
	exit 2
elif [ $rc -ne 0 ]; then
	generic_error "Failed to install system requirements"
	exit 1
fi;

# ---------------------- install mysql/mariadb -------------------------
print_break
rpm -q mysql-server &> /dev/null
if [ $? -ne 0 ]; then
	rpm -q mariadb-server &> /dev/null
	if [ $? -ne 0 ]; then
		echo "Installing MySQL/MariaDB Server..."
		yum -q search mysql-server | grep -q '^mysql-server'
		if [ $? -ne 0 ]; then
			yum -q search mariadb-server | grep -q '^mariadb-server'
			if [ $? -ne 0 ]; then
				echo "No mysql-server or mariadb-server packages found by yum"
				exit 1
			else
				yum -q -y install mariadb-server
				if [ $? -ne 0 ]; then generic_error "Failed to install mariadb-server"; exit 1; fi;
				echo "setting MariaDB to start on boot"
				/sbin/chkconfig mariadb on
				if [ $? -ne 0 ]; then generic_error "Failed to set mariadb-server to start at boot"; exit 1; fi;
				/sbin/service mariadb start
				if [ $? -ne 0 ]; then generic_error "Failed to start mariadb-server"; exit 1; fi;
			fi
		else
			yum -q -y install mysql-server
			if [ $? -ne 0 ]; then generic_error "Failed to install mysql-server"; exit 1; fi;
			echo "setting MySQL to start on boot"
			/sbin/chkconfig mysqld on
			if [ $? -ne 0 ]; then generic_error "Failed to set mysql-server to start at boot"; exit 1; fi;
			/sbin/service mysqld start
			if [ $? -ne 0 ]; then generic_error "Failed to start mysql-server"; exit 1; fi;
		fi
	else
		echo "MariaDB server already installed"
		echo "setting MariaDB to start on boot"
		/sbin/chkconfig mariadb on
		if [ $? -ne 0 ]; then generic_error "Failed to set mariadb-server to start at boot"; exit 1; fi;
		/sbin/service mariadb start
		if [ $? -ne 0 ]; then generic_error "Failed to start mariadb-server"; exit 1; fi;
	fi
else
	echo "MySQL server already installed"
	echo "setting MySQL to start on boot"
	/sbin/chkconfig mysqld on
	if [ $? -ne 0 ]; then generic_error "Failed to set mysql-server to start at boot"; exit 1; fi;
	/sbin/service mysqld start
	if [ $? -ne 0 ]; then generic_error "Failed to start mysql-server"; exit 1; fi;
fi

# ---------------------- install httpd and php -------------------------
print_break
echo "Installing httpd and php components..."
yum -q -y install httpd php mod_ssl php php-gd php-CRYPT php-mysql php-xml php-xmlrpc php-ldap php-process sendmail php-mbstring
if [ $? -ne 0 ]; then generic_error "Failed to install httpd"; exit 1; fi;
echo "setting httpd to start on boot"
/sbin/chkconfig httpd on
if [ $? -ne 0 ]; then generic_error "Failed to set httpd to start at boot"; exit 1; fi;
/sbin/service httpd start
if [ $? -ne 0 ]; then generic_error "Failed to start httpd"; exit 1; fi;


# ------------------------- set up firewall ----------------------------
print_break
echo "Opening TCP ports 80 and 443..."

if [[ -x /bin/systemctl && -x /bin/firewall-cmd ]]; then
	systemctl status firewalld &> /dev/null
	if [ $? -eq 0 ]; then
		firewall-cmd --zone=public --add-service=http --permanent
		if [ $? -ne 0 ]; then echo "Error: Failed to set firewall to allow port 80"; exit 1; fi;
		firewall-cmd --zone=public --add-service=https --permanent
		if [ $? -ne 0 ]; then echo "Error: Failed to set firewall to allow port 443"; exit 1; fi;
		firewall-cmd --reload
		if [ $? -ne 0 ]; then echo "Error: Failed reload firewall"; exit 1; fi;
	fi
elif [[ -x /sbin/iptables ]]; then 
	if ! /sbin/iptables -nL INPUT | grep 80 | grep ACCEPT; then
		/sbin/iptables -I INPUT 1 -m state --state NEW,RELATED,ESTABLISHED -m tcp -p tcp -j ACCEPT --dport 80
	fi
	if ! /sbin/iptables -nL INPUT | grep 443 | grep ACCEPT; then
		/sbin/iptables -I INPUT 1 -m state --state NEW,RELATED,ESTABLISHED -m tcp -p tcp -j ACCEPT --dport 443
	fi
	/sbin/iptables-save > /etc/sysconfig/iptables
	if [ $? -ne 0 ]; then echo "Error: Failed save iptables configuration"; exit 1; fi;
else
	echo "Warning: Failed to detect firewall system. You will need to ensure ports"
	echo "80 and 443 are allowed through your firewall on $PUBIP."
	echo ""
	echo "(Press ENTER to continue)"
	read tmp
fi

# ---------------------- create/set up vcl database ------------------------
print_break
echo "Creating VCL database..."
mysql -e "DROP DATABASE IF EXISTS vcl;"
mysql -e "CREATE DATABASE vcl;"
if [ $? -ne 0 ]; then generic_error "Failed to create VCL database"; exit 1; fi;
mysql -e "GRANT SELECT,INSERT,UPDATE,DELETE,CREATE TEMPORARY TABLES ON vcl.* TO '$DB_USERNAME'@'localhost' IDENTIFIED BY '$DB_PASSWORD';"
if [ $? -ne 0 ]; then generic_error "Failed to create VCL database user"; exit 1; fi;
mysql vcl < $WORKPATH/apache-VCL-$VCL_VERSION/mysql/vcl.sql
if [ $? -ne 0 ]; then generic_error "Failed to initialize VCL database"; exit 1; fi;

# ------------------------- copy web code in place -------------------------
print_break
echo "Copying VCL web code..."
/bin/cp -r $WORKPATH/apache-VCL-$VCL_VERSION/web/ /var/www/html/vcl
if [ $? -ne 0 ]; then generic_error "Failed to copy VCL web code in place"; exit 1; fi;
chown apache /var/www/html/vcl/.ht-inc/maintenance

# ---------------------------- configure web code --------------------------
echo "Configureing secrets.php..."
/bin/cp -f /var/www/html/vcl/.ht-inc/secrets-default.php /var/www/html/vcl/.ht-inc/secrets.php
if [ $? -ne 0 ]; then echo "Error: Failed to create secrets.php"; exit 1; fi;
sed -i -r -e "s/(vclusername\s+=\s+).*;/\1'$DB_USERNAME';/" /var/www/html/vcl/.ht-inc/secrets.php
if [ $? -ne 0 ]; then echo "Error: Failed to configure secrets.php"; exit 1; fi;
sed -i -r -e "s/(vclpassword\s+=\s+).*;/\1'$DB_PASSWORD';/" /var/www/html/vcl/.ht-inc/secrets.php
if [ $? -ne 0 ]; then echo "Error: Failed to configure secrets.php"; exit 1; fi;
sed -i -r -e "s/(cryptkey\s+=\s+).*;/\1'$CRYPTKEY';/" /var/www/html/vcl/.ht-inc/secrets.php
if [ $? -ne 0 ]; then echo "Error: Failed to configure secrets.php"; exit 1; fi;
sed -i -r -e "s/(pemkey\s+=\s+).*;/\1'$PEMKEY';/" /var/www/html/vcl/.ht-inc/secrets.php
if [ $? -ne 0 ]; then echo "Error: Failed to configure secrets.php"; exit 1; fi;

echo "Configureing conf.php..."
/bin/cp -f /var/www/html/vcl/.ht-inc/conf-default.php /var/www/html/vcl/.ht-inc/conf.php
if [ $? -ne 0 ]; then echo "Error: Failed to configure conf.php"; exit 1; fi;

echo "Generating keys..."
cd /var/www/html/vcl/.ht-inc
./genkeys.sh &> /dev/null
if [ $? -ne 0 ]; then echo "Error: Failed to generate crypto keys"; exit 1; fi;

# ---------------------------- set passwords ---------------------------
print_break
echo "Setting passwords..."
set_localauth_password admin $ADMIN_PASSWORD
set_localauth_password vclsystem $ADMIN_PASSWORD

# ---------------- copy management node code in place ------------------
print_break
echo "Installing management node components..."
/bin/cp -r $WORKPATH/apache-VCL-$VCL_VERSION/managementnode/ /usr/local/vcl

#--------------------- configure management node code ------------------
echo "Configuring vcld.conf..."
pkill -9 -f vcld
if [[ ! -d /etc/vcl ]]; then
	mkdir /etc/vcl
	if [ $? -ne 0 ]; then echo "Error: Failed to create /etc/vcl directory"; exit 1; fi;
fi
/bin/cp -f /usr/local/vcl/etc/vcl/vcld.conf /etc/vcl
if [ $? -ne 0 ]; then echo "Error: Failed to copy vcld.conf file to /etc/vcl"; exit 1; fi;
sed -i -r -e "s/(FQDN=).*/\1localhost/" /etc/vcl/vcld.conf
if [ $? -ne 0 ]; then echo "Error: Failed to configure vcld.conf"; exit 1; fi;
sed -i -r -e "s/(server=).*/\1localhost/" /etc/vcl/vcld.conf
if [ $? -ne 0 ]; then echo "Error: Failed to configure vcld.conf"; exit 1; fi;
sed -i -r -e "s/(LockerWrtUser=).*/\1$DB_USERNAME/" /etc/vcl/vcld.conf
if [ $? -ne 0 ]; then echo "Error: Failed to configure vcld.conf"; exit 1; fi;
sed -i -r -e "s/(wrtPass=).*/\1$DB_PASSWORD/" /etc/vcl/vcld.conf
if [ $? -ne 0 ]; then echo "Error: Failed to configure vcld.conf"; exit 1; fi;
sed -i -r -e "s/(xmlrpc_url=).*/\1https:\/\/localhost\/vcl\/index.php?mode=xmlrpccall/" /etc/vcl/vcld.conf
if [ $? -ne 0 ]; then echo "Error: Failed to configure vcld.conf"; exit 1; fi;
sed -i -r -e "s/(xmlrpc_pass=).*/\1$ADMIN_PASSWORD/" /etc/vcl/vcld.conf
if [ $? -ne 0 ]; then echo "Error: Failed to configure vcld.conf"; exit 1; fi;

#------------------ configure management to start at boot ---------------
echo "Configuring vcld service..."
/bin/cp -f /usr/local/vcl/bin/S99vcld.linux /etc/init.d/vcld
if [ $? -ne 0 ]; then echo "Error: Failed to copy initialization file in place"; exit 1; fi;
/sbin/chkconfig --add vcld
if [ $? -ne 0 ]; then echo "Error: Failed to configure vcld service to start on boot"; exit 1; fi;
/sbin/chkconfig --level 345 vcld on
if [ $? -ne 0 ]; then echo "Error: Failed to configure vcld service to start on boot"; exit 1; fi;

#----------------------- configure management in vcl --------------------
print_break
echo "Adding managment node to database..."
mysql -e "DELETE FROM vcl.managementnode;"
mysql -e "INSERT INTO vcl.managementnode (IPaddress, hostname, stateid) VALUES ('$PUBIP', 'localhost', '2');"
if [ $? -ne 0 ]; then echo "Error: Failed to add management node to database"; exit 1; fi;
mysql -e "DELETE FROM vcl.resource WHERE resourcetypeid = 16;"
mysql -e "INSERT INTO vcl.resource (resourcetypeid, subid) VALUES ('16', (SELECT id FROM vcl.managementnode WHERE hostname = 'localhost'));"
if [ $? -ne 0 ]; then echo "Error: Failed to add management node to database"; exit 1; fi;
mysql -e "INSERT INTO vcl.resourcegroupmembers (resourceid, resourcegroupid) SELECT vcl.resource.id, vcl.resourcegroup.id FROM vcl.resource, vcl.resourcegroup WHERE vcl.resource.resourcetypeid = 16 AND vcl.resourcegroup.resourcetypeid = 16;"
if [ $? -ne 0 ]; then echo "Error: Failed to add management node to database"; exit 1; fi;

# ----------------- install and configure dhcpd ------------------------
if [[ $DODHCP = 'yes' ]]; then
	print_break
	echo "Installing dhcp..."
	yum -q -y install dhcp
	if [ $? -ne 0 ]; then echo "Error: Failed to install dhcp"; exit 1; fi;

	echo "Configuring dhcp..."
	if ifconfig ${if[$privnum]} | grep $PRIVIP | grep -q 'Mask:'; then
		privmask=$(ifconfig ${if[$privnum]} | grep $PRIVIP | awk '{print $4}' | awk -F: '{print $2}')
	elif ifconfig ${if[$privnum]} | grep $PRIVIP | grep -q 'netmask '; then 
		privmask=$(ifconfig ${if[$privnum]} | grep $PRIVIP | awk '{print $4}')
	fi
	if [[ ! $privmask =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
		echo "Error: Failed to determine netmask for private address"
		exit 1
	fi
	IFS=. read pr1 pr2 pr3 pr4 <<<"$PRIVIP"
	IFS=. read prm1 prm2 prm3 prm4 <<<"$privmask"
	privnet="$((pr1 & prm1)).$((pr2 & prm2)).$((pr3 & prm3)).$((pr4 & prm4))"
	if [[ ! $privnet =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
		echo "Error: Failed to determine network for private address"
		exit 1
	fi

	echo "Private address: $PRIVIP"
	echo "Private netmask: $privmask"
	echo "Private network: $privnet"

	echo "Configuring /etc/dhcp/dhcpd.conf..."
(
cat <<'EOF'
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#   see 'man 5 dhcpd.conf'
#
ddns-update-style none;
shared-network eth0 {
	subnet PRIVNET netmask PRIVMASK {
		ignore unknown-clients;
	}
	# ----------- add computers from VCL web site below here ------------
}
EOF
) > /etc/dhcp/dhcpd.conf
	sed -i "s/PRIVNET/$privnet/" /etc/dhcp/dhcpd.conf
	sed -i "s/PRIVMASK/$privmask/" /etc/dhcp/dhcpd.conf
	if ! grep -q $privmask /etc/dhcp/dhcpd.conf; then
		echo "Error: Failed to configure /etc/dhcp/dhcpd.conf"
		exit 1
	fi

	if [[ -f /etc/sysconfig/dhcpd ]] && grep -q DHCPDARGS /etc/sysconfig/dhcpd; then
		sed -i -r -e "s/(DHCPDARGS=).*/\1${if[$privnum]}/" /etc/sysconfig/dhcpd
	else
		echo "DHCPDARGS=\"${if[$privnum]}\"" > /etc/sysconfig/dhcpd
	fi
	if ! grep -q ${if[$privnum]} /etc/sysconfig/dhcpd; then
		echo "Error: Failed to configure /etc/sysconfig/dhcpd"
		exit 1
	fi

	/sbin/chkconfig dhcpd on
	if [ $? -ne 0 ]; then echo "Error: Failed to configure dhcpd service to start on boot"; exit 1; fi;

	echo "Starting dhcpd service..."
	/sbin/service dhcpd start
	if [ $? -ne 0 ]; then generic_error "Failed to start dhcpd service"; exit 1; fi;
fi

# -------------------- create ssh identity key ---------------------
if [[ ! -r /etc/vcl/vcl.key ]]; then
	print_break
	echo "Creating SSH identity key file at /etc/vcl/vcl.key"
	ssh-keygen -t rsa -f "/etc/vcl/vcl.key" -N '' -b 1024 -C 'VCL root account'
	if [ $? -ne 0 ]; then echo "Error: Failed to create ssh identity key for connecting to managed VMs"; exit 1; fi;
	echo "IdentityFile /etc/vcl/vcl.key" >> /etc/ssh/ssh_config
	if [ $? -ne 0 ]; then echo "Error: Failed to add ssh identity key to /etc/ssh/ssh_config"; exit 1; fi;
fi

# ---------------------------- start vcld ----------------------------
print_break
echo "Starting vcld service..."
/sbin/service vcld stop &> /dev/null
sleep 1
/sbin/service vcld start
if [ $? -ne 0 ]; then echo "Error: Failed to start vcld service"; exit 1; fi;

echo ""
echo "VCL installation complete"
echo ""
echo "Your VCL system now needs to be configured. Follow online instructions to"
echo "1) Set up a VM Host Profile"
echo "2) Add a Virtual Host"
echo "3) Add VMs"
echo "4) export dhcpd data for the VMS and add that to /etc/dhcp/dhcpd.conf"
echo "5) Assign VMs to your VM Host(s)"
echo "6) create base images"
echo ""
echo "Your VCL system can be accessed at https://$PUBIP/vcl"
