メール転送設定スクリプト

qmail+vpopmailで、ユーザに転送設定するスクリプトを書いてみました。
ちなみに前提は次の通りです。
・vpopmailパス
/home/vpopmail
・vpopmailユーザ
vpopmail
・vpopmailグループ
vchkpw

[setfwrd]

#!/bin/sh

if [ -z $1 ]; then
        echo "please input your name."
        exit 1
fi

if [ -z $2 ]; then
        echo "please input your domain."
        exit 1
fi

if [ -z $3 ]; then
        echo "please specify the spool flag."
        exit 1
fi

name=`echo $1 | tr '[A-Z]' '[a-z]'`
domain=$2
is_spool=$3

flg=0

if [ -d /home/vpopmail/domains/$domain/$name ]; then
        flg=1
        datapath=/home/vpopmail/domains/$domain/$name
else
        count=0
        count_max=20
        while [ $count -le $count_max ]; do
                if [ -d /home/vpopmail/domains/$domain/$count/$name ]; then
                        flg=1
                        datapath=/home/vpopmail/domains/$domain/$count/$name
                        count=$count_max
                fi
                count=`expr $count + 1`
        done
fi

if [ $flg -eq 1 ]; then
        if [ -f $datapath/.qmail ]; then
                mv -f $datapath/.qmail $datapath/.qmail.bak
        fi

        touch $datapath/.qmail

        while [ "$4" != "" ]; do
                echo "&$4" >> $datapath/.qmail
                shift 1
        done

        if [ $is_spool != "0" ]; then
                echo "$datapath/Maildir/" >> $datapath/.qmail
        fi

        chown vpopmail:vchkpw $datapath/.qmail
else
        echo "specified account not found."
fi

使い方は、

setfwrd [ユーザ名] [ドメイン名] [メールを残す=1 残さない=0] [転送先1] [転送先2] [転送先3]...

setfwrd taro.yamada foo.com 1 t.yamada@bar.com taro@hoge.com

みたいな感じです。