#!/bin/sh

# This is the interactive installer that will asks questions
# for all of these OS that don't have ncurse (or similar)
# automated system.
# Authors: Thomas Goirand <thomas at goirand.fr>
# and Damien Mascord <tusker at tusker.org> with the help
# of some contributors

installerQuestions () {
	echo "###############################################################"
	echo "### Welcome to DTC config script for automatic installation ###"
	echo "###############################################################"

	if [ -z ""$DTC_SAVED_INSTALL_CONFIG ] ; then
		DTC_SAVED_INSTALL_CONFIG="/root/.dtc_saved_config"
	fi

	if [ -f ${DTC_SAVED_INSTALL_CONFIG} ] ; then
		. ${DTC_SAVED_INSTALL_CONFIG}
	else
		touch DTC_SAVED_INSTALL_CONFIG
	fi

	# DATABASE CONFIGURATION
	# conf_mysql_host
	echo "### MYSQL CONFIGURATION ###"
	echo ""
	echo "DTC needs to access to your mysql database"
	echo "Please give your mysql account information"
	if [ -z $conf_mysql_host ] ; then
		conf_mysql_host="localhost"
	fi
	echo -n "MySQL hostname [${conf_mysql_host}]: "
	read answer
	if [ ! -z $answer ]; then
		conf_mysql_host=${answer}
	fi

	# conf_mysql_login
	if [ -z $conf_mysql_login ] ; then
		conf_mysql_login="root"
	fi
	echo -n "MySQL root login [${conf_mysql_login}]: "
	read answer
	if [ ! -z $answer ]; then
		conf_mysql_login=${answer}
	fi

	# conf_mysql_pass
	if [ -z $conf_mysql_pass ] ; then
		conf_mysql_pass=""
	fi
	echo -n "MySQL root password [${conf_mysql_pass}]: "
	read answer
	if [ ! -z $answer ]; then
		conf_mysql_pass=${answer}
	fi

	# conf_mysql_change_root
	echo ""
	echo "Do you want that DTC setup this password"
	echo "for you ? (eg: UPDATE user SET Password=PASSWORD('XXX')...)"
	if [ -z ""$conf_mysql_change_root ] ; then
		conf_mysql_change_root="false"
	fi
	if [ $conf_mysql_change_root"" = "true" ] ; then
		echo -n 'Setup the mysql password [nY]: '
		read answer
		if [ -z $answer"" ] ; then
			answer="true"
		fi
	else
		echo -n 'Setup the mysql password [Ny]: '
		read answer
		if [ -z $answer"" ] ; then
			answer="false"
		fi
	fi
	if [ ""$answer = "y" -o ""$answer = "Y" ]; then
		answer="true"
	fi
	if [ ""$answer = "n" -o ""$answer = "N" ]; then
		answer="false"
	fi
	if [ ""$answer = "true" ] ; then
		echo "===> Will change MySQL Root password"
		conf_mysql_change_root="true"
	else
		echo "-> Will skip MySQL password root change!"
		conf_mysql_change_root="false"
	fi

	# conf_mysql_db
	if [ -z ""$conf_mysql_db ] ; then
		conf_mysql_db="dtc"
	fi
	echo -n "Choose a DB name for DTC [${conf_mysql_db}]: "
	read answer
	if [ ! -z ""$answer ] ; then
		conf_mysql_db=$answer
	fi
	if [ ""$conf_mysql_db = "" ] ; then
		conf_mysql_db="dtc"
	fi

	# conf_mta_type
	if [ -z ""$conf_mta_type ] ; then
		conf_mta_type=postfix
	fi
	echo ""
	echo "What MTA (Mail Tranport Agent, the one that"
	echo "will route and deliver your incoming mail) do"
	echo "you wish to use with DTC ? Type q for qmail"
	echo "or type p for postfix."
	if [ ""$conf_mta_type = "postfix" ] ; then
		echo -n 'MTA type (Qmail or Postfix) [q/P]: '
		read answer
		if [ -z ""$answer ] ; then
			conf_mta_type=postfix
		fi
	else
		echo -n 'MTA type (Qmail or Postfix) [Q/p]: '
		read answer
		if [ -z ""$answer ] ; then
			conf_mta_type=qmail
		fi
	fi
	if [ ""$answer = "Q" -o ""$answer = "q" ]; then
		conf_mta_type=qmail
	fi
	if [ ""$answer = "P" -o ""$answer = "p" ]; then
		conf_mta_type=postfix
	fi
	if [ ! ""$conf_mta_type = "postfix" ] ; then
		conf_mta_type=qmail
	fi

	# conf_cyrus_enable
	if [ -z ""$conf_use_cyrus ] ; then
		conf_use_cyrus="true"
	fi
	if [ ""$conf_use_cyrus = "true" ] ; then
		echo -n 'Are you using Cyrus IMAPd? [Y/n]: '
		read answer
		if [ -z ""$answer ] ; then
			conf_use_cyrus="true"
		fi
	else
		echo -n 'Are you using Cyrus IMAPd? [y/N]: '
		read answer
		if [ -z ""$answer ] ; then
			conf_use_cyrus="false"
		fi
	fi
	if [ ""$answer = "n" -o ""$conf_use_cyrus = "N" ]; then
		conf_use_cyrus="false"
	fi
	if [ ""$answer = "y" -o ""$conf_use_cyrus = "Y" ]; then
		conf_use_cyrus="true"
	fi

	if [ ""$conf_cyrus_enable = "true" ]; then
		if [ -z ""$conf_cyrus_pass ] ; then
			conf_cyrus_pass=${conf_mysql_pass}
		fi
		echo "Cyrus IMAPd will be used"
		echo ""
		echo -n "Password for Mail Administrator? [${conf_cyrus_pass}]: "
		read answer
		if [ ! -z $answer ]; then
			conf_cyrus_pass=${answer}
		fi
	else
		conf_cyrus_pass=${conf_mysql_pass}
	fi

	# FTP configuration
	if [ -z ""$conf_ftp_type ] ; then
		conf_ftp_type=pureftpd
	fi
	echo ""
	echo "What FTP Server do you wish to use with DTC ?"
	echo -n "Type 1 for pure-ftpd or type 2 for proftpd: [1]"
	read answer
	if [ -z ""$answer ] ; then
		conf_ftp_type=pureftpd
	fi
	if [ ""$answer = "1" ]; then
		conf_ftp_type=pureftpd
	fi
	if [ ""$answer = "2" ]; then
		conf_ftp_type=proftpd
	fi

	# Host configuration
	if [ -z ""${main_domain_name} ] ; then
		main_domain_name="example.com"
	fi
	echo "### YOUR SERVER CONFIGURATION ###"
	echo ""
	echo "Please enter the main domain name you will use."
	echo "DTC will install the root admin panel on that host."
	echo -n "Domain name [${main_domain_name}]: "
	read answer
	if [ ! -z ""${answer} ] ; then
		main_domain_name=${answer}
	fi

	if [ -z ""$dtc_admin_subdomain ] ; then
		dtc_admin_subdomain="dtc"
	fi
	echo ""
	echo "DTC will install a root admin panel on a subdomain"
	echo "of the domain you just provided. The default subdomain"
	echo "is dtc, which leads you to http://dtc."$main_domain_name"/"
	echo "You can enter another subdomain name if you want."
	echo -n "Subdomain for DTC admin panel [$dtc_admin_subdomain]: "
	read answer
	if [ ! -z ""$answer ] ; then
		dtc_admin_subdomain=$answer
	fi

	if [ ""$UNIX_TYPE = "freebsd" -o ""$UNIX_TYPE = "osx" ]; then
		echo "***FIX ME*** Installer in OS X and BSD version don't have IP addr detection yet!"
		guessed_ip_addr=""
	else
		echo "Trying to guess your current IP..."
		guessed_ip_addr=`ifconfig | head -n 2 | tail -n 1 | cut -f2 -d":" | cut -f1 -d" "`
	fi

	# conf_use_nated_vhosts
	# conf_nated_vhosts_ip
	echo ""
	echo "Do you want that DTC generates apache file to use"
	echo "a LAN IP address that your server is using?"
	echo "If your server is in the LAN behind a firewall"
	echo "that does NAT and port redirections of the public IP(s)"
	echo "address(es) to your server, then you must say YES"
	echo "here, otherwise (if your server is connected directly"
	echo "to the internet with a public static IP) leave it to NO."
	if [ -z ""$conf_use_nated_vhosts ] ; then
		conf_use_nated_vhosts="false"
	fi
	if [ ""$conf_use_nated_vhosts = "false" ] ; then
		echo -n "Use NATed vhosts ? [N/y]: "
		read answer
		if [ ! -z ""$answer ] ; then
			conf_use_nated_vhosts="false"
		fi
	else
		echo -n "Use NATed vhosts ? [n/Y]: "
		read answer
		if [ -z ""$answer ] ; then
			conf_use_nated_vhosts="true"
		fi
	fi
	if [ ""$answer = "y" -o ""$answer = "Y" -o ""$answer = "yes" ]; then
		conf_use_nated_vhosts="true"
	fi
	if [ ""$answer = "n" -o ""$answer = "N" ] ; then
		conf_use_nated_vhosts="false"
	fi
	if [ ! ""$conf_use_nated_vhosts = "false" ] ; then
		conf_use_nated_vhosts = "true"
	fi
	if [ ""$conf_use_nated_vhosts = "true" ] ; then
		echo ""
		echo " Please enter the LAN IP of your server."
		echo -n "IP address of your server if in the LAN [${guessed_ip_addr}]: "
		read answer
		if [ ! -z ""$answer ]; then
			conf_nated_vhosts_ip=$answer
		else
			conf_nated_vhosts_ip=${guessed_ip_addr}
		fi
	else
		conf_nated_vhosts_ip="192.168.0.2"
	fi

	# conf_ip_addr
	echo ""
	echo "I need now you host information to configure the daemons."
	if [ ""$conf_use_nated_vhosts = "yes" ] ; then
		echo -n "What is your external (public) IP addresse ?: "
		read conf_ip_addr
	else
		echo -n "What is your IP addresse ? [${conf_ip_addr}]: "

		read answer
		if [ ! -z ${answer} ] ; then
			conf_ip_addr=${answer}
		fi
	fi

	if [ -z ""$conf_hosting_path ] ; then
		conf_hosting_path="/var/www/sites"
	fi
	echo ""
	echo "Where will you keep your files for hosting ?"
	echo -n "Hosting path [$conf_hosting_path]: "
	read answer
	if [ ! -z ""$answer ] ; then
		conf_hosting_path=$answer
	fi

	# conf_chroot_path
	if [ -z ""$conf_chroot_path ] ; then
		conf_chroot_path="/var/www/chroot"
	fi
	echo ""
	echo "Path where to build the chroot environment."
	echo "Where do you want DTC to build the cgi-bin chroot"
	echo "environment? Please note that DTC will do hardlinks"
	echo "to that directory, so the chroot path should be in"
	echo "the same logical device as the path for hosted"
	echo "domains files."
	echo -n "Chroot path [$conf_chroot_path]: "
	read answer
	if [ ! -z ""$answer ] ; then
		conf_chroot_path=$answer
	fi

	# conf_adm_login
	if [ -z ""$conf_adm_login ] ; then
		conf_adm_login=dtc
	fi
	echo ""
	echo "What admin login/pass you want for the administration of "$main_domain_name "?"
	echo -n "Login [${conf_adm_login}]: "
	read answer
	if [ ! -z ""$answer ] ; then
		conf_adm_login=${answer}
	fi

	# conf_adm_pass
	echo -n "Password: [${conf_adm_pass}]"
	read answer
	if [ ! -z ${answer} ] ; then
		conf_adm_pass=${answer}
	fi

	# conf_eth2monitor
	if [ ""$UNIX_TYPE = "freebsd" -o ""$UNIX_TYPE = "osx" ]; then
		echo "***FIX ME*** OS X and FreeBSD don't have interface detection yet!"
	else
		NBRLINES=`grep -v "lo:" /proc/net/dev | wc -l`
		NBRIFACE=$((${NBRLINES} - 2 ))
		CNT=${NBRIFACE}
		ALL_IFACES=''
		while [ ${CNT} -gt 0 ] ; do
			ALL_IFACES=${ALL_IFACES}' '`grep -v "lo:" /proc/net/dev | tail -n ${CNT} | cut -f 1 -d':' | gawk -F ' ' '{print $1}' | head -n 1`
			CNT=$((${CNT} - 1 ))
		done
	fi
	if [ -z ""$conf_eth2monitor ] ; then
		conf_eth2monitor=$ALL_IFACES
	fi
	echo ""
	echo "DTC will setup an RRDTools graphing system for you, please"
	echo "enter all the interfaces you wish to see in the total traffic."
	echo -n 'Enter the iface you wish to monitor ['$conf_eth2monitor']: '
	read answer
	if [ ! -z ""$answer ]; then
		conf_eth2monitor=$answer
	fi

	echo ""
	echo "In order to know how many DTC setup has been done, the"
	echo "installer can report to GPLHost web site. No data is collected"
	echo "exept the operating system (Debian in your case), IP address"
	echo "of the setup (we use the IP as an identifier so we don't account"
	echo "upgrades and/or reinstallations, we wont ever use it for commercial"
	echo "purpose)."
	echo "You need an internet connection and wget installed to report. Do you"
	echo "wish to allow DTC installer to report the setup of the control panel?"
	if [ ""$conf_report_setup = "true" ] ; then
		echo -n 'Allow installer to report setup? [Yn]:'
		read answer
		if [ -z ""$answer ] ; then
			conf_report_setup="true"
		fi
	else
		echo -n 'Allow installer to report setup? [yN]:'
		read answer
		if [ -z ""$answer ] ; then
			conf_report_setup="false"
		fi
	fi
	if [ ""$answer = "y" -o ""$answer = "Y" ] ; then
		conf_report_setup="true"
	fi
	if [ ""$answer = "n" -o ""$answer = "N"] ; then
		conf_report_setup="false"
	fi

	echo ""
	echo "In some environment, like FreeBSD Jail or Linux vServer,"
	echo "mknod is not allowed to be executed. For that reason, it's possible"
	echo "to skip the creation of the dev null, random and urandom devices"
	echo "durring the chroot template creation."
	if [ ""$conf_omit_dev_mknod = "true" ] ; then
		echo "Skip mknod? [Yn]";
		read answer
		if [ -z ""$answer ] ; then
			conf_omit_dev_mknod="true"
		fi
	else
		echo "Skip mknod? [yN]";
		read answer
		if [ -z ""$answer ] ; then
			conf_omit_dev_mknod="false"
		fi
	fi
	if [ ""$answer = "n" -o ""$answer = "N" ] ; then
		conf_omit_dev_mknod="false"
	fi
	if [ ""$answer = "y" -o ""$answer = "Y" ] ; then
		conf_omit_dev_mknod="true"
	fi
	if [ ! ""$conf_omit_dev_mknod = "true" ] ; then
		conf_omit_dev_mknod="false"
	fi

	echo ""
	echo "DTC can generates a self signed certificate"
	echo "so you can browse safely the control panel use SSL"
	echo -n "Generate an SSL cert [Yn]: "
	read answer
	if [ -z ""$answer -o ""$answer = "y" -o ""$answer = "Y" ] ; then
		conf_gen_ssl_cert="true"
	else
		conf_gen_ssl_cert="false"
	fi
	if [ ""$conf_gen_ssl_cert = "true" ] ; then
		echo ""
		echo -n "Certificate pass phrase [${conf_cert_passphrase}]: "
		read answer
		if [ ! -z ""$answer ] ; then
			conf_cert_passphrase=$answer
		fi

		echo -n "Certificate 2 letter country code [${conf_cert_countrycode}]: "
		read answer
		if [ ! -z ""$answer ] ; then
			conf_cert_countrycode=$answer
		fi

		echo -n "Certificate locality [${conf_cert_locality}]: "
		read answer
		if [ ! -z ""$answer ] ; then
			conf_cert_locality=$answer
		fi

		echo -n "Certificate organization [${conf_cert_organization}]: "
		read answer
		if [ ! -z ""$answer ] ; then
			conf_cert_organization=$answer
		fi

		echo -n "Organization unit [${conf_cert_unit}]: "
		read answer
		if [ ! -z ""$answer ] ; then
			conf_cert_unit=$answer
		fi

		echo -n "Certificate email [${conf_cert_email}]: "
		read answer
		if [ ! -z ""$answer ] ; then
			conf_cert_email=$answer
		fi

		echo -n "Certificate challenge pass [${conf_cert_challenge_pass}]: "
		read answer
		if [ ! -z ""$answer ] ; then
			conf_cert_challenge_pass=$answer
		fi
	fi
}

