#!/bin/sh
# install_root.sh
# $Id$

. ./install_var.sh

#set -x

rm -f /tmp/insttoughlog

if [ -z "`grep '^postgres:' /etc/passwd`" ]; then
	echo "Error: User 'postgres' does not exist. Install postgresql.";
	exit 1
fi

if [ -z "$(su - postgres -c 'psql -l')" ]; then
	echo "PostgreSQL is not started."
	exit 1
fi

if [ -z "$(ps auxw | egrep '^postgres.*postmaster')" ]; then
	echo "PostgreSQL is not started by user 'postgres'."
	exit 1
fi

if [ -z "`grep '^toughlog:' /etc/group`" ]; then
	groupadd toughlog
else
	echo "Group 'toughlog' exist.";
fi

if [ -z "`grep '^toughlog:' /etc/passwd`" ]; then
	adduser -m -c "Tough Log" -g toughlog toughlog
else
	echo "User 'toughlog' exist.";
fi

[ ! -d /etc/toughlog ] && mkdir /etc/toughlog
# toughlogd.conf could have a password written in clear text, use mode 0600
install -D -o toughlog -g toughlog -m 0600 etc/toughlog/toughlogd.conf /etc/toughlog/toughlogd.conf
if [ ! -e /etc/toughlog/apache.conf ]; then
	install -D -o ${APACHEGROUP} -g toughlog -m 0664 etc/toughlog/apache.conf /etc/toughlog/apache.conf
	echo "Changing 'CustomLog' in /etc/toughlog/apache.conf: ${APACHELOG}/toughlog_access_log"
	perl -i -pe "s|^\s*CustomLog.*toughlog$|\tCustomLog ${APACHELOG}/toughlog_access_log toughlog|" /etc/toughlog/apache.conf
fi

[ ! -d "${APACHELOG}" ] && mkdir -p ${APACHELOG}
[ ! -e ${APACHELOG}/toughlog_access_log ] && touch ${APACHELOG}/toughlog_access_log
chown ${APACHEUSER}:toughlog ${APACHELOG}/toughlog_access_log
chmod 0664 ${APACHELOG}/toughlog_access_log

install -D -o root -g root -m 0755 usr/bin/toughlogd /usr/local/bin/toughlogd
install -D -o root -g root -m 0755 sql/toughlog2sql /usr/local/bin/toughlog2sql

# Apache
INCLUDEAPACHE="Include /etc/toughlog/apache.conf"
if [ -z "`grep -i \"^${INCLUDEAPACHE}\" ${APACHECONF}`" ]; then
	echo "Adding '${INCLUDEAPACHE}' to ${APACHECONF}"
	echo ${INCLUDEAPACHE} >> ${APACHECONF}
fi

# Create working directory
mkdir /tmp/toughlog

# Do postgres things
echo "Start as user 'postgres' and add user and language."
cp -a install_var.sh install_postgres.sh /tmp/toughlog/
su postgres -c "/tmp/toughlog/install_postgres.sh" >> /tmp/insttoughlog

# Do toughlog things
echo "Start as user 'toughlog'. Installing tables - wait 20 seconds..."
cp install_toughlog.sh /tmp/toughlog/
cp sql/*.sql sql/Makefile /tmp/toughlog/
chmod -R a+rX /tmp/toughlog/
su toughlog -c "/tmp/toughlog/install_toughlog.sh" >> /tmp/insttoughlog

# Clean up
rm -rf /tmp/toughlog

echo
echo "Installation completed."
echo "--------------------------"
echo -n "Databases created:.. "
grep "CREATE DATABASE" /tmp/insttoughlog | wc -l | egrep -o "[0-9]+"
echo -n "Users created:...... "
grep "CREATE USER" /tmp/insttoughlog | wc -l | egrep -o "[0-9]+"
echo -n "Tables created:..... "
grep "CREATE TABLE" /tmp/insttoughlog | wc -l | egrep -o "[0-9]+"
echo -n "Functions created:.. "
grep "CREATE FUNCTION" /tmp/insttoughlog | wc -l | egrep -o "[0-9]+"
echo -n "GeoIP rows inserted: "
su - toughlog -c "psql -At -c 'select count(*) from geoip' toughlog"
echo -n "Errors:............. "
grep "^ERROR" /tmp/insttoughlog | wc -l | egrep -o "[0-9]+"
echo
echo "For further information: /tmp/insttoughlog"
echo
echo "To activate a website with ToughLog, open your ${APACHECONF}"
echo "and add/change the following line:"
echo -e "\tCustomLog ${APACHELOG}/toughlog_access_log toughlog"

