#!/bin/sh

VERBOSE_INSTALL=yes

set -e

DO_EXIT="no"
for i in $@ ; do
	case "${1}" in
	"--not-interactive")
		NOT_INTERACTIVE=yes
		shift
		;;
	"--centos-init-daemons")
		CENTOS_INIT_DAEMONS=yes
		shift
		;;
	"--mysql-pass")
		if [ -z "${2}" ] ; then echo "Parameter for option --mysql-pass is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		conf_mysql_pass=${2}
		shift
		shift
		;;
	"--dtcadmin-pass")
		if [ -z "${2}" ] ; then echo "Parameter for option --dtcadmin-pass is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		conf_adm_pass=${2}
		shift
		shift
		;;
	esac
done
if [ "${NOT_INTERACTIVE}" = "yes" ] ; then
	if [ -z "${conf_mysql_pass}" ] ; then
		echo "No MySQL password. Please use --mysql-pass <ROOT-PASS>" > /dev/stderr
		DO_EXIT="yes"
	fi
	if [ -z "${conf_adm_pass}" ] ; then
		echo "No MySQL password. Please use --dtcadmin-pass <DTC-PASS>" > /dev/stderr
		DO_EXIT="yes"
	fi
fi
if [ "${DO_EXIT}" = "yes" ] ; then
	exit 1
fi

preseed_default_conf_values () {
	gen_pass=`dd if=/dev/random bs=64 count=1 2>|/dev/null | md5sum | cut -d' ' -f1 | awk '{print substr($0,0,16)}'`
	conf_mysqlautoconfig=false
	conf_mysql_host=localhost
	conf_mysql_login=root
	if [ -z "${conf_mysql_pass}" ] ; then
		conf_mysql_pass=${gen_pass}
	fi
	conf_mysql_db=dtc
	conf_mysql_change_root=false
	conf_mta_type=postfix
	conf_use_cyrus=false
	dtc_admin_subdomain=dtc
	main_domain_name=`hostname -d`
	conf_adm_login=dtc
	if [ -z "${conf_adm_pass}" ] ; then
		conf_adm_pass=${gen_pass}
	fi
	if [ -x /sbin/ifconfig ] ; then
		IFCONFIG=/sbin/ifconfig
	else
		IFCONFIG=ifconfig
	fi
	guessed_ip_addr=`${IFCONFIG} | head -n 2 | tail -n 1 | cut -f2 -d":" | cut -f1 -d" "`
	conf_ip_addr=${guessed_ip_addr}
	conf_use_nated_vhosts=false
	conf_nated_vhosts_ip=192.168.2.199
	conf_apache_version=2
	conf_hosting_path=/var/www/sites
	conf_chroot_path=/var/lib/dtc/chroot_template
	conf_eth2monitor=eth0
	conf_recipient_delim="+"
	conf_mx_mail=mx
	conf_dnsbl_list=zen.spamhaus.org
	conf_report_setup=true
	conf_omit_dev_mknod=false
	conf_gen_ssl_cert=true
	conf_cert_passphrase=${gen_pass}
	conf_cert_countrycode=FR
	conf_cert_locality=Paris
	conf_cert_organization=default-org
	conf_cert_unit=no-unit
	conf_cert_email=changeme@example.org
	conf_cert_challenge_pass=${gen_pass}
	conf_ftp_type=pureftpd
	conf_mta_type=postfix
}


if [ -f /etc/debian_version ] ; then
	/usr/share/dtc/admin/install/uninstall
	. /usr/share/dtc/admin/install/debian_config
	. ${DTC_SAVED_INSTALL_CONFIG}
	if [ ""$conf_apache_version = "2" ] ; then
		PATH_HTTPD_CONF=/etc/apache2/apache2.conf
	else
		PATH_HTTPD_CONF=/etc/apache/httpd.conf
	fi
	. /usr/share/dtc/admin/install/functions
	DTCsetupDaemons
fi

if [ -f /etc/redhat-release ] ; then
	# Prepare everything
	. /usr/share/dtc/admin/install/redhat_config
	. /usr/share/dtc/admin/install/functions
	. /usr/share/dtc/admin/install/interactive_installer
	if [ "${CENTOS_INIT_DAEMONS}" = "yes" ] ; then
		ALL_DAEMONS="httpd crond postfix clamd amavisd rsyslog mysqld courier-authlib courier-imap dkimproxy pure-ftpd spamassassin saslauthd"
		for i in ${ALL_DAEMONS} ; do
			if [ -x /etc/rc.d/init.d/$i ] ; then
				service $i start
				chkconfig $i on
			fi
		done
		/usr/bin/mysqladmin -u root password ${conf_mysql_pass}
	fi
	if [ "${NOT_INTERACTIVE}" = "yes" ] ; then
		preseed_default_conf_values
		saveConfig
	else
		interactiveInstaller
	fi
	DTCinstallPackage
	/usr/share/dtc/admin/install/uninstall
	. ${DTC_SAVED_INSTALL_CONFIG}
	# Then call the installer!
	DTCsetupDaemons
elif [ -f SuSE-release ] ; then
	# Prepare everything
	. /usr/share/dtc/admin/install/suse_config
	. /usr/share/dtc/admin/install/functions
	. /usr/share/dtc/admin/install/interactive_installer
	interactiveInstaller
	DTCinstallPackage
	/usr/share/dtc/admin/install/uninstall
	. ${DTC_SAVED_INSTALL_CONFIG}
	/usr/share/dtc/admin/install/functions
	# Then call the installer!
	DTCsetupDaemons
fi

UNAME_MINUS_S=`uname -s`
if [ "${UNAME_MINUS_S}" = "Darwin" ] ; then
	. /usr/share/dtc/admin/install/osx_config
	. /usr/share/dtc/admin/install/functions
	. /usr/share/dtc/admin/install/interactive_installer
	interactiveInstaller
	DTCinstallPackage
	/usr/share/dtc/admin/install/uninstall
	DTCsetupDaemons
fi

if [ ${UNAME_MINUS_S} = "FreeBSD" ] ; then
	echo "FreeBSD detected"
        . /usr/local/www/dtc/admin/install/bsd_config
	. /usr/local/www/dtc/admin/install/functions
        . /usr/local/www/dtc/admin/install/interactive_installer
        interactiveInstaller
        DTCinstallPackage
        DTCsetupDaemons
fi

if [ -e /etc/gentoo-release ] ; then
	echo "Gentoo detected"
	. /usr/share/dtc/admin/install/gentoo_config
	. /usr/share/dtc/admin/install/functions
	. /usr/share/dtc/admin/install/interactive_installer
	interactiveInstaller
	DTCinstallPackage
	/usr/share/dtc/admin/install/uninstall
	. ${DTC_SAVED_INSTALL_CONFIG}
	/usr/share/dtc/admin/install/functions
	DTCsetupDaemons
fi
