#!/bin/bash
#
# Copyright (c) 2003 Stefan Moeding <s.moeding@setuid.de>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#       $Id$
#

source /usr/local/lib/hostsharing/pac
source /usr/local/lib/hostsharing/dom


#
# Renice
#
renice 20 $$ >/dev/null 2>&1


#
# Temporäres Directory
#
TMPDIR=`mktemp -d /tmp/purgelogs-XXXXXX`


#
# Mail & FTP
#
for PAC in `pacs_by_name '*'`; do
  if [ -d "/home/pacs/${PAC}/etc" -a -d "/home/pacs/${PAC}/var" ]; then
    #
    # Maillogs löschen
    #
    PURGELOG=`get-conf-parameter --pac=${PAC} --parameter=mail_log_purge`
    test "$?" -ne "0" && PURGELOG="yes"

    case "${PURGELOG}" in
      [Yy][Ee][Ss])
        #
        # Aufbewahrungszeit für Logfiles dieses Paketes holen
        # Bei fehlerhaften Werten wird der Default 48 Tage genommen.
        #
        typeset -i AGE
        AGE=`get-conf-parameter --pac=${PAC} --parameter=mail_log_retention`
        test "$?" -ne "0" && AGE=48
        test "${AGE}" -gt "0" || AGE=48

        find /home/pacs/${PAC}/var \
          -name "mail-????????-????.log.gz" -mtime +${AGE} -daystart \
          -maxdepth 1 -type f -exec rm -f {} \;
        ;;
      *)
        ;;
    esac

    #
    # FTP-Logs löschen
    #
    PURGELOG=`get-conf-parameter --pac=${PAC} --parameter=ftp_log_purge`
    test "$?" -ne "0" && PURGELOG="yes"

    case "${PURGELOG}" in
      [Yy][Ee][Ss])
        #
        # Aufbewahrungszeit für Logfiles dieses Paketes holen
        # Bei fehlerhaften Werten wird der Default 48 Tage genommen.
        #
        typeset -i AGE
        AGE=`get-conf-parameter --pac=${PAC} --parameter=ftp_log_retention`
        test "$?" -ne "0" && AGE=48
        test "${AGE}" -gt "0" || AGE=48

        # Neue Filenamen
        find /home/pacs/${PAC}/var \
          -name "ftp-????????-????.log.gz" -mtime +${AGE} -daystart \
	  -maxdepth 1 -type f -exec rm -f {} \;

        # Alte Filenamen
        find /home/pacs/${PAC}/var \
          -name "ftp.log-????-??-??.gz" -mtime +${AGE} -daystart \
	  -maxdepth 1 -type f -exec rm -f {} \;
        ;;
      *)
        ;;
    esac
  fi
done


#
# Liste mit Paketname und möglichen Logverzeichnissen generieren
#
for DOMAIN in `doms_by_name '*'`; do
  dom_get ${DOMAIN}
  PAC="${DOM_OWNER:0:5}"

  if [ -d "/home/pacs/${PAC}" ]; then
    LOGDIR=`get-conf-parameter --pac=${PAC} --dom=${DOMAIN} --parameter=http_log_directory`
    test "$?" -ne "0" && LOGDIR="pac"

    case "${LOGDIR}" in
      [dD][oO][mM][oOaA][wWdD][nNmM][eEiI][rRnN])
        DIRECTORY=`grep "^${DOM_OWNER}:" /etc/passwd | cut -d: -f6`
        ;;
      [dD][oO][mM][aA][iI][nN])
        DIRECTORY=/home/doms/${DOMAIN}
        ;;
      *)
        DIRECTORY=/home/pacs/${PAC}
        ;;
    esac

    if [ -d "${DIRECTORY}/var" ]; then
      echo ${PAC} ${DIRECTORY}/var >>${TMPDIR}/dirlist
    fi
  fi
done


#
# Löschen von HTTP-Logs
#
if [ -s ${TMPDIR}/dirlist ]; then
  #
  # Zur Sicherheit eventuelle Duplikate rauswerfen
  #
  sort -u -o ${TMPDIR}/dirlist.sort ${TMPDIR}/dirlist

  while read PAC DIRECTORY; do
    find ${DIRECTORY} -name "web-*-????????-????.log.gz" -maxdepth 1 -type f |
      sed -e 's;^.*/web-\(.*\)-[0-9]\{8\}-[0-9]\{4\}\.log\.gz$;\1;' |
      sort -u >${TMPDIR}/domainlist

    cat ${TMPDIR}/domainlist >>${TMPDIR}/done

    while read DOMAIN; do
      PURGELOG=`get-conf-parameter --pac=${PAC} --dom=${DOMAIN} --parameter=http_log_purge`
      test "$?" -ne "0" && PURGELOG="yes"
 
      case "${PURGELOG}" in
        [Yy][Ee][Ss])
          #
          # Aufbewahrungszeit für Logfiles dieses Paketes holen
          # Bei fehlerhaften Werten wird der Default 48 Tage genommen.
          #
          typeset -i AGE
          AGE=`get-conf-parameter --pac=${PAC} --dom=${DOMAIN} --parameter=http_log_retention`
          test "$?" -ne "0" && AGE=48
          test "${AGE}" -gt "0" || AGE=48
 
          find ${DIRECTORY} -name "web-${DOMAIN}-????????-????.log.gz" \
            -mtime +${AGE} -daystart -maxdepth 1 -type f -exec rm -f {} \;
          ;;
        *)
          ;;
      esac
    done <${TMPDIR}/domainlist
  done <${TMPDIR}/dirlist.sort
fi

#wc -l ${TMPDIR}/done >>/root/tmp/domainlogs

rm -rf ${TMPDIR}

exit 0
