#!/bin/sh

# File:        sfconf
#
# Author:      Ulli Horlacher (framstag@rus.uni-stuttgart.de)
#
# History:
#
#   22 Nov 1997 Framstag	initial version
#   23 Nov 1997 Framstag	substituted "-" with "_" in all identifiers
#   10 Jan 1998 Framstag	added option -m
#
# The configuration helper program for the sendfile package.
#
# Copyright  1997,1998 Ulli Horlacher
# This file is covered by the GNU General Public License

SPOOL=/var/spool/sendfile/`whoami`
CONFIG=$SPOOL/config
EDITOR=${EDITOR:=vi}
PAGER=${PAGER:=more}
PRG=`basename $0`
TMP=$CONFIG/$$.tmp
FILE=


usage() {
  echo "$PRG is the sendfile user configuration helper"
  echo "usage: $PRG OPTION ARGUMENT"
  echo "options:  -l  -- list"
  echo "          -e  -- edit"
  echo "          -i  -- initialize (= edit with default values)"
  echo "          -m  -- message receiving"
  echo "arguments for options -l -e -i :"
  echo "          config  -- user configuration file"
  echo "          aliases -- user alias file"
  echo "          filter  -- user restrictions file"
  echo "arguments for option -l :"
  echo "          log     -- sendfile log file"
  echo "          msglog  -- sendmsg log file"
  echo "arguments for option -m :"
  echo "          all     -- messages can be written on all ttys"
  echo "          here    -- messages can be written only on this tty"
  echo "examples:"
  echo "          $PRG -l aliases  # list the aliases file"
  echo "          $PRG -m here     # messages will be written only on this tty"
  exit
}


testfile() {
  if [ ! -f "$1" ]; then
    echo "%$PRG-Error: $1 does not exist" >&2
    exit 1
  fi
  if [ ! -r "$1" ]; then
    echo "%$PRG-Error: $1 is not readable by you" >&2
    exit 1
  fi
}


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

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

EOD
    cat $FILE >> $TMP
  fi
  mv $TMP $FILE
}


init_aliases() {
  cat <<EOD> $TMP
## This is the user aliases file for sendfile/sendmsg. 
## The syntax is: ALIAS ADDRESS, example:
frams framstag@bofh.belwue.de

EOD
  init_append
}


init_config() {
  cat <<EOD> $TMP
## This is the user configuration file for sendfile/sendmsg. 
## Text after a # is a comment and will be ignored.

# ring the gong when a message arrives (on/off)
bell = on

# allow remote sender to delete his files afterwards (on/off)
deleting = on

# logging of incoming messages in the user log file (on/off)
msglog = off

# notification by message, mail, both or none when a file arrives
# you may specify a second argument as a recipients address
# notification = mail tome@somewhere.inthe.net
notification = message

# enforce secure incoming transfer with pgp (sign/encrypt/both/none)
forcepgp = none

# forward incoming files to another SAFT address
# forward = tome@on.another.org

# if you have an O-SAFT account, specify it here
# (the fetchfile client will use it)
# o-saft = othername@saft.banana.net

EOD
  init_append
}


init_restrictions() {
  cat <<EOD> $TMP
## This is the user restrictions file for sendfile/sendmsg. 
## It contains addresses from where you don't want messages or files. 
## The format is: user@host [mfb]
## m stands for messages, f for files and b for both. 
## Wildcards * and ? are allowed. Examples:
## bgates@microsoft.com b
## *aol.com m
## Text after a # is a comment, like these first lines.

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

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

if [ "$msg" = true ]; then
  case "$2" in
    a|all)		sendmsg -M;;
    h|here|t|this)	sendmsg -m;;
    *)			usage;;
  esac
  exit;
fi

case "$2" in
  a|alias|aliases)		FILE=$CONFIG/aliases;;
  c|conf|config)		FILE=$CONFIG/config;;
  f|filter|r|restrictions)	FILE=$CONFIG/restrictions;;
  l|log)			log=true; FILE=$SPOOL/log;;
  m|msg|msglog)			log=true; FILE=$SPOOL/msglog;;
  *)				usage;;
esac

if [ "$log" = true ]; then 
  if [ "$edit" = true ]; then usage; fi
  testfile $FILE
  awk '{if (NR>1 || $1!="#") print $0}' $FILE | utf7decode | $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 $TMP;exit" 1 2 3 15
  case $FILE in
    *aliases)		init_aliases;;
    *config)		init_config;;
    *restrictions)	init_restrictions;;
  esac
  $EDITOR $FILE
  exit $?
fi
