#!/bin/sh

# File:        sfdconf
#
# Author:      Ulli Horlacher (framstag@rus.uni-stuttgart.de)
#
# History:
#
#   1997-11-23	 Framstag	initial version
#   1998-01-04	 Framstag	added -r option
#
# The daemon configuration helper program for the sendfile package.
#
# Copyright  1997,1998 Ulli Horlacher
# This file is covered by the GNU General Public License

#SPOOL=`sendfile -qW=spool`
SPOOL=/var/spool/sendfile
INLOG=/var/spool/sendfile/LOG/in
OUTLOG=/var/spool/sendfile/LOG/out
CONFIG=/usr/local/etc/sendfile.cf
ALIASES=/usr/local/etc/sendfile.aliases
DENY=/usr/local/etc/sendfile.deny
ALLOW=/usr/local/etc/sendfile.allow
EDITOR=${EDITOR:=vi}
PAGER=${PAGER:=more}
PRG=`basename $0`
FILE=


usage() {
  echo "$PRG is the sendfiled configuration helper"
  echo "usage: $PRG OPTION ARGUMENT"
  echo "options:  -l  -- list"
  echo "          -e  -- edit"
  echo "          -i  -- initialize (= edit with default values)"
  echo "          -r  -- receiving mode"
  echo "arguments for options -l -e -i :"
  echo "          config   -- sendfiled configuration file"
  echo "          redirect -- system alias (redirection) file"
  echo "          allow    -- users allow-only file"
  echo "          deny     -- users deny file"
  echo "arguments for option -l :"
  echo "          inlog    -- input log file"
  echo "          outlog   -- output log file"
  echo "arguments for option -r :"
  echo "          enable   -- enable receiving"
  echo "          disable  -- disable receiving"
  echo "examples:"
  echo "          $PRG -l deny    # list the deny file"
  echo "          $PRG -e config  # edit the configuration file"
  exit
}


testfile() {
  error="`(cat $1 >/dev/null) 2>&1 |  sed 's/cat: //'`"
  if [ "$error" ]; then
    echo "%$PRG-Error: $error" >&2
    exit 1
  fi
}


init_append() {
  if [ -r $FILE ]; then
    cat <<EOD>> $FILE.tmp

## The next lines are from your old `basename $FILE` file. 
## You may want to delete them (if they are garbage).

EOD
    cat $FILE >> $FILE.tmp
  fi
  mv $FILE.tmp $FILE
}


init_aliases() {
  cat <<EOD> $FILE.tmp || exit 1
## This is the global aliases file for sendfiled. 
## It is used only for redirection of incoming files or messages!
## Text after a # is a comment and will be ignored.
## The syntax is: ALIAS USER[@HOST], examples:
# zrxh0370	framstag 
# root		admin@bigvax.saft.net

EOD
  init_append
}


init_config() {
  cat <<EOD> $FILE.tmp || exit 1
CONFIG
EOD
  init_append
}


init_deny() {
  cat <<EOD> $FILE.tmp || exit 1
## This is the exclusion list for sendfiled. 
## Users which are listed here are not allowed to receive files or messages.
## Warning: if sendfile.allow contains any user names it will be used as an
## allow-only list and this file here will be ignored.
## Text after a # is a comment.
daemon
bin
news
ftp
lpr
uucp
anonymous
nobody
OUTGOING
LOG
EOD
  init_append
}


init_allow() {
  cat <<EOD> $FILE.tmp || exit 1
## This is the allow-only list for sendfiled. 
## Only users which are listed here are allowed to receive files or messages.
## If this files contains no user names at all, it will be ignored and
## senfile.deny will be used instead as an exclusion list.
## Text after a # is a comment.
EOD
  init_append
}


#args=`getopt h?lei $*` || usage
#set -- x $args
#while shift; do
#  case $1 in
#    -\?|-h) usage;;
#    -l)	    list=true;;
#    -e)     edit=true;;
#    -i)     init=true;;
#    --)     shift; break;;
#  esac
#done

list=
edit=
init=
rmode=

case "$1" in
  -l) list=true;;
  -e) edit=true;;
  -i) init=true;;
  -r) rmode=true;;
  *)  usage;;
esac

if [ "$rmode" = true ]; then
  case "$2" in
    e|enable|y)		rmode=enable;;
    d|disable|n)	rmode=disable;;
    *)			rmode=show;;
  esac
else
  case "$2" in
    r|redirect)		FILE=$ALIASES;;
    c|conf|config)	FILE=$CONFIG;;
    a|allow)		FILE=$ALLOW;;
    d|deny)		FILE=$DENY;;
    i|inlog)		FILE=$INLOG; log=true;;
    o|outlog)		FILE=$OUTLOG; log=true;;
    *)			usage;;
  esac
fi


if [ "$rmode" ]; then
  status=0
  case "$rmode" in
    enable)	rm -f $SPOOL/.nosendfile; status=$?;;
    disable)	touch $SPOOL/.nosendfile; status=$?;;
  esac
  if [ -f $SPOOL/.nosendfile ]; then
    echo "receiving of files is disabled"
  else
    echo "receiving of files is enabled"
  fi
  exit $status
fi

if [ "$log" = true ]; then 
  if [ "$list" != true ]; then usage; fi
  testfile $FILE
  utf7decode $FILE | $PAGER  
  exit $?
fi

if [ "$list" = true ]; then 
  testfile $FILE
  $PAGER $FILE
  exit $?
fi

if [ "$edit" = true ]; then 
  if [ -f $FILE ]; then
    $EDITOR $FILE
    exit $?
  else
    echo "%$PRG-Error: $FILE does not exist" >&2
    echo "%$PRG-Info: you can create it with: $PRG -i $2" >&2
    exit 1
  fi
fi

if [ "$init" = true ]; then 
  trap "rm -f $FILE.tmp;exit" 1 2 3 15
  case $FILE in
    *aliases)		init_aliases;;
    *cf)		init_config;;
    *allow)		init_allow;;
    *deny)		init_deny;;
  esac
  $EDITOR $FILE
  exit $?
fi
