#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Turn logging on or off

# License
# =======
#
# Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-logging
export TEXTDOMAINDIR="/usr/share/locale"

WEBSERVER_LOG_LEVEL='warn'

if [ ! "$1" ]; then
    exit 1
fi

if [[ "$1" == "on" || "$1" == "On" || "$1" == "ON" ]]; then
  if [ -d /etc/nginx ]; then
      for filename in /etc/nginx/sites-available/* ; do
          filename_domain=$(echo "$filename" | awk -F '/' '{print $5}')
          sed -i "s|access_log.*|access_log /var/log/nginx/$filename_domain.access.log;|g" $filename
          sed -i "s|error_log.*|error_log /var/log/nginx/$filename_domain.err.log $WEBSERVER_LOG_LEVEL;|g" $filename
      done
  fi
  if [ -f /etc/init.d/spamassassin ]; then
      sed -i 's|DOPTIONS="-s null -d --pidfile=$PIDFILE"|DOPTIONS="-d --pidfile=$PIDFILE"|g' /etc/init.d/spamassassin
  fi
  if [ -d /etc/prosody ]; then
      sed -i 's|info = "/dev/null";|info = "/var/log/prosody/prosody.log";|g' /etc/prosody/prosody.cfg.lua
      sed -i 's|error = "/dev/null";|error = "/var/log/prosody/prosody.err";|g' /etc/prosody/prosody.cfg.lua
  fi
  if [ -d /etc/exim4 ]; then
      sed -i 's|log_selector =.*|log_selector = MAIN_LOG_SELECTOR|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
  fi
else
  if [ -d /etc/nginx ]; then
      for filename in /etc/nginx/sites-available/* ; do
          sed -i 's|access_log.*|access_log off;|g' $filename
          sed -i 's|warn_log.*|warn_log off;|g' $filename
          sed -i 's|error_log.*|error_log off;|g' $filename
      done
      shred -zu /var/log/nginx/*
  fi
  if [ -f /etc/init.d/spamassassin ]; then
      sed -i 's|DOPTIONS="-d --pidfile=$PIDFILE"|DOPTIONS="-s null -d --pidfile=$PIDFILE"|g' /etc/init.d/spamassassin
  fi
  if [ -d /etc/prosody ]; then
      sed -i 's|info = "/var/log/prosody/prosody.log";|info = "/dev/null";|g' /etc/prosody/prosody.cfg.lua
      sed -i 's|error = "/var/log/prosody/prosody.err";|error = "/dev/null";|g' /etc/prosody/prosody.cfg.lua
      shred -zu /var/log/prosody/prosody.log
      shred -zu /var/log/prosody/prosody.err
  fi
  if [ -d /etc/exim4 ]; then
      sed -i 's|log_selector =.*|log_selector = -all|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
  fi
fi

if [ -d /etc/nginx ]; then
    service php5-fpm restart
    service nginx restart
fi
if [ -f /etc/init.d/spamassassin ]; then
    service spamassassin restart
fi
if [ -d /etc/prosody ]; then
    service prosody restart
fi
if [ -d /etc/exim4 ]; then
    service exim4 restart
fi

exit 0
