#  Besides this example, also can look at oldfiles and monthno (e.g., for case) and other stuff on ~/consult/risetime


#!/bin/bash
#
# Appliance_Transfer_Email_Init.sh
#
# Author: R. Greenberg
#
# Date:   16-Jun-2005
#
# Version History
#
# Version     Name           Date         Changes
# _______   _______________  ______   _________________________________________
#  1.0       R. Greenberg    6/15/05     Initial Release
#  1.1       R. Greenberg    6/16/05     Incorporate special handling for root
# ___________________________________________________________________________
#
# Purpose
#
# Perform initial setup so that email directed to certain users will
# be transfered by ssh to the DataCenter and delivered there.  The
# users that this script applies to are panadmin and root by default
# or the users whose home directories are arguments after the first
# (with user name assumed to be the part of the directory name after
# the last slash).
#
#       Input Parameters: DataCenterIP and, optionally, home directories of users this
 script applies to
#       Example:          192.168.2.41 ~panadmin ~root
#
# ___________________________________________________________________________
#

# Main Body Here
#

#  Set up variables
#
datacenterIP=$1
shift
userhomes=(~panadmin ~root)
if ((${#*})); then userhomes=($*); fi
stagingpar=$HOME/dcmail/staging
archivepar=$HOME/dcmail/archive
dcdeliverypar='$HOME/appmail/from_'`hostname`

ln -fs `which procmail` /etc/smrsh

for userhome in ${userhomes[*]}; do
  user=${userhome##*/}
  if [[ $user == root ]]; then
    ln -fs /var/spool/mail/rootmail/root/.forward $userhome
    userhome=/var/spool/mail/rootmail/root
  fi
  deliverydir=$userhome/mail
  stagingdir=$stagingpar/$user
  archivedir=$archivepar/$user
  dcdeliverydir="$dcdeliverypar"/$user

  ssh $datacenterIP mkdir -p $dcdeliverydir

  mkdir -p $deliverydir $stagingdir $archivedir
  if [[ $user == root ]]; then
    chown mail: $deliverydir
    touch $userhome/procmaillog
    chown mail: $userhome/procmaillog
  fi
  cat > $userhome/.procmailrc <<-EOF
        SHELL=/bin/sh
        LOGFILE=$userhome/procmaillog

        :0
        $userhome/mail
        EOF
  cat > $userhome/.forward <<-EOF
        "| /usr/bin/procmail $userhome/.procmailrc"
        EOF

done
