#!/bin/sh
# This script is designed to be called from webmin, in order to
# forward email for one user to another (via a .forward file).
# Arguments: $1 = username, $2 = email address to fwd to.
#
# Yeah, this isn't secure, as we pass strings directly to the
# shell to be executed.  Needs to be fixed, perhaps by a Perl
# version of the script. :O
#
# v0.1 -        Paul Kreiner, 1/27/2003

if [ "$1" = "" ]; then
  echo "ERROR: A username was not provided.  Please try again."
  echo "Usage: $0 <username> <forwarding_address>"
  echo
  exit 4
fi

if [ -d /home/$1/ ]; then
  echo $2 > /home/$1/.forward 2> /dev/null
  chown $1 /home/$1/.forward 2>&1 > /dev/null

  if [ -w /home/$1/.forward ]; then

    if [ "$2" = "" ]; then
      rm -f /home/$1/.forward 2>&1 > /dev/null

      if [ -e /home/$1/.forward ]; then
        echo "ERROR: Unable to delete file /home/$1/.forward.  Aborting."
        exit 3
      else
        echo "SUCCESS: Disabled email forwarding for user ($1)."
        exit 0
      fi

    else
      echo "SUCCESS: Mail forwarding for user ($1) set up."
      exit 0
    fi

  else
    echo "ERROR: Error writing .forward file.  Aborting."
    exit 2
  fi

else
  echo "ERROR: The user's home directory (/home/$1) does not exist.  Aborting."
  exit 1
fi


