#!/bin/ksh
[ "${LOGNAME:-$USER}" = root ] ||
decho=echo
localnet_dscl_flags='-u diradmin -p'
localnet_dscl_domain='/LDAPv3/127.0.0.1'
skel_dir='/opt/local/etc/skel'
sieve_basedir='/Library/Server/Mail/Data/rules'
mail_group='mail'
comma2vbar(){
  for a in "$@"; do
    echo "$a" | sed -e "s/,/|/g"
  done
}
getguid(){
  dscl="dscl $localnet_dscl_domain"
  kv=$($dscl -read /Users/"$1" GeneratedUID 2> /dev/null) || {
    dscl="dscl ."
    kv=$($dscl -read /Users/"$1" GeneratedUID 2> /dev/null) || return 1
  }
  echo "$kv" | sed -ne 's/.* \(.*\)/\1/p'
}
print_usage(){
  me=$(basename "$0")
  cat <<EOF
$me - generate a dovecot.sieve script
usage:
        $me [-h|--help] [--dry-run] [--dscl '$localnet_dscl_flags'] [--domain $localnet_dscl_domain] -u|-account_name account_name [options]
        sudo $me -u|-account_name account_name [options]
options:
        -m|-mail_domain mail_domain
        -J|-sw_antispam on|off
        -t|-forward_to forward_to
        -g|-forwarding_allow_receivers forwarding_allow_receiver[,...]
        -d|-forwarding_deny_senders forwarding_deny_sender[,...]
authors:
        Copyright (C) 2013 Jun Takahashi <junt@aihara.co.jp>
EOF
}
init_params(){
  account_name=
  mail_domain='aihara.co.jp'
  guid=
  sw_antispam='off'
  sw_forwarding='off'
  forward_to=
  forwarding_allow_receivers=
  forwarding_deny_senders=
}
print_params(){
  cat <<EOF
account_name:  $account_name
mail_domain:   $mail_domain
guid:          $guid
sw_antispam:   $sw_antispam
sw_forwarding: $sw_forwarding
forward_to:    $forward_to
forwarding_allow_receivers: $forwarding_allow_receivers
forwarding_deny_senders:    $forwarding_deny_senders
EOF
  echo
}
set_params(){
  [ $# -gt 0 ] || { print_usage; exit 1; }
    while [ $# -gt 0 ]; do
    case "$1" in
    -h|--help) print_usage; exit 0;;
    --dry-run) decho=echo;;
    --dscl)    [ $# -gt 1 ] && shift && localnet_dscl_flags="$1";;
    --domain)  [ $# -gt 1 ] && shift && localnet_dscl_domain="$1";;
    -u|-account_name)               [ $# -gt 1 ] && shift && account_name="$1";;
    -m|-mail_domain)                [ $# -gt 1 ] && shift && mail_domain="$1";;
    -J|-sw_antispam)                [ $# -gt 1 ] && shift && sw_antispam="$1";;
    -t|-forward_to)                 [ $# -gt 1 ] && shift && forward_to="$1";;
    -g|-forwarding_allow_receivers) [ $# -gt 1 ] && shift && forwarding_allow_receivers="$1";;
    -d|-forwarding_deny_senders)    [ $# -gt 1 ] && shift && forwarding_deny_senders="$1";;
    esac
    shift
  done
  [ "$account_name" != "" ] || exit 1
  guid=$(getguid "$account_name") || exit 1
  [ "$forward_to" = "" ] || sw_forwarding='on'
  [ "$forwarding_allow_receivers" = "" ] || forwarding_allow_receivers=",$forwarding_allow_receivers"
  forwarding_allow_receivers=$(comma2vbar "\${LOGNAME}$forwarding_allow_receivers")
  [ "$forwarding_deny_senders" = "" ] || forwarding_deny_senders=",$forwarding_deny_senders"
  forwarding_deny_senders=$(comma2vbar "\${LOGANME}@$mail_domain$forwarding_deny_senders")
}
generate_script(){
  sed -e "
s/^set \"LOGNAME\" .*$/set \"LOGNAME\" \"$account_name\";/
s/^set \"SW_ANTISPAM\" .*$/set \"SW_ANTISPAM\" \"$sw_antispam\";/
s/^set \"SW_FORWARDING\" .*$/set \"SW_FORWARDING\" \"$sw_forwarding\";/
s/^set \"FORWARD_TO\" .*$/set \"FORWARD_TO\" \"$forward_to\";/
s/^set \"FORWARDING_ALLOW_RECEIVERS\" .*$/set \"FORWARDING_ALLOW_RECEIVERS\" \"$forwarding_allow_receivers\";/
s/^set \"FORWARDING_DENY_SENDERS\" .*$/set \"FORWARDING_DENY_SENDERS\" \"$forwarding_deny_senders\";/
" "$skel_dir"/dovecot.sieve
}
generate_file(){
  [ -d "$sieve_basedir" ] || exit 1
  sieve_dir="$sieve_basedir/$guid"
  [ -d "$sieve_dir" ] || {
    $decho mkdir "$sieve_dir"
    $decho chown "$account_name" "$sieve_dir"
    $decho chgrp "$mail_group" "$sieve_dir"
    $decho chmod g+w "$sieve_dir"
  }
  script_file="$sieve_dir"/dovecot.sieve-`/bin/date +%Y%m%d`
  $decho eval generate_script '>' "$script_file"
  $decho chown "$account_name" "$script_file"
  $decho sievec "$script_file" || exit 1
  $decho chown "$account_name" "$script_file".svbin
}
init_params     &&
set_params "$@" &&
print_params    &&
generate_file

