#!/bin/sh

# pfilter      Bring up/down packet filtering

# Copyright 2003 Neil Gorsuch
#
# This file is part of pfilter.
#
# pfilter 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.
#
# pfilter 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

#================================================================
# Since some of the parsing of system commands output is not
# language independant, we set the language temporarily to english

export LC_ALL=C

#================================================================
# Source function libraries 

# /etc/init.d/functions is in Redhat and Mandrake

[ -r /etc/init.d/functions ] && . /etc/rc.d/init.d/functions

# /etc/rc.status is in SuSE

[ -r /etc/rc.status ] && . /etc/rc.status

#================================================================
# chkconfig information blocks

# the following block is for RedHat/Mandrake

# chkconfig: - 11 92
# description: Loads/Unloads packet filtering rules
# probe: true

# the following block is for SuSE

### BEGIN INIT INFO
# Provides: pfilter
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Loads/Unloads packet filtering rules
### END INIT INFO

#==================================================================

RETVAL=0
exec_dir="/usr/sbin"
prog="pfilter"
cmds_dir="/etc"

#==================================================================

# Every time the pfilter service is started, stopped,
# restarted, the status is displayed, or the chain
# rules are displayed, pfilter re-compiles it's
# configuration file /etc/pfilter.conf into the
# commands file /etc/pfilter.cmds which is then
# executed. 
 
# If you don't need to change the pfilter configuration
# file /etc/pfilter.conf, and you want to modify or
# customize the commands file /etc/pfilter.cmds, you
# can uncomment the following line.  This will prevent
# pfilter from being run and will allow your modified
# commands file to be run as the pfilter service.
# The better and easier way to customize the commands
# file /etc/pfilter.cmds is to use the LITERAL and
# ENDLITERAL directives in the pfilter configuration file
# /etc/pfilter.conf with your desired shell commands
# in between the LITERAL and ENDLITERAL directives.
# See the configuration file for examples on how to do this.

# if [ -x "$cmds_dir/pfilter.cmds" ] then ; prog="pfilter.cmds" ; fi

#==================================================================

# make sure the pfilter program (or previous commands file) is there

if [ ! -x $exec_dir/$prog ] ; then
  echo -n "pfilter not installed ! "
  exit 5
fi

#==================================================================

# for Redhat and Mandrake and similar systems we do things this way:

if [ -r /etc/init.d/functions ] ; then

  case "$1" in
    start)
      echo -n $"Starting $prog:"
      $exec_dir/$prog start && success || failure
      RETVAL=$?
      echo
      ;;
    stop)
      echo -n $"Stopping $prog:"
      $exec_dir/$prog stop && success || failure
      RETVAL=$?
      echo
      ;;
    restart)
      echo -n $"Restarting $prog:"
      $exec_dir/$prog restart && success || failure
      echo
      RETVAL=$?
      ;;
    status)
      $exec_dir/$prog status 
      RETVAL=$?
      ;;
    chains)
      $exec_dir/$prog chains
      RETVAL=$?
      ;;
    *)
      echo "Usage: $0 {start|stop|restart|status|chains}"
      exit 1
  esac
  exit $RETVAL

# for SuSE and similar systems we do things this way:

elif [ -r /etc/rc.status ] ; then

  rc_reset
  case "$1" in
    start)
      echo -n $"Starting $prog "
      $exec_dir/$prog start
      rc_status -v
      ;;
    stop)
      echo -n $"Stopping $prog "
      $exec_dir/$prog stop
      rc_status -v
      ;;
    restart)
      echo -n $"Restarting $prog "
      $exec_dir/$prog restart
      rc_status -v
      ;;
    status)
      $exec_dir/$prog status 
      exit 0
      ;;
    chains)
      $exec_dir/$prog chains
      exit 0
      ;;
    *)
      echo "Usage: $0 {start|stop|restart|status|chains}"
      exit 1
  esac
  rc_exit

# default way to do things when we do not recognize environment

else

  case "$1" in
    start)
      echo -n $"Starting $prog "
      $exec_dir/$prog start
      exit $?
      ;;
    stop)
      echo -n $"Stopping $prog "
      $exec_dir/$prog stop
      exit $?
      ;;
    restart)
      echo -n $"Restarting $prog "
      $exec_dir/$prog restart
      exit $?
      ;;
    status)
      $exec_dir/$prog status 
      exit $?
      ;;
    chains)
      $exec_dir/$prog chains
      exit $?
      ;;
    *)
      echo "Usage: $0 {start|stop|restart|status|chains}"
      exit 1
  esac

fi
