#!/bin/sh
CWD=`pwd`

NAME=custombuild
WORKDIR=/usr/local/directadmin/${NAME}
OPTIONS_CONF=${WORKDIR}/options.conf
CUSTOMBUILD_OPT=1.1

INPUT_VALUE=none

if [ $# -eq 1 ]; then
	INPUT_VALUE=$1
fi

cd ${CWD}
echo -n "" > ${OPTIONS_CONF}

# Which version of PHP set as default?
if [ "${INPUT_VALUE}" = "d" ]; then
	phpver=5
else
	echo -n "Do you want to have PHP4 or PHP5 as default? (4/5): ";
	read phpver;
fi
until [ "${phpver}" = "4" ] || [ "${phpver}" = "5" ]; do
	echo -n "Please enter '4' or '5':"
	read phpver
done
	
echo "#PHP settings. default_php possible values - 4 or 5" >> ${OPTIONS_CONF}
echo "default_php=${phpver}" >> ${OPTIONS_CONF}

# Which version of PHP5 to install?
if [ "${INPUT_VALUE}" = "d" ]; then
	wantphp5=yes
else
	echo -n "Do you want to have PHP5? (yes/no): ";
	read wantphp5;
fi
until [ "${wantphp5}" = "yes" ] || [ "${wantphp5}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no':"
	read wantphp5
done

if [ "${wantphp5}" = "yes" ]; then
	if [ "${INPUT_VALUE}" = "d" ]; then
		php5type=cli
	else
		echo -n "Do you want to have PHP5 as CLI or CGI? (cli/cgi): ";
		read php5type;
	fi
	until [ "${php5type}" = "cli" ] || [ "${php5type}" = "cgi" ]; do
		echo -n "Please enter 'cli' or 'cgi':"
		read php5type;
	done
	
	if [ "${php5type}" = "cli" ]; then
		echo "php5_cli=yes" >> ${OPTIONS_CONF}
		echo "php5_cgi=no" >> ${OPTIONS_CONF}
	elif [ "${php5type}" = "cgi" ]; then
		echo "php5_cli=no" >> ${OPTIONS_CONF}
		echo "php5_cgi=yes" >> ${OPTIONS_CONF}
	fi
elif [ "${wantphp5}" = "no" ]; then
	echo "php5_cli=no" >> ${OPTIONS_CONF}
	echo "php5_cgi=no" >> ${OPTIONS_CONF}
fi

# Which version of PHP4 to install?
if [ "${INPUT_VALUE}" = "d" ]; then
	wantphp4=no
else
	echo -n "Do you want to have PHP4? (yes/no): ";
	read wantphp4;
fi
until [ "${wantphp4}" = "yes" ] || [ "${wantphp4}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read wantphp4
done

if [ "${wantphp4}" = "yes" ]; then
	echo -n "Do you want to have PHP4 as CLI or CGI? (cli/cgi): ";
	read php4type;

	until [ "${php4type}" = "cli" ] || [ "${php4type}" = "cgi" ]; do
		echo -n "Please enter 'cli' or 'cgi': "
		read php4type;
	done
	
	if [ "${php4type}" = "cli" ]; then
		echo "php4_cli=yes" >> ${OPTIONS_CONF}
		echo "php4_cgi=no" >> ${OPTIONS_CONF}
	elif [ "${php4type}" = "cgi" ]; then
		echo "php4_cli=no" >> ${OPTIONS_CONF}
		echo "php4_cgi=yes" >> ${OPTIONS_CONF}
	fi
elif [ "${wantphp4}" = "no" ]; then
	echo "php4_cli=no" >> ${OPTIONS_CONF}
	echo "php4_cgi=no" >> ${OPTIONS_CONF}
fi
	# Which version of php.ini to install?
if [ "${INPUT_VALUE}" = "d" ]; then
	wantphpini=no
else
	echo -n "Do you want to have the php.ini (PHP configuration file) rewritten? (yes/no): ";
	read wantphpini;
fi
until [ "${wantphpini}" = "yes" ] || [ "${wantphpini}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read wantphpini
done

if [ "${wantphpini}" = "yes" ]; then
	echo -n "For the next question, the usual file is the dist (distribution) version.";
	echo -n "What type of php.ini (PHP configuration file) do you want to have? (dist/recommended): ";
	read phpinitype;
		until [ "${phpinitype}" = "dist" ] || [ "${phpinitype}" = "recommended" ]; do
		echo -n "Please enter 'dist' or 'recommended':"
		read phpinitype;
	done
	
	echo "php_ini=yes" >> ${OPTIONS_CONF}
	echo "#Possible values - recommended or dist" >> ${OPTIONS_CONF}
	echo "php_ini_type=${phpinitype}" >> ${OPTIONS_CONF}
elif [ "${wantphpini}" = "no" ]; then
	echo "php_ini=no" >> ${OPTIONS_CONF}
	echo "#Possible values - recommended or dist" >> ${OPTIONS_CONF}
	echo "php_ini_type=recommended" >> ${OPTIONS_CONF}
fi

# Install Zend Optimizer or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	installzend=no
else
	echo -n "Do you want to have Zend Optimizer? (yes/no): ";
	read installzend;
fi
until [ "${installzend}" = "yes" ] || [ "${installzend}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read installzend;
done
	
echo "zend=${installzend}" >> ${OPTIONS_CONF}
echo "" >> ${OPTIONS_CONF}

# Install MySQL or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	installmysql=no
else
	echo -n "Do you want to be able to update/instal MySQL using CustomBuild? (yes/no): ";
	read installmysql;
fi
until [ "${installmysql}" = "yes" ] || [ "${installmysql}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read installmysql;
done
	
if [ "${installmysql}" = "yes" ]; then
	echo -n "Which version of MySQL do you want to have? (4.1/5.0/5.1): ";
	read mysqlvers;
		until [ "${mysqlvers}" = "4.1" ] || [ "${mysqlvers}" = "5.0" ] || [ "${mysqlvers}" = "5.1" ]; do
		echo -n "Please enter '4.1', '5.0' or '5.1': "
		read mysqlvers;
	done
	
	echo "#Possible values - 5.0 or 5.1 (4.1 is possible too, but it's EOL)" >> ${OPTIONS_CONF}
	echo "mysql=${mysqlvers}" >> ${OPTIONS_CONF}
	echo "mysql_inst=yes" >> ${OPTIONS_CONF}
	echo "" >> ${OPTIONS_CONF}
elif [ "${installmysql}" = "no" ]; then
	echo "#Possible values - 5.0 or 5.1 (4.1 is possible too, but it's EOL)" >> ${OPTIONS_CONF}
	echo "mysql=5.0" >> ${OPTIONS_CONF}
	echo "mysql_inst=no" >> ${OPTIONS_CONF}
	echo "" >> ${OPTIONS_CONF}
fi

# Which version of Apache to use?
if [ "${INPUT_VALUE}" = "d" ]; then
	apachever=2.2
else
	echo -n "Which version of Apache do you want to have? (1.3/2.0/2.2): ";
	read apachever;
fi
until [ "${apachever}" = "1.3" ] || [ "${apachever}" = "2.0" ] || [ "${apachever}" = "2.2" ]; do
	echo -n "Please enter '1.3', '2.0' or '2.2': "
	read apachever;
done
	
echo "#Possible values - 1.3, 2.0 or 2.2" >> ${OPTIONS_CONF}
echo "apache_ver=${apachever}" >> ${OPTIONS_CONF}
echo "" >> ${OPTIONS_CONF}

# Install phpMyAdmin or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	phpmyadmin=yes
else
	echo -n "Do you want to be able to install/update phpMyAdmin using CustomBuild? (yes/no): ";
	read phpmyadmin;
fi
until [ "${phpmyadmin}" = "yes" ] || [ "${phpmyadmin}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read phpmyadmin;
done
	
echo "#Web applications" >> ${OPTIONS_CONF}
echo "phpmyadmin=${phpmyadmin}" >> ${OPTIONS_CONF}

# Install SquirrelMail or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	squirrelmail=yes
else
	echo -n "Do you want to be able to install/update SquirrelMail webmail using CustomBuild? (yes/no): ";
	read squirrelmail;
fi
until [ "${squirrelmail}" = "yes" ] || [ "${squirrelmail}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read squirrelmail;
done
	
echo "squirrelmail=${squirrelmail}" >> ${OPTIONS_CONF}

# Install RoundCube or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	roundcube=yes
else
	echo -n "Do you want to be able to install/update RoundCube webmail using CustomBuild? (yes/no): ";
	read roundcube;
fi
until [ "${roundcube}" = "yes" ] || [ "${roundcube}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read roundcube;
done
	
echo "roundcube=${roundcube}" >> ${OPTIONS_CONF}
echo "" >> ${OPTIONS_CONF}

# Install PHP mail() header patch or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	mailheaderpatch=yes
else
	echo -n "Do you want to install PHP mail() header patch together with PHP? (yes/no): ";
	read mailheaderpatch;
fi
until [ "${mailheaderpatch}" = "yes" ] || [ "${mailheaderpatch}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read mailheaderpatch;
done

echo "#Mail options" >> ${OPTIONS_CONF}
echo "mail-header-patch=${mailheaderpatch}" >> ${OPTIONS_CONF}

# Install Dovecot or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	dovecot=yes
else
	echo -n "Do you want to install Dovecot? (yes/no): ";
	read dovecot;
fi
until [ "${dovecot}" = "yes" ] || [ "${dovecot}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read dovecot;
done
echo "dovecot=${dovecot}" >> ${OPTIONS_CONF}

# Update exim.conf or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	eximconf=yes
else
	echo -n "Do you want to be able to update Exim configuration file (exim.conf) using CustomBuild? (yes/no): ";
	read eximconf;
fi
until [ "${eximconf}" = "yes" ] || [ "${eximconf}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read eximconf;
done

echo "eximconf=${eximconf}" >> ${OPTIONS_CONF}
echo "" >> ${OPTIONS_CONF}

# Install proftpd or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	proftpd=yes
else
	echo -n "Do you want to be able to install/update ProFTPD using CustomBuild? (yes/no): ";
	read proftpd;
fi
until [ "${proftpd}" = "yes" ] || [ "${proftpd}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read proftpd;
done

echo "#FTP options" >> ${OPTIONS_CONF}
echo "proftpd=${proftpd}" >> ${OPTIONS_CONF}
echo "" >> ${OPTIONS_CONF}

# Clean every time or not?
if [ "${INPUT_VALUE}" = "d" ]; then
	clean=yes
else
	echo -n "Do you want to clean everything (run './build clean' every time) running CustomBuild? (yes/no): ";
	read clean;
fi
until [ "${clean}" = "yes" ] || [ "${clean}" = "no" ]; do
	echo -n "Please enter 'yes' or 'no': "
	read clean;
done

echo "#Custombuild options" >> ${OPTIONS_CONF}
echo "clean=${clean}" >> ${OPTIONS_CONF}

echo "custombuild=${CUSTOMBUILD_OPT}" >> ${OPTIONS_CONF}

echo "cleanapache=no" >> ${OPTIONS_CONF}
echo "clean_old_tarballs=no" >> ${OPTIONS_CONF}

echo "Options.conf has been installed successfully" 
