#!/sbin/sh
#
# @(#)imta	@(#)imta	1.32 04/29/99
# Copyright (c) 1996, 1997 Sun Microsystems, Inc.
# All Rights Reserved.
#
# imta - script to execute the various imta commands.
#


ARCH=`uname -p`

##################################################################
# define some exit codes
##################################################################
EX_USAGE=64

##################################################################
# we need to set LD_LIBRARY_PATH because for some reason
# the -R flag does not seem be working for all executables
# linked by the admin makefiles (like imtaDirsync)
##################################################################
LD_LIBRARY_PATH=/usr/lib:/opt/SUNWmail/lib:/opt/SUNWmail/imta/lib
export LD_LIBRARY_PATH

#
# we have to hard code the path to the tailor file.
#
#
IMTATAILOR=/etc/opt/SUNWmail/imta/imta_tailor


##################################################################
# give some initial default values to all paths.
##################################################################
MAILPATH=/opt/SUNWmail
IMTAPATH=/opt/SUNWmail/imta
IMTAOPTPATH=/etc/opt/SUNWmail/imta
IMTATMPPATH=/var/opt/SUNWmail/imta/tmp
IMTAQUEPATH=/var/opt/SUNWmail/imta/queue
IMTADBPATH=/var/opt/SUNWmail/imta/db

#default components for start,stop or restart
COMPONENT=all

##################################################################
# try to figure out the UID of the mail user.
# Default to inetmail if none is found.
##################################################################
IMTAUID=`pkgparam SUNWimmta MAILUSER 2>/dev/null`
if test -z "$IMTAUID" ; then
  IMTAUID=inetmail
fi

##################################################################
# shell function to get a variable's value
# from tailor file.
##################################################################
getTailorValue() {
 y=`/bin/grep "$1"= $IMTATAILOR |/bin/awk -F= '{print $2}'`
 echo $y
}


##################################################################
# shell function to get some values from tailor file.
##################################################################
getValuesFromTailor() {
  IMTAPATH=`getTailorValue IMTA_ROOT`
  IMTAOPTPATH=`getTailorValue IMTA_TABLE`
  IMTATMPPATH=`getTailorValue IMTA_SCRATCH`
  IMTAQUEPATH=`getTailorValue IMTA_QUEUE`
  IMTAUSER=`getTailorValue IMTA_USER`
  IMTAOPTIONFILE=`getTailorValue IMTA_OPTION_FILE`
  IMTAAUTORESIZE=`getTailorValue IMTA_CNBUILD_AUTORESIZE`
  if test -z "$IMTAAUTORESIZE" ; then
    IMTAAUTORESIZE=1
  fi
  MAILPATH=$IMTAPATH/..
}


##################################################################
# try to read and reinitialize all the paths from the tailor file
# if it exists.
##################################################################
if test -f $IMTATAILOR; then
  getValuesFromTailor
fi


IMTAPREFIX=IMTA
BINDIR=$IMTAPATH/lib
LAUNCH=$IMTAPATH/sbin/imta-launch

