#!/bin/bash
APACHE=$(type -p apache2ctl)
JAVA=$(type -p java)
MYSQL=$(type -p mysql)
APACHE_USER=www-data
APACHE_GROUP=www-data
DEFAULT_APACHE_CONF="/etc/apache2/sites-enabled/000-default"
DEFAULT_LOG_DIR="/var/log/etherpad"
DEFAULT_INIT_SCRIPT_NAME="etherpad"
DEFAULT_INIT_SCRIPT_LOC="/etc/init.d/$DEFAULT_INIT_SCRIPT_NAME"
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
echo "ERROR: Please, run installation as root" 1>&2
exit 1
fi
echo "Checking prerequisites..."
# Check if apache is installed
if [[ ! -x $APACHE ]]; then
echo "ERROR: Apache not found"
exit 1
fi
if $APACHE -v | grep "Apache/2" > /dev/null; then
echo -ne "\t* "
$APACHE -v | grep "Apache/2"
else
echo "ERROR: Apache 2 is needed"
exit 1
fi
if ! $APACHE -D DUMP_MODULES 2>&1 | grep rewrite_module > /dev/null; then
echo "ERROR: mod_rewrite needed. Try to run as root 'a2enmod rewrite'"
exit 1
fi
if ! $APACHE -D DUMP_MODULES 2>&1 | grep proxy_module > /dev/null; then
echo "ERROR: mod_proxy needed. Try to run as root 'a2enmod proxy'"
exit 1
fi
if ! $APACHE -D DUMP_MODULES 2>&1 | grep proxy_http_module > /dev/null; then
echo "ERROR: mod_proxy_http needed. Try to run as root 'a2enmod proxy_http'"
exit 1
fi
#if [[ ! -a "/etc/apache2/mods-enabled/rewrite.load" ]]; then
# echo "ERROR: mod_rewrite needed"
# exit 1
#fi
# Check if java 1.6 is installed
if [[ ! -x $JAVA ]]; then
echo "ERROR: Java exec not found in path"
exit 1
fi
if $JAVA -version 2>&1 | grep 'java version "1.6' > /dev/null; then
echo -e "\t* Java 1.6 installed"
else
echo "ERROR: Java 1.6 required"
exit 1
fi
# Check if mysql is installed
if [[ -x $MYSQL ]]; then
echo -e "\t* MySQL client installed"
else
echo "ERROR: MySQL client not found in path"
exit 1
fi
if ! ps -ax 2>/dev/null | grep mysqld | grep -v grep > /dev/null; then
echo "WARNING: MySQL is not running. If you're planning to use it at localhost, please stop this installation (Ctr+C) and start mysqld before continue"
fi
echo "Prerequisites checking done"
while [[ true ]]; do
echo -n "Apache web folder [/var/www]: "
read www
if [[ -z $www ]]; then
www="/var/www"
fi
if [[ ! -a "$www" ]]; then
echo "ERROR: $www doesn't exist"
continue;
fi
echo -n "MySQL host [localhost]: "
read mysql_host
if [[ -z $mysql_host ]]; then
mysql_host="localhost"
fi
echo -n "MySQL port [3306]: "
read mysql_port
if [[ -z $mysql_port ]]; then
mysql_port="3306"
fi
echo -n "MySQL root password: "
stty -echo
read mysql_password
stty echo
echo
echo -n "Etherpad admin password: "
stty -echo
read etherpad_password
stty echo
echo
echo -n "Etherpad database name [etherpad]: "
read etherpad_db_name
if [[ -z $etherpad_db_name ]]; then
etherpad_db_name="etherpad"
fi
echo -n "Etherpad database user name [etherpad]: "
read etherpad_db_username
if [[ -z $etherpad_db_username ]]; then
etherpad_db_username="etherpad"
fi
echo -n "Etherpad database user password: "
stty -echo
read etherpad_db_password
stty echo
echo
echo -n "Etherpad server port [9000]: "
read etherpad_port
if [[ -z $etherpad_port ]]; then
etherpad_port="9000"
fi
domain_name=$(hostname)":80"
echo -n "Domain name and port [$domain_name]: "
read domain_name
if [[ -z $domain_name ]]; then
domain_name=$(hostname)":80"
fi
echo -n "Web application name [dokuwiki]: "
read web_name
if [[ -z $web_name ]]; then
web_name="dokuwiki"
fi
echo -e "\t* Apache web folder: $www"
echo -e "\t* MySQL host and port: $mysql_host:$mysql_port"
echo -e "\t* Etherpad database name: $etherpad_db_name"
echo -e "\t* Etherpad database user name: $etherpad_db_username"
echo -e "\t* Etherpad port: $etherpad_port"
echo -e "\t* Domain name and port: $domain_name"
echo -e "\t* Web application name: $web_name"
echo -n "Is this correct? [n]: "
read ans
case $ans in
Y|y) break;;
[Yy][Ee][Ss]) break;;
*)
esac
done
DEFAULT_INIT_SCRIPT_NAME="$DEFAULT_INIT_SCRIPT_NAME""_$etherpad_port.sh"
DEFAULT_INIT_SCRIPT_LOC="/etc/init.d/$DEFAULT_INIT_SCRIPT_NAME"
echo -n "Creating etherpad configuration file..."
etherdoku_conf="dokuwiki/etherpad/trunk/etherpad/etc/etherpad.localdev-default.properties"
echo "ajstdlibHome = ../infrastructure/framework-src/modules" > $etherdoku_conf
echo "appjetHome = ./data/appjet" >> $etherdoku_conf
echo "devMode = true" >> $etherdoku_conf
echo "etherpad.adminPass = $etherpad_password" >> $etherdoku_conf
echo "etherpad.fakeProduction = false" >> $etherdoku_conf
echo "etherpad.isProduction = false" >> $etherdoku_conf
echo "etherpad.SQL_JDBC_DRIVER = com.mysql.jdbc.Driver" >> $etherdoku_conf
echo "etherpad.SQL_JDBC_URL = jdbc:mysql://$mysql_host:$mysql_port/$etherpad_db_name" >> $etherdoku_conf
echo "etherpad.SQL_PASSWORD = $etherpad_db_password" >> $etherdoku_conf
echo "etherpad.SQL_USERNAME = $etherpad_db_username" >> $etherdoku_conf
echo "listen = 0.0.0.0:$etherpad_port" >> $etherdoku_conf
echo "logDir = ./data/logs" >> $etherdoku_conf
echo "modulePath = ./src" >> $etherdoku_conf
echo "transportPrefix = /comet" >> $etherdoku_conf
echo "transportUseWildcardSubdomains = true" >> $etherdoku_conf
echo "useVirtualFileRoot = ./src" >> $etherdoku_conf
echo "wikiurl = http://$domain_name/$web_name/etherpad" >> $etherdoku_conf
echo "wikilockspath= ../../../data/locks/" >> $etherdoku_conf
echo "wikilocktime=900000" >> $etherdoku_conf
echo "etherpad.skipHostnameCheck=true" >> $etherdoku_conf
echo " done"
if [[ -a "$www/$web_name" ]]; then
echo -n "WARNING: $www/$web_name exists. Press Ctr+C now if you want to cancel the installation or ENTER if you wish to overwrite it "
read
if ! rm -fr "$www/$web_name" ; then
echo "ERROR: Can't delete $www/$web_name"
exit 1
else
echo "$www/$web_name deleted"
fi
fi
echo -n "Copying files..."
mkdir "$www/$web_name"
if ! cp -R dokuwiki/* "$www/$web_name/" ; then
echo "ERROR: copying files failed"
exit 1
fi
echo " done"
echo -n "Creating etherpad DB '$etherpad_db_name' if not exists..."
if ! echo "create database if not exists $etherpad_db_name;" | $MYSQL -u root --password="$mysql_password" -h $mysql_host -P $mysql_port ; then
echo "ERROR: Can't create database"
exit 1
fi
echo " done"
echo -n "Removing previous etherpad DB user if exists..."
echo "DROP USER '$etherpad_db_username'@'localhost';" | $MYSQL -u root --password="$mysql_password" -h $mysql_host -P $mysql_port 2>/dev/null
echo "DROP USER '$etherpad_db_username'@'%';" | $MYSQL -u root --password="$mysql_password" -h $mysql_host -P $mysql_port 2>/dev/null
echo " done"
echo -n "Creating etherpad DB user..."
if ! echo "CREATE USER '$etherpad_db_username'@'localhost' identified by '$etherpad_db_password';" | $MYSQL -u root --password="$mysql_password" -h $mysql_host -P $mysql_port ; then
echo "ERROR: Can't create etherpad DB user"
exit 1
fi
if ! echo "CREATE USER '$etherpad_db_username'@'%' identified by '$etherpad_db_password';" | $MYSQL -u root --password="$mysql_password" -h $mysql_host -P $mysql_port ; then
echo "ERROR: Can't create etherpad DB user"
exit 1
fi
if ! echo "grant all privileges on $etherpad_db_name.* to '$etherpad_db_username'@'localhost' identified by '$etherpad_db_password';" | $MYSQL -u root --password="$mysql_password" -h $mysql_host -P $mysql_port ; then
echo "ERROR: Can't grant privileges to etherpad DB user"
exit 1
fi
if ! echo "grant all privileges on $etherpad_db_name.* to '$etherpad_db_username'@'%' identified by '$etherpad_db_password';" | $MYSQL -u root --password="$mysql_password" -h $mysql_host -P $mysql_port ; then
echo "ERROR: Can't grant privileges to etherpad DB user"
exit 1
fi
echo " done"
echo -n "Changing files owner..."
if ! chown -R $APACHE_USER "$www/$web_name" && chown root "$www/$web_name/etherpad/trunk/etherpad/etc/etherpad.localdev-default.properties" ; then
echo "ERROR: Can't change files owner. Maybe $APACHE_USER user doesn't exist"
exit 1
fi
echo " done"
echo -n "Changing files group..."
if ! chgrp -R $APACHE_GROUP "$www/$web_name" && chgrp root "$www/$web_name/etherpad/trunk/etherpad/etc/etherpad.localdev-default.properties" ; then
echo "ERROR: Can't change files group. Maybe $APACHE_GROUP group doesn't exist"
exit 1
fi
echo " done"
echo -n "Setting permissions to config file..."
if ! chmod o=-r "$www/$web_name/etherpad/trunk/etherpad/etc/etherpad.localdev-default.properties" ; then
echo "ERROR: Can't change permissions"
exit 1
fi
echo " done"
echo -n "Setting permissions to startup script..."
if ! chmod +x "$www/$web_name/etherpad/trunk/etherpad/bin/run-local.sh" ; then
echo "ERROR: Can't change permissions"
exit 1
fi
echo " done"
echo -n "Creating apache config file..."
if ! echo -e "RewriteEngine on\nRewriteRule ^(.*)$ http://%{SERVER_NAME}:$etherpad_port/\$1 [P]" > "$www/$web_name/etherpad/.htaccess" ; then
echo "ERROR: Can't create apache config file"
exit 1
fi
echo " done"
if [[ ! -a "$DEFAULT_LOG_DIR" ]] ; then
echo -n "Creating log dir..."
if ! mkdir "$DEFAULT_LOG_DIR" ; then
echo "ERROR: Can't create log dir at $DEFAULT_LOG_DIR"
exit 1
fi
if ! chown -R root "$DEFAULT_LOG_DIR" ; then
echo "ERROR: Can't set root owner to $DEFAULT_LOG_DIR"
exit 1
fi
echo " done"
fi
echo -n "Adding etherpad startup script to $DEFAULT_INIT_SCRIPT_LOC..."
if ! echo "export PATH=$PATH" > "$DEFAULT_INIT_SCRIPT_LOC" ; then
echo "ERROR: Can't create $DEFAULT_INIT_SCRIPT_LOC"
exit 1
fi
if ! echo BASE="$www/$web_name" >> $DEFAULT_INIT_SCRIPT_LOC ; then
echo "ERROR: Can't create $DEFAULT_INIT_SCRIPT_LOC"
exit 1
fi
if ! echo LOG="$DEFAULT_LOG_DIR"/log_"$etherpad_port.log" >> $DEFAULT_INIT_SCRIPT_LOC ; then
echo "ERROR: Can't create $DEFAULT_INIT_SCRIPT_LOC"
exit 1
fi
if ! echo ERROR_LOG="$DEFAULT_LOG_DIR"/error_"$etherpad_port.log" >> $DEFAULT_INIT_SCRIPT_LOC ; then
echo "ERROR: Can't create $DEFAULT_INIT_SCRIPT_LOC"
exit 1
fi
if ! cat "$www/$web_name/etherpad/trunk/etherpad/bin/etherpad.sh" >> $DEFAULT_INIT_SCRIPT_LOC ; then
echo "ERROR: Can't create $DEFAULT_INIT_SCRIPT_LOC"
exit 1
fi
if ! chmod +x $DEFAULT_INIT_SCRIPT_LOC ; then
echo "ERROR: Can't set permissions for $DEFAULT_INIT_SCRIPT_LOC"
exit 1
fi
echo "Removing previous init script"
update-rc.d -f $DEFAULT_INIT_SCRIPT_NAME remove 2>/dev/null
if ! update-rc.d $DEFAULT_INIT_SCRIPT_NAME defaults ; then
echo "ERROR: Can't add etherpad startup script. Please do it manually and start etherpad"
fi
echo " done"
echo -n "Starting etherpad..."
nohup $DEFAULT_INIT_SCRIPT_LOC restart 2>/dev/null >/dev/null &
echo " done"
if ! cat $DEFAULT_APACHE_CONF 2>/dev/null | grep "AllowOverride All" >/dev/null ; then
echo "WARNING: Can't find 'AllowOverride All' directive in $DEFAULT_APACHE_CONF. Please be sure that you have that directive in your apache configuration. Otherwise etherpad won't work"
fi
echo "Installation finished. If you wish to customize your dokuwiki installation, please go to http://$domain_name/$web_name/install.php"