#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_syslog,v 1.18 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005           SUSE GmbH, Nuernberg, Germany
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Author: Fabian Herschel
#
#############################################################################
#
# make_syslog
# configures the syslog
#
# Sections:
# Tags:     SYSLOGLIST, ALICE_LOGGER_FACILITY, ALICE_LOGGER_ACTION_*
#
############################################################################
#
# NOTES:
#
############################################################################
#
#
usage()
{
   echo "$_self -h | --help | -?"
   echo "$_self -fqhn fqhn"
}
std_syslog_conf=/etc/syslog.conf

test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir

. $alice_dir/lib/alicerc

WELCOME
#
############################################################################
#
while [ $# -gt 0 ]
do
   case $1 in
       -h | --help | -? ) usage
                          exit 1
                          ;;
       -fqhn ) fqhn=$1; shift
               ;; 
   esac
   shift
done
#
############################################################################
#
#
# save the old configuration
#
BACKUP --error ${std_syslog_conf}
#
# First priority is on a special syslog.con file
#
#config_file=$(get_config_file syslog.conf) || FATAL NOCFG syslog.conf
#. ${config_file}
GET_CONFIGURATION --require syslog $CFG_DEBUG
if [ -z "$SYSLOGLIST" ]
then
#   FATAL NOVALUE SYSLOGLIST $config_file
    GOOD_BYE
fi
(
     echo "# ${std_syslog_conf}"
     echo "# generated by alice"
     echo "#"
     echo "${SYSLOGLIST}" 
     #
     # Now let us set the alice logger for syslog
     #
     if [ -n "$ALICE_LOGGER_FACILITY" ]  # i.e. local0
     then
        if [ -n "$ALICE_LOGGER_ACTION_ALL" ]
        then
           for action in $ALICE_LOGGER_ACTION_ALL
           do
              echo "$ALICE_LOGGER_FACILITY.* $action"
           done
        fi
        if [ -n "$ALICE_LOGGER_ACTION_ERR" ]
        then
           for action in $ALICE_LOGGER_ACTION_ALL
           do
              echo "$ALICE_LOGGER_FACILITY.err $action"
           done
        fi
        if [ -n "$ALICE_LOGGER_ACTION_WARN" ]
        then
           for action in $ALICE_LOGGER_ACTION_ALL
           do
              echo "$ALICE_LOGGER_FACILITY.warn $action"
           done
        fi
     fi
) > ${std_syslog_conf}.alice 
#
# maybe some syslog.conf entries refer to direcories or files, which do
# not exist
#
cat ${std_syslog_conf}.alice | awk '/^[^#]/ { print $2 }' | while read ACTION
do
    case $ACTION in
           /* |\
          -/*    )       
                    #
                    # the action seams to be a normal file
                    #
                    ACTION=${ACTION#-}
                    DIR_ACTION=$(dirname $ACTION)
                    if [ ! -d $DIR_ACTION ]
                    then
                       mkdir -p $DIR_ACTION
                    fi
                    touch $ACTION 
                    ;;
           "@"*  )
                    #
                    # the action seams to be a host name
                    #
                    ACTION=${ACTION#@} 
                    ping -c3 $ACTION 2>&1 1>/dev/null || WARNING NOTREACHABLE $ACTION
                    ;;
           "|"*  )       
                    #
                    # the action seams to be a fifo pipe
                    #
                    DIR_ACTION=$(dirname $ACTION)
                    if [ ! -d $DIR_ACTION ]
                    then
                       mkdir -p $DIR_ACTION
                    fi
                    if [ ! -p $ACTION ]
                    then
                       if [ -e $ACTION ]
                       then
                          #
                          # ups, the ACTION exists but is no pipe
                          #
                          ERROR ISNOTAPIPE $ACTION
                       else
                          mkfifo $ACTION    
                       fi
                    fi
                    ;;
    esac
done
#
# to install and activate
#
CHANGED ${std_syslog_conf} || {
   BACKUP ${std_syslog_conf}
   mv ${std_syslog_conf}.alice ${std_syslog_conf} 
   #
   # Now we invite syslog to reload the information
   #
   if [ -x $rc_d_dir/syslog ]
   then
      if [ "$($rc_d_dir/syslog status)" = "OK" ]
      then
         $rc_d_dir/syslog reload
      else
         $rc_d_dir/syslog start
      fi
   else
      FATAL NOEXE $rc_d_dir/syslog 
   fi
}


#
############################################################################
# Thats all
GOOD_BYE 