##################################################################
# shell function to backup IMTA configuration to a directory
# the is passed as the parameter (can be backup or default)
# All the files that are under $IMTAOPTPATH/* will be backed up to 
# $IMTATMPPATH/$1/*.
##################################################################
doBackUp() {

  cd $IMTATMPPATH

  if [ ! -d $1 ] 
  then
    mkdir $1
  else
    cd $1
    rm -rf *
  fi

  cd $IMTAOPTPATH
  cp -p -R $IMTAOPTPATH/* $IMTATMPPATH/$1
  status=`echo $?`
  
  echo "save to $1  is completed"
  exit $status

}

#################################################################
# shell function to restore the back'd up imta configuration.
# must be called with atleast one argument (backup or default).
#################################################################
doRestore() {
  
  echo "Stopping $IMTAPREFIX .."
  
  if [ -d $IMTATMPPATH/$1 ]
  then
    $IMTAPATH/sbin/imta stop
    echo "Saving current confguration ..."
    cd $IMTAOPTPATH
    tar -cvfp $IMTATMPPATH/imtaopt-"$1"-saved.tar * > /dev/null
    rm -rf *
    
    echo "Restoring from last backup ..."
    cp -p -R $IMTATMPPATH/$1/* $IMTAOPTPATH
    backup_status=`echo $?`
    if [ $backup_status -ne 0 ]; then
      echo "Restoration from backup failed"
      tar -xvfp $IMTATMPPATH/imtaopt-"$1"-saved.tar > /dev/null
    else
      chown $IMTAUSER:mail *   
      echo "Restoration from backup completed."
    fi

    echo "Rebuilding configuration data ..."
    $IMTAPATH/sbin/imta cnbuild
    status=`echo $?`
    if [ $status -eq 0 ]; then
      echo "Restarting $IMTAPREFIX ..."
      $IMTAPATH/sbin/imta start
      status=`echo $?`
    else
      tar -xvfp $IMTATMPPATH/imtaopt-"$1"-saved.tar > /dev/null
      echo "Error in backup configuration, restoration of backup removed."
      $IMTAPATH/sbin/imta start
    fi

    rm -rf $IMTATMPPATH/imtaopt-"$1"-saved.tar

    if [ $backup_status -eq 0 ]; then
      exit $status
    else
      exit $backup_status
    fi
  else
    echo "There is nothing to restore." 
    exit 1
  fi
}


#################################################################
# gets the pid of the parameter $1 in a special way
# and echoes it. used by dispps, jobps, smtpps commands.
#
#################################################################
getExitCode() {
  
  PIDLIST=`ps -ef| grep $1 | grep -v grep | awk '{print $2}'`
  PID=`echo ${PIDLIST} | awk '{print $1}'`
  echo $PID
  
  if [ "$PID" -gt 0 ]
  then EXIT=$PID
  else 
    EXIT=0
  fi
  
  echo $EXIT
  
}
#################################################################
#
#gets the uid of the user executing this script
#and tests whether it is IMTA_USER,root or other
#
#################################################################
verifyUid() {
  UID=`/usr/xpg4/bin/id -u`
  MTAUID=`/usr/xpg4/bin/id -u $IMTAUSER`
  if [ $UID -eq 0 ];
   then USER=0
  elif [ $UID -eq $MTAUID ];
   then USER=1
  else
	USER=2
  fi
}
#
#################################################################
#
#  Usage function for this script. Must be
#  called  on errors. Also serves as a central place
#  to document all available utilities.
#
#################################################################
# TODO: Fix usage script
usage() {
  echo ""
  echo "Usage:"
  echo ""
  echo "  imta cache < -close | -rebuild | -sync | -view >"
  echo "  imta chbuild"
  echo "  imta cnbuild"
  echo "  imta counters < -create | -clear | -delete | -show >"
  echo "  imta crdb <input-filename> <output-database-spec>"
  echo "  imta dirsync"
  echo "  imta dumpdb <input-database-spec> <output-filename>"
  echo "  imta process | processes"
  echo "  imta profile"
  echo "  imta program"
  echo "  imta purge"
  echo "  imta qm"
  echo "  imta queue"
  echo "  imta renamedb"
  echo "  imta restart"
  echo "  imta return"
  echo "  imta run"
  echo "  imta send"
  echo "  imta shutdown | stop"
  echo "  imta start | startup"
  echo "  imta submit | submit_master" 
  echo "  imta test < -mapping | -rewrite >"
  echo "  imta version"
  echo ""
}


#################################################################
#
#  The main switch statement in the script.
#
#################################################################
case $ARCH
in
  sparc )
    PATH=$PATH:/usr/ccs/bin
    export PATH
    ;;
  * )
    OS=OTHER
    ;;
esac

# Exit if no arguments
if [ $# = 0 ]; then
  usage
  exit $EX_USAGE
fi


first_arg=$1

if [ $first_arg = "processes" ]; then
  first_arg=process
elif [ $first_arg = "submit" ]; then
  first_arg=submit_master
elif [ $first_arg = "start" ]; then
  first_arg=startup
elif [ $first_arg = "shutdown" ]; then
  first_arg=stop
fi

case $first_arg in
  dirsync )
	verifyUid
	if [ $USER -ne 0 ];
	then echo "imta dirsync can be executed by root only"
	  exit 1
	fi
	
	# analyze arguments
	shift
	args=$*	      
	restart_mta=0
	dirsync_test=0
	show_usage=0
	while getopts csvh:P:FLl:b:td:i:rC:? c
	do
	  case $c in
	    F) restart_mta=1 ;;
	    t) dirsync_test=1 ;;
	    C) custom_methods=$OPTARG ;;
	    \?) show_usage=1 ;;
	  esac
	done 

	# do not restart the MTA if the alias and routing tables
	# are not rebuilt
	if [ ! -z "${custom_methods}" ] ; then
	  present=`echo "${custom_methods}" | grep -c BUILD_IMTA_TABLE`
	  if [ "$present" = "0" ] ; then
	    restart_mta=0
	  fi
	fi

	# If -\? specified, display usage and quit
	if [ $show_usage = 1 ]; then
	  su $IMTAUSER -c "$MAILPATH/imta/lib/imtaSync -\?"
	  exit 0
	fi
	
	# pass -E if enterprise version
	pkginfo -q SUNWbbmto
	status=`echo $?`
	if [ $status -eq 0 ] ; then
	  args="$args -E"
	fi	

	# run the actual alias synchronization
	su $IMTAUSER -c "$MAILPATH/imta/lib/imtaSync $args"
	status=`echo $?`
	if [ $status -eq 0 ] ; then
	  if [ $dirsync_test -eq 0 ] ; then 
	    if [ $restart_mta -eq 1 ] ; then  
	      # build the configuration again
	      # echo "Compiling the configuration ..."
	      su $IMTAUSER -c "$IMTAPATH/sbin/imta cnbuild"
	      status=`echo $?`
	      if [ $status -eq 0 ]; then
		# restart dispatcher  if already running.
		disp=`/bin/ps -f -u $IMTAUSER | grep dispatcher | grep -v grep`
		if [ ! -z "$disp" ];then
		  $LAUNCH -a  restart -c dispatcher -r $IMTAPATH
		fi
	      fi
	    fi
	  fi
	fi

	# return a useful exit code
	exit $status
	;;
    
  dispps)
	exit `getExitCode dispatcher` ;;
    
  jobps)
	exit `getExitCode job_controller` ;;

  smtpps)
	exit `getExitCode tcp_smtp_server` ;;
    
  dumpdb )
	verifyUid
	if [ $USER -eq 1 ]; then
	  $IMTAPATH/lib/imtacli $*
	  status=`echo $?`
	elif [ $USER -eq 0 ]; then
	  su $IMTAUSER -c "$IMTAPATH/lib/imtacli $*"
	  status=`echo $?`
	else
	  echo "imta dumpdb can be executed by $IMTAUSER only"
	  exit 1
	fi
	exit $status
	;;

  process )
	/usr/bin/ps -ef | grep imta | grep SUNWmail | grep -v process | \
	 grep -v "/sbin/sh"
	/usr/bin/ps -ef | grep ims_mast | grep -v grep
	exit 0
	;;

  program )
	# get rid of the first argument
	shift
	      
	args=
	while getopts adcluh:m:p:g:e: ch
	do 
	  case $ch in
	    a) args="$args -a";;
	    d) args="$args -d";;
	    c) args="$args -c";;
	    l) args="$args -l";;
	    u) args="$args -u";;
	    h) args="$args -h $OPTARG";;
	    m) args="$args -m $OPTARG";;
	    p) args="$args -p $OPTARG";;
	    g) args="$args -g \"$OPTARG\"";;
	    e) args="$args -e $OPTARG";;
	  esac
	done
	
	verifyUid
	if [ $USER -eq 1 ]; then
	  $MAILPATH/imta/lib/imtaProgram "$@"
	  status=`echo $?`
	elif [ $USER -eq 0 ]; then
	  su $IMTAUSER -c "$MAILPATH/imta/lib/imtaProgram $args"
	  status=`echo $?`
	else
	  echo "imta program can be executed by root or $IMTAUSER only"
	  exit 1
	fi
	exit $status
	;;

#  fix_permissions )
#	cd $IMTAOPTPATH/..
#	chown -R $IMTAUSER:bin imta
#	cd $IMTATMPPATH/../..
#	chown -R $IMTAUSER:bin imta
#	chown -R $IMTAUSER ims
#	;;

  purge )
	$IMTAPATH/lib/$*
	status=`echo $?`
	exit $status
	;;

  # ** NOTE: This is a SIMS internal function.
  #          DO NOT run by hand.
  backup )
	verifyUid
	if [ $USER -ne 0 ];
	then
	  echo "imta backup can be executed by root only"
	  exit 1
	fi
	doBackUp backup
	status=`echo $?`
	exit $status
	;;

  # ** NOTE: This is a SIMS internal function.
  #          DO NOT run by hand.
  restore )
	verifyUid
	if [ $USER -ne 0 ];
	then
	  echo "imta restore can be executed by root only"
	  exit 1
	fi
	doRestore backup
	status=`echo $?`
	exit $status
	;;

  # ** NOTE: This is a SIMS internal function.
  #          DO NOT run by hand.
  savedefault )
	verifyUid
	if [ $USER -ne 0 ];
	then
	  echo "imta savedefault can be executed by root only"
	  exit 1
	fi
	doBackUp default
	status=`echo $?`
	exit $status
	;;

  # ** NOTE: This is a SIMS internal function.
  #          DO NOT run by hand.
  restoredefault )
	verifyUid
	if [ $USER -ne 0 ];
	then echo "imta restoredefault can be executed by root only"
	  exit 1
	fi
	doRestore default
	status=`echo $?`
	exit $status
	;;

  restart )
	verifyUid
	if [ $USER -ne 0 ];
	then echo "imta restart can be executed by root only"
	  exit 1
	fi
	if [ $# -gt 1 ]
	then
	  COMPONENT=$2
	fi
	echo "Rebuilding configuration data ..."
	su $IMTAUSER -c "$IMTAPATH/sbin/imta cnbuild"
	status=`echo $?`
	if [ $status -eq 0 ]; then
	  $LAUNCH -a  restart -c $COMPONENT -r $IMTAPATH
	  status=`echo $?`
	  exit $status
	else
	  echo "configuration data error - MTA not restarted"
	  exit 1
	fi
	;;
  qm )
	verifyUid
	if [ $USER -ne 0 -a $USER -ne 1 ]; then
	  echo "imta qm can only be executed by root or inetmail"
	  exit 1
	fi
	shift
	$IMTAPATH/lib/imtacli qm -maint $*
	;;
  queue )
	case $2 in
	  -retry_delivery )
		verifyUid
		if [ $USER -eq 2 ]; then
		  echo "imta queue -retry_delivery can be executed by root or $IMTAUSER only"
		  exit 1
		fi
		if [ -z "$3" ]; then
		  echo
		  echo "Error: Channel name not specified"
		  echo "Usage: imta queue -retry_delivery <channel_name>"
		  echo
		  exit 1
		fi
		orig_dir=`pwd`
		queue=$IMTAQUEPATH/$3
		if [ ! -d $queue ]; then
		  echo "Channel $3 does not exist in system"
		  exit 1
		fi
		echo "Renaming the .HELD message files in channel $3's queue ..."
		for msg in `find $queue -name "*.HELD"`; do
		  mv $msg `dirname $msg`/`basename $msg .HELD`.00
		done
		if [ $USER -eq 0 ]; then
		  echo "Synchronizing the queue cache database ..."
		  su $IMTAUSER -c "$IMTAPATH/lib/imtacli cache -sync"
		  echo "Attempting delivery of stored messages ..."
		  su $IMTAUSER -c "$IMTAPATH/lib/run_channel $3"
		  cd $orig_dir
		else
		  echo "Synchronizing the queue cache database ..."
		  $IMTAPATH/lib/imtacli cache -sync
		  echo "Attempting delivery of stored messages ..."
		  $IMTAPATH/lib/run_channel $3
		  cd $orig_dir
		fi
		;;
	  -recover_crash )
		verifyUid
		if [ $USER -ne 0 ];
		then echo "imta queue -recover_crash can be executed by root only"
		  exit 1
		fi
		$LAUNCH -a stop -r $IMTAPATH
		/bin/rm -f $IMTAQUEPATH/../queue_cache/*
		echo "rebuilding the queue cache databases..."
		su $IMTAUSER -c "$IMTAPATH/sbin/imta cache -rebuild"
		su $IMTAUSER -c "$IMTAPATH/sbin/imta cache -close"
		;;
	  * )
		$MAILPATH/admin/lib/imtaQuUtil $*
		status=`echo $?`
		exit $status;;
	esac
	;;
  recover-crash )
	verifyUid
	prompt=1
	shift
	while getopts i ch
	do 
	  case $ch in
	    i) prompt=0;;
	  esac
	done
	
	if [ $USER -ne 0 -a $USER -ne 1 ]; then
	  echo "imta recover-crash can be executed by root or $IMTAUSER only"
	  exit 1
	fi
	# check and fix databases in /var/opt/SUNWmail/imta/db
	if [ -f $IMTAOPTPATH/.dirsync_unsafe ]; then
	  echo "Database may be corrupted"
	  echo "  Removing corrupted databases"
	  rm -f ${IMTADBPATH}/*
	  echo "  Restoring backup"
	  $IMTAPATH/sbin/imta dirsync -cv
	  status=$?
	  if [ $status -eq 0 ]; then
	    echo "  Backup restored"
	    if [ $prompt = 1 ]; then
	      echo "  Running incremental dirsync ... \c"
	      $IMTAPATH/sbin/imta dirsync
	      if [ $? = 0 ]; then
		echo "Done"
	      fi
	    else
	      echo "  Running incremental dirsync in the background"
	      $IMTAPATH/sbin/imta dirsync &
	    fi
	    exit 0
	  else
	    # dirsync -c fails
	    echo "\n*** Backup not available ***\n"
	    
	    if [ $prompt = 1 ]; then
	      # prompt user to perform dirsync -F
	      printf "Do you want to proceed with a full dirsync (Y/n)? "
	      read fullsync
	      if [ -z "$fullsync" ]; then
		fullsync=y
	      fi
	    else
	      fullsync=n
	    fi
	    
	    if [ $fullsync = y -o $fullsync = Y ]; then
	      $IMTAPATH/sbin/imta dirsync -F
	      status=$?
	      exit $status
	    else
	      echo "Database corrupted. Run imta dirsync -F to correct problem."
	      echo "MTA will not run until problem is rectified.\n"
		exit 1
	    fi
	    
	  fi
	else
	  echo "Database OK"
	  exit 0
	fi
	;;

  return )
	verifyUid
	if [ $USER -ne 1 ];
	then echo "imta return can be executed by $IMTAUSER only"
	  exit 1
	fi
	if [ $# -eq 1 ]
	then
	  echo "File name: \c"
	  read filename
	  cond=`echo "$filename" | grep \'*\'`
	  if [ ! -z "$cond" ]
	  then
	    filename=`echo "$filename" | tr -d \'`
	  fi
	  $IMTAPATH/lib/return return "$filename"
	  status=`echo $?`
	else
	  $IMTAPATH/lib/$1 $*
	  status=`echo $?`
	fi
	exit $status
	;;
  run )
	if [ $# -ge 1 ] ; then
	  shift
	  verifyUid
	  if [ $USER -eq 1 ]; then
	    $IMTAPATH/lib/run_channel $*
	    status=`echo $?`
	  elif [ $USER -eq 0 ]; then
	    su $IMTAUSER -c "$IMTAPATH/lib/run_channel $*"
	    status=`echo $?`
	  else
	    echo "imta run can be executed by $IMTAUSER only"
	    exit 1
	  fi
	fi
	exit $status
	;;
    
  startup )
	verifyUid
	if [ $USER -ne 0 ];
	then echo "imta startup can be executed by root only"
	  exit 1
	fi
	
	shift
	while getopts i c
	do
	  case $c in
	    i) recover_arg="-i"
	       shift;;
	  esac
	done 
	
	if [ $# -ne 0 ]
	then
	  COMPONENT=$1
	fi
	# Make sure databases are not corrupted
	$IMTAPATH/sbin/imta recover-crash $recover_arg
	if [ $? -ne 0 ]; then
	  echo "MTA not started"
	  exit 1
	fi
	
	echo "Rebuilding configuration data ..."
	su $IMTAUSER -c "$IMTAPATH/sbin/imta cnbuild"
	status=`echo $?`
	if [ $status -eq 0 ]; then
	  $LAUNCH -a  start -c $COMPONENT -r $IMTAPATH
	  status=`echo $?`
	  exit $status
	else
	  echo "configuration data error - MTA not restarted"
	  exit 1
	fi
	;;
  stop )
	verifyUid
	if [ $USER -ne 0 ]; then
	  echo "imta stop can be executed by root only"
	  exit 1
	fi
	if [ $# -gt 1 ]; then
	  COMPONENT=$2
	fi
	$LAUNCH -a  stop -c $COMPONENT -r $IMTAPATH
	exit 0
	;;

  submit_master )
	if [ $# -ge 1 ] ; then
	  shift
	  verifyUid
	  if [ $USER -eq 1 ]; then
	    $IMTAPATH/lib/submit_master $*
	    status=`echo $?`
	  elif [ $USER -eq 0 ]; then
	    su $IMTAUSER -c "$IMTAPATH/lib/submit_master $*"
	    status=`echo $?`
	  else
	    echo "imta run can be executed by $IMTAUSER only"
	    exit 1
	  fi
	fi
	exit $status
	;;

  version )
	$IMTAPATH/lib/version
	status=`echo $?`
	uname -a
	exit $status
	;;

  cnbuild )
	case $2 in
	  -sizes)
		verifyUid
		if [ $USER -eq 1 ]; then
		  $IMTAPATH/lib/cnbuild cnbuild -noimage_file -sizes
		  status=`echo $?`
		elif [ $USER -eq 0 ]; then
		  su $IMTAUSER -c "$IMTAPATH/lib/cnbuild cnbuild -noimage_file -sizes"
		  status=`echo $?`
		else
		  echo "imta cnbuild can be executed by root or $IMTAUSER only"
		  exit 1
		fi			
		;;
	  -remove)
		verifyUid
		if [ $USER -eq 1 ]; then
		  $IMTAPATH/lib/cnbuild cnbuild -remove
		  status=`echo $?`
		elif [ $USER -eq 0 ]; then
		  su $IMTAUSER -c "$IMTAPATH/lib/cnbuild cnbuild -remove"
		  status=`echo $?`
		else
		  echo "imta cnbuild can be executed by root or $IMTAUSER only"
		  exit 1
		fi			 
		;;
	  -statistics)
		verifyUid
		if [ $USER -eq 1 ]; then
		  $IMTAPATH/lib/cnbuild cnbuild -noimage_file -statistics
		  status=`echo $?`
		elif [ $USER -eq 0 ]; then
		  su $IMTAUSER -c "$IMTAPATH/lib/cnbuild cnbuild -noimage_file -statistic"
		  status=`echo $?`
		else
		  echo "imta cnbuild can be executed by root or $IMTAUSER only"
		  exit 1
		fi
		;;
	  -resize_tables)
		verifyUid
		$IMTAPATH/lib/cnbuild cnbuild -noimage_file -maximum -option_file=/tmp/option.cnbuild.tmp.$$
		status=`echo $?`
		if [ $status -eq 0 ]; then
		  if [ -f $IMTAOPTIONFILE ]; then
		    cat $IMTAOPTIONFILE | grep -v SIZE > /tmp/option.cnbuild.new.$$
		  fi
		  cat /tmp/option.cnbuild.tmp.$$ | grep SIZE | grep -v FRUITS >> /tmp/option.cnbuild.new.$$
		  if [ $USER -eq 1 ]; then
		    mv /tmp/option.cnbuild.new.$$ $IMTAOPTIONFILE
		    rm -f /tmp/option.cnbuild.tmp.$$
		    $IMTAPATH/lib/imtacli cnbuild
		    status=`echo $?`
		  elif [ $USER -eq 0 ]; then
		    mv /tmp/option.cnbuild.new.$$ $IMTAOPTIONFILE
		    chown $IMTAUSER $IMTAOPTIONFILE
		    rm -f /tmp/option.cnbuild.tmp.$$
		    su $IMTAUSER -c "$IMTAPATH/lib/imtacli cnbuild"		       
		    status=`echo $?`
		  else
		    rm -f /tmp/option.cnbuild.tmp.$$
		    echo "imta cnbuild can be executed by root or $IMTAUSER only"
		    exit 1
		  fi
		fi
                if [ $IMTAAUTORESIZE -eq 0 ]; then
                  echo "Your configuration tables have been modified."
                  echo "Please execute, imta restart, for the changes to take effect."
                fi
                exit $status
                ;;
	*)
		verifyUid
		if [ $USER -eq 1 ]; then
                  if [ $IMTAAUTORESIZE -eq 1 ]; then
	            $IMTAPATH/sbin/imta cnbuild -resize_tables
                    status=`echo $?`
                  else
                    $IMTAPATH/lib/imtacli cnbuild
                    status=`echo $?`
                  fi
                elif [ $USER -eq 0 ]; then
                  if [ $IMTAAUTORESIZE -eq 1 ]; then
                    su $IMTAUSER -c "$IMTAPATH/sbin/imta cnbuild -resize_tables"
                    status=`echo $?`
                  else
                    su $IMTAUSER -c "$IMTAPATH/lib/imtacli cnbuild"
                    status=`echo $?`
                  fi
                else 
                  echo "imta cnbuild can be executed by root or $IMTAUSER only"
                  exit 1
                fi
		if [ $status -ne 0 ]; then
		  echo "Error building configuration"
		  exit 2
		fi
		POST=`$IMTAPATH/lib/imtacli test -rewrite postmaster | grep "Address list error" 2>/dev/null`
		if [ -z "$POST" ]; then
		  echo "Configuration rebuilt"
		  exit 0
		else
		  echo "Illegal postmaster"
		  exit 1
		fi
		;;
	esac
	exit $status
	;;
    
  counters )
	verifyUid
	if [ $USER -ne 0 -a $USER -ne 1 ]; then
	  echo "imta counters can be executed by root or $IMTAUSER only"
	  exit 1
	fi
	$IMTAPATH/lib/imtacli $*
	status=`echo $?`
	exit $status
	;;

  dispatcher_stats_tty )
	verifyUid
	if [ $USER -ne 0 -a $USER -ne 1 ]; then
	  echo "imta counters can be executed by root or $IMTAUSER only"
	  exit 1
	fi
	$IMTAPATH/lib/dispatcher_stats_tty
	status=`echo $?`
	exit $status
	;;

  test )
	case $2 in
	  '-mapping' | '-rewrite' | '-match' )
		$IMTAPATH/lib/imtacli $*
		status=`echo $?`
		exit $status
		;;
	  * )
		echo "\nUsage: imta test < -mapping | -rewrite | -match >\n"
		exit $EX_USAGE
		;;
	esac
	;;

  cache )
	case $2 in
	  '-close' | '-rebuild' | '-sync' | '-view' )
		$IMTAPATH/lib/imtacli $*
		status=`echo $?`
		exit $status
		;;
	  * )
		echo "\nUsage: imta cache < -close | -rebuild | -sync | -view >\n"
		exit $EX_USAGE
		;;
	esac
	;;

  # all other commands that don't need special handling must be listed here
  chbuild | convertdb | crdb | profile | renamedb | find | view )
	$IMTAPATH/lib/imtacli $*
	status=`echo $?`
	exit $status
	;;

  * )
	usage
	exit $EX_USAGE
	;;
esac
