#!/bin/bash -ex

# FUDforum configuration script to execute in chroot (rootfs).
# Note that overlay/ is applied before this script executes.

# Set the hostname.
HOSTNAME=fudforum
echo "$HOSTNAME" > /etc/hostname
sed -i "s|127.0.1.1 \(.*\)|127.0.1.1 $HOSTNAME|" /etc/hosts

# Install Linux updates and required packages.
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y \
     -o DPkg::Options::=--force-confdef \
     -o DPkg::Options::=--force-confold \
     install wget unzip zip php5-cli php5-gd

# Reset mysql's root passsword (handy while testing).
# service mysql stop | true
# pkill mysqld | true
# /usr/bin/mysqld_safe --skip-grant-tables &
# sleep 2
# mysql -u root -e "UPDATE user SET password='' WHERE user='root';" mysql
# mysql -u root -e "FLUSH PRIVILEGES;"
# pkill mysqld

# Start mysql.
service mysql start

# Create database and return true in case it already exists.
mysql -u root -e "CREATE DATABASE fudforum CHARACTER SET utf8 COLLATE utf8_general_ci;" || true

# Create db user.
mysql -u root -e "CREATE USER 'fudforum'@'localhost' IDENTIFIED BY 'tmppasswd'" || true

# Create db user (we will change the password later in 40regen-fudforum-secrets).
mysql -u root -e "GRANT ALL PRIVILEGES ON fudforum.* TO fudforum@localhost IDENTIFIED BY 'tmppasswd'"

# Download the FUDforum installer.
cd /tmp
wget -O fudforum.zip https://sourceforge.net/projects/fudforum/files/FUDforum_3.0.2.zip/download
unzip fudforum.zip

# Write config file to guide the installer.
# Ignore hard coded IP's, we fill update them at each boot (/etc/rc.local).
cat >/tmp/fudforum/install.cfg <<EOF
WWW_ROOT                = "http://127.0.1.1/"
SERVER_ROOT             = "/var/www/"
SERVER_DATA_ROOT        = "/var/www/data/"
DBHOST                  = localhost
DBHOST_USER             = fudforum
DBHOST_PASSWORD         = tmppasswd
DBHOST_DBNAME           = fudforum
DBHOST_TBL_PREFIX       = fud30_
DBHOST_DBTYPE           = mysql
COOKIE_DOMAIN           = 127.0.1.1
LANGUAGE                = en
TEMPLATE		= twilightgrey
ROOT_LOGIN              = admin
ROOT_PASS               = admin
ADMIN_EMAIL             = "admin@localhost"
PHP_CLI                 = "/usr/bin/php"
EOF

# Install FUDforum from command line.
cd /tmp/fudforum/
php install.php install.cfg

# Cleanup after installation.
rm -f /tmp/fudforum.zip
rm -rf /tmp/fudforum

# Set files to be writable by the webserver.
chown -R www-data:www-data /var/www

# Setup FUDforum's Mailing List Catcher and post sample message to the forum.
newaliases
service postfix start || true
mysql -uroot fudforum <<EOF
INSERT INTO fud30_mlist (id, forum_id, name, mlist_opt, mbox_type) VALUES
(1, 1, 'Default Mailing List Catcher', 72, 0);
EOF
sendmail -t <<EOF
Subject: Mailing List Catcher
From: admin@localhost
To: list-bot@localhost

This is a test E-mail message that was piped into the forum with postfix.
Subscribe user "list-bot" on this appliance to the mailing list you want to archive on this forum.
For details, see http://cvs.prohost.org/index.php/Mailing_List_Manager
EOF
sleep 1

# Setup cron to run cron.php every 5 minutes.
cat <<EOF >/tmp/cron.tab
0,5,10,15,20.25,30,35,40,45,50,55 * * * * /usr/bin/php /var/www/data/scripts/cron.php >/tmp/cron.log 2>&1
EOF
crontab -u www-data /tmp/cron.tab

# Stop mysql daemon.
service mysql stop

# Stop postfix.
service postfix stop

# Notes to self:
#
# Development:
#   vi fudforum/conf
#   tar cvfz fudforum.tar.gz fudforum/
#   tklpatch-apply / fudforum.tar.gz
#
# Reboot or execute the following commands to test the website:
#   /etc/rc.local
#   service mysql start
#
# Final ISO build:
#   tklpatch turnkey-lamp-11.0rc-lucid-x86.iso fudforum/
#

# EOF

