#!/bin/sh
# rpm helper scriptlet to add an entry into default syslog implementation
# $Id$

item_in_list() {
   list=$1
   search_item=$2
   found=0

   for item in $list; do
      if [ $item = $search_item ]; then
         echo $found
         return
      fi
      found=$(($found + 1))
   done

   echo "-1"
}

add_rsyslog_entry() {

    package=$1
    source=$2
    dest=$3
    facility=$4
    min=$5
    max=$6

    # compute selector
    selector=$( get_selector $facility $min $max )

    # append entry
    out="/etc/rsyslog.d/$package.conf"

    echo "# Automatically added by $package installation" > $out

    if [ ! -f $out ]; then
        echo "Can't open /etc/rsyslog.d/$package.conf for writing."
        exit 1
    fi

    if [ $source != '/dev/log' ]; then
      echo "\$AddUnixListenSocket $source" >> $out;
    fi
    echo "$selector	-$dest" >> $out;

    # relaunch rsyslog
    service rsyslog condrestart 2>&1 >/dev/null
}

add_new_source() {
    source=$1
    file=$2

    changed=0
    content=""

    if [ !-f "$file" ]; then
        echo "Can't open $file for reading"
        exit 1
    fi

    while read LINE
    do
        if [ $line =~ /^SYSLOGD_OPTIONS=\(.*\)/ ]; then
            options=$1
            if [ ! -z $options ]; then
               if [ $options !~ /-a\s+$source/ ]; then
                   if [ $options =~ /^\([\"\']\)\(.*\)\1$/ ]; then
                       quote = $1
                       options = $2
                   else
                       quote = '"'
                   fi
               fi
            else
               options="\"-a $source\""
               changed=1
            fi
            content="${content}SYSLOGD_OPTIONS=$options\n"
        else
            content="${content}$line"
        fi
    done < $file

    if [ $changed -eq 1 ]; then
        echo $content > $out
    fi
}

get_selector() {
    facility=$1
    min=$2
    max=$3
    progress=0

    if [ "$max" = 'emerg' ]; then
        if [ "$min" = 'debug' ]; then
            selector="$facility.*"
        else 
            selector="$facility.$min"
        fi
    else
        for item in $PRIORITIES; do
            if [ "$item" = "$min" ]; then
              progress=1
            fi

            if [ ! -z "$selector" ]; then
              selector="$selector;"
            fi

            if [ "$progress" -eq "1" ]; then
              selector="$selector$facility.=$item"
            fi
            
            if [ "$item" = "$max" ]; then
              progress=0
            fi
        done
    fi

    echo $selector;
}


FACILITIES="auth authpriv cron daemon kern lpr mail mark news syslog user uucp local0 local1 local2 local3 local4 local5 local6 local"
PRIORITIES="debug info notice warning err crit aler emerg"


source=/dev/log
min=debug
max=emerg

while [ "$1" != "${1##[-=]}" ]; do
  case $1 in 
    '-s')
         source=$2
         shift 2
         ;;
    '-m')
         min=$2
         shift 2
         ;;
    '-M')
         max=$2
         shift 2
         ;;
  esac
done

if [ $# -lt 4 ]; then
    echo "
usage: $0 [options] <pkg> <nb> <facility> <dest>
Available options:
-s <source>   source (default: /dev/log)
-m <priority> min priority (default: debug)
-M <priority> max priority (default: emerg)
"
    exit 1
fi

package=$1
number=$2
facility=$3
dest=$4

    # don't do anything for upgrade
if [ $number -eq 2 ]; then
  exit 0
fi

# check arguments
found_facility=$(item_in_list "$FACILITIES" $facility)
if [ ! -z "$facility" ] && [ "$found_facility" == "-1" ]; then
   echo "invalid facility '$facility'"
   exit 1
fi

min_occurrence=$(item_in_list "$PRIORITIES" $min)
echo "Min: $min_occurrence"
if [ ! -z "$min" ] && [ "$min_occurrence" == "-1" ]; then
   echo "invalid min priority '$min'"
   exit 1
fi

max_occurrence=$(item_in_list "$PRIORITIES" $max )
echo "Max: $max_occurrence"
if [ ! -z "$max" ] && [ "$max_occurrence" == "-1" ]; then
   echo "invalid max priority '$max'"
   exit 1
fi

if [ "$min_occurrence" != "-1" ] && [ "$max_occurrence" != "-1" ] && (( "$max_occurrence" < "$min_occurrence" )); then
   echo "maximum priority '$max' lower than minimum priority '$min'"
   exit 1
fi

add_rsyslog_entry $package  $source $dest $facility $min $max;
