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


ARCH=`uname -p`

##################################################################
# 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`
  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() {
  
  
  if [ -d $IMTATMPPATH/$1 ]
  then
    echo "Stopping $IMTAPREFIX .."
    $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.
#
#################################################################
usage() {
  echo ""
  echo "Utilities available:"
  echo "  backup "
  echo "  cache "
  echo "  chbuild"
  echo "  cnbuild"
  echo "  counters"
  echo "  crdb"
  echo "  dirsync"
  echo "  dumpdb"
  echo "  fix_permissions"
  echo "  notary_locale"
  echo "  process | processes"
  echo "  profile"
  echo "  program"
  echo "  purge"
  echo "  queue"
  echo "  recover-crash"
  echo "  renamedb"
  echo "  restart"
  echo "  restore"
  echo "  restoredefault"
  echo "  return"
  echo "  run"
  echo "  savedefault"
  echo "  send"
  echo "  shutdown | stop"
  echo "  start | startup"
  echo "  submit | submit_master" 
  echo "  test"
  echo "  version"
  echo " "
}


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

if [ $# -gt 0 ]
then
  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=$*	      
	      dirsync_full=0
	      while getopts tFcELvl:d:h:b:p: c
	      do
		case $c in
		  F) dirsync_full=1 ;;
		  \?) ;;
		esac
	      done 

	      # run the actual alias synchronization
	      su $IMTAUSER -c "$MAILPATH/imta/lib/imtaDirsync $args"
	      status=`echo $?`
	      if [ $status -eq 0 ] ; then
		if [ $dirsync_full -eq 1 ] ; then
		  # remove the dl authentication database
		  AUTHDL=`getTailorValue IMTA_AUTHDL_DATABASE`
		  if [ ! -z "$AUTHDL" ]; then
		    /bin/rm $AUTHDL.* 2> /dev/null
		  fi
		  
		  # 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 -ef | grep $IMTAUSER | grep dispatcher | grep -v grep`
		    if [ ! -z "$disp" ];then
		      $LAUNCH -a  restart -c dispatcher -r $IMTAPATH
		    fi
		  fi
		  # get new dl directory name  
		  ls $IMTAOPTPATH/.dl > /dev/null 2>&1
		  status=`echo $?`
		  if [ $status -eq 0 ]; then
		    new_dl=`/bin/grep "DL_SUBDIR"= $IMTAOPTPATH/.dl |/bin/awk -F= '{print $2}'` 2>/dev/null
		    dl_path=`/bin/grep "DL_DIR"= $IMTAOPTPATH/.dl |/bin/awk -F= '{print $2}'` 2>/dev/null
		    
		    #remove all old dl directories
		    for file in `ls $dl_path`; do
		      if [ $file -ne $new_dl ]; then
			su $IMTAUSER -c "rm -rf $dl_path/$file" 2> /dev/null
		      fi
		    done
		  fi
		fi
	      fi
	      
	      # return a useful exit code
	      exit $status
	      ;;
    
    dispps)
	    exit `getExitCode dispatcher` ;;
    
    jobps)
	   exit `getExitCode job_controller` ;;
    smtpps)
	    exit `getExitCode SMTP` ;;
    
    dumpdb )
	     verifyUid
	     if [ $USER -eq 1 ]; then
	       $IMTAPATH/lib/dbutil dbutil $*
	       status=`echo $?`
	     elif [ $USER -eq 0 ]; then
	       su $IMTAUSER -c "$IMTAPATH/lib/dbutil dbutil $*"
	       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 grep | \
	      grep -v "/sbin/sh"
	      /usr/bin/ps -ef | grep SMTP | grep -v grep
	      /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
		      ;;

    notary_locale )
		    if [ $# -lt 2 ]
		    then
		      echo "Please specify a locale"
		      exit 1
		    fi
		    verifyUid
		    TMPTAILOR=/tmp/tailor.notary
		    if [ $USER -eq 1 ]; then
		      sed -e '/IMTA_LANG=/d' $IMTATAILOR > $TMPTAILOR
		      echo "IMTA_LANG=/etc/opt/SUNWmail/imta/locale/$2/LC_MESSAGES/" >> $TMPTAILOR
		      mv $TMPTAILOR $IMTATAILOR
		      rm -f $TMPTAILOR
		    elif [ $USER -eq 0 ]; then
		      sed -e '/IMTA_LANG=/d' $IMTATAILOR > $TMPTAILOR
		      echo "IMTA_LANG=/etc/opt/SUNWmail/imta/locale/$2/LC_MESSAGES/" >> $TMPTAILOR
		      mv $TMPTAILOR $IMTATAILOR
		      (chown $IMTAUSER $IMTATAILOR && chgrp bin $IMTATAILOR) || echo "Permissions change failed for tailor file."
			rm -f $TMPTAILOR
		    else
		      echo "imta notary_locale can be executed by root or $IMTAUSER only"
		      exit 1
		    fi			
		    ;;
    
    purge )
	    $IMTAPATH/lib/$*
	    status=`echo $?`
	    exit $status
	    ;;
    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
	    ;;
    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
	      ;;
    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
		  ;;
    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
		     ;;
    
    renamedb )
	       $IMTAPATH/lib/dbutil dbutil $*
	       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
	      ;;
    queue )
	    case $2 in
	      -retry_delivery )
				orig_dir=`pwd`
				verifyUid
				if [ $USER -ne 1 ];
				  then echo "imta queue -retry_delivery can be executed by $IMTAUSER only"
				    exit 1
				fi
				queue=$IMTAQUEPATH/$3
				if [ ! -d $queue ]; then
				  echo "Channel $3 does not exist in system"
				else
				  echo "Renaming the .HELD message files in channel $3's queue ..."
				  for subdir in `ls $queue`; do
				    cd $queue/$subdir
				    for msg in `ls *.HELD 2> /dev/null`; do
				      mv $msg `basename $msg .HELD`.00
				    done
				  done
				  echo "Synchronizing the queue cache database ..."
				  $IMTAPATH/lib/cache 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"
			      ;;
	      * )
		  echo "Not a valid command"
		  exit 1;;
	    esac
	    ;;
    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
	      if [ $# -gt 1 ]
	      then
  		COMPONENT=$2
	      fi
	      # make sure databases are not corrupted
	      $IMTAPATH/sbin/imta recover-crash
  	      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
			if [ $USER -eq 1 ]; then
			  $IMTAPATH/lib/cnbuild cnbuild -noimage_file -maximum -option_file=$IMTAOPTPATH/option.tmp
			  status=`echo $?`
			  if [ $status -eq 0 ]; then
			  cat $IMTAOPTIONFILE | grep -v SIZE > $IMTAOPTPATH/option.new
			  cat $IMTAOPTPATH/option.tmp | grep SIZE | grep -v FRUITS >> $IMTAOPTPATH/option.new
			  mv $IMTAOPTPATH/option.new $IMTAOPTIONFILE
			  status=`echo $?`
			  fi
			elif [ $USER -eq 0 ]; then
			  su $IMTAUSER -c "$IMTAPATH/lib/cnbuild cnbuild -noimage_file -maximum -option_file=$IMTAOPTPATH/option.tmp"
			  status=`echo $?`
			  if [ $status -eq 0 ]; then
			  su $IMTAUSER -c "cat $IMTAOPTIONFILE | grep -v SIZE > $IMTAOPTPATH/option.new"
			  su $IMTAUSER -c "cat $IMTAOPTPATH/option.tmp | grep SIZE | grep -v FRUITS >> $IMTAOPTPATH/option.new"
			  su $IMTAUSER -c "mv $IMTAOPTPATH/option.new $IMTAOPTIONFILE"
			  status=`echo $?`
			  fi
			else
			  echo "imta cnbuild can be executed by root or $IMTAUSER only"
			  exit 1
			fi	
			 rm -f $IMTAOPTPATH/option.tmp
			 echo "Your configuration tables have been modified."
			 echo "Please execute, imta restart, for the changes to take effect."
			 exit $status
			 ;;
		*)
		   verifyUid
		   if [ $USER -eq 1 ]; then
		     $IMTAPATH/lib/cnbuild cnbuild
		     status=`echo $?`
		   elif [ $USER -eq 0 ]; then
		     su $IMTAUSER -c "$IMTAPATH/lib/cnbuild cnbuild"
		     status=`echo $?`
		   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/test test -rewrite postmaster | grep "Address list error" 2>/dev/null`
		   if test -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/$1 $*
	      status=`echo $?`
	      exit $status
	      ;;
    recover-crash )
		    verifyUid
		    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; restoring backup"
		      $IMTAPATH/sbin/imta dirsync -c
		      status=$?
		      if [ $status -eq 0 ]; then
			echo "Backup restored"
			exit 0
		      else
			# dirsync -c fails
			# prompt user to perform dirsync -F
			echo "\n*** Backup not available ***\n"
			printf "Do you want to proceed with a full dirsync (Y/n)? "
			read fullsync
			if [ -z "$fullsync" ]; then
			  fullsync=y
			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"
			  exit 1
			fi
		      fi
		    else
		      echo "Database OK"
		      exit 0
		    fi
		    ;;
    * )
	$IMTAPATH/lib/$1 $*
	status=`echo $?`
	exit $status
	;;
  esac
else
  usage
fi