printVariables () {
	echo "### Daemon path ###"
	echo "httpd.conf: "$PATH_HTTPD_CONF
	echo "named.conf: "$PATH_NAMED_CONF
	echo "proftpd.conf: "$PATH_PROFTPD_CONF
	echo "dovecot.conf: "$PATH_DOVECOT_CONF
	echo "Courier config path: "$PATH_COURIER_CONF_PATH
        if [ $conf_mta_type = "postfix" ]; then
                echo "postfix/main.cf: "$PATH_POSTFIX_CONF
        else   
                echo "qmail control: "$PATH_QMAIL_CTRL
        fi
	echo "php cgi: "$PATH_PHP_CGI
	echo "generated files: "$PATH_DTC_ETC

	echo "### DATABASE ###"
	echo "MySQL host: "$conf_mysql_host
	echo "MySQL login: "$conf_mysql_login
	echo "MySQL pass: "$conf_mysql_pass
	echo "MySQL db: "$conf_mysql_db
	echo "MySQL change pass: $conf_mysql_change_root"

	echo "### Mail ###"
	echo "MTA type: $conf_mta_type"
	echo "Cyrus enable: $conf_cyrus_enable"
	if [ $conf_cyrus_enable = "true" ]; then
		echo "Cyrus pass: $conf_cyrus_pass"
	fi

	echo "### Admin interface addresse ###"
	echo "Addresse of dtc panel: http://"$dtc_admin_subdomain"."$main_domain_name"/"
	echo "DTC login: "$conf_adm_login
	echo "DTC pass: "$conf_adm_pass
	echo "IP addr: "$conf_ip_addr
	echo "Use nated vhosts: $conf_use_nated_vhosts"
	if [ $conf_use_nated_vhosts = "true" ]; then
		echo "LAN IP for NAT: $conf_nated_vhosts_ip"
	fi

	echo "### Preferences ###"
	echo "Apache version: "$conf_apache_version
	echo "Hosting path: "$conf_hosting_path
	echo "Chroot template path: $conf_chroot_path"
	echo "Iface to monitor: $conf_eth2monitor"
	echo "Report setup: $conf_report_setup"
	echo "Omit mknod devices: $conf_omit_dev_mknod"
}

interactiveInstaller () {
	if [ -z ""${DTC_SAVED_INSTALL_CONFIG} ] ; then
		DTC_SAVED_INSTALL_CONFIG="/root/.dtc_saved_config"
	fi

	if [ -f $DTC_SAVED_INSTALL_CONFIG ] ; then
		. ${DTC_SAVED_INSTALL_CONFIG}
	else
		touch ${DTC_SAVED_INSTALL_CONFIG}
	fi
	if [ -z $conf_mysql_host ]; then
		installerQuestions
	fi
	printVariables
	echo "Config ok and continuing install? [Yn]"
	read continueinst
	while [ ""$continueinst = "n" -o ""$continueinst = "N" ] ; do
		installerQuestions
		printVariables
		echo "Config ok and continuing install? [Yn]"
		read continueinst
	done
	saveConfig

	# Deamon path configuration
	echo ""
	echo "### Last confirmation before installation !!! ###"
	echo -n 'Confirm and install DTC ? [Ny]:'
	read valid_infos
	if [ ""$valid_infos = "y" -o ""$valid_infos = "Y" ] ; then
		echo "Installation has started..."
	else
		echo "Configuration not validated : exiting !"
		exit
	fi
}
