#!/bin/sh

# File:        sfconf
#
# Author:      Ulli Horlacher (framstag@rus.uni-stuttgart.de)
#
# History:
#
#   1997-11-22	 Framstag	initial version
#   1997-11-23	 Framstag	substituted "-" with "_" in all identifiers
#   1998-01-10	 Framstag	added option -m
#   1998-03-15	 Framstag	look for spool and configdir in $HOME
#   1998-07-11	 Framstag	added notify program support
#   2005-05-30	 Framstag	better USER detection
#
# The configuration helper program for the sendfile package.
#
# Copyright  1997-2005 Ulli Horlacher
# This file is covered by the GNU General Public License

if [ "$LOGNAME" ]; then 
  USER=$LOGNAME
else
  USER=`id | sed 's/[()]/ /g' | awk '{print $2}'`
fi
SPOOL=$HOME/.sfspool
test -d $SPOOL || SPOOL=/var/spool/sendfile/$USER
CONFIG=$HOME/.sendfile
EDITOR=${EDITOR:=vi}
PAGER=${PAGER:=more}
PRG=`basename $0`
TMP=$CONFIG/$$.tmp
FILE=

if [ ! -d $CONFIG ]; then
  if [ -d $SPOOL/config ]; then
    ln -s $SPOOL/config $CONFIG
  else
    mkdir $CONFIG
  fi
fi

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 "          notify  -- notify program for file receiving"
  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.
# Extra: when you specify "notification = program" then the notify-program
# in your sendfile configuration directory will be executed.
# Type "sfconf -i notify" for an example.
# notification = mail tome@somewhere.inthe.net
# notification = program
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

# default compression methode (none/gzip/bzip2)
defaultcompress = gzip

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
}


init_notify() {
  cat <<EOD> $TMP
#!/bin/sh

# This program will be called whenever someone has sent you some files AND
# when you have set the option "notification = program" with "sfconf -e config".
#
# The shell-argument \$1 contains the name of the sender, 
# in \$2..\$n are the spool file numbers.
# This here is just an example what you can do!

from=\$1; shift
n=\`echo \$* | wc -w\`

sendmsg -f -s "\$n new file(s) from \$from!" \$USER

exit
EOD
  chmod 700 $TMP
  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;;
  n|notify)			FILE=$CONFIG/notify
  				if [ $USER = root ]; then 
				  echo "%$PRG-Error: for security reasons, root is not allowed to use a notify program" >&2
				  exit 1
				fi;;
  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;;
    *notify)		init_notify;;
  esac
  $EDITOR $FILE
  exit $?
fi
