#!/bin/sh

# Copyright Apple Computer, Inc. 2003

usage () {
    echo "
usage: $COMMAND [-h | --help] [-v | --verbose] [-V | --version]
		[-n] [-f | --force] <mta>

Switch the default mail transport agent (MTA) to either sendmail or postfix

  -f, -force		Ensure the links are correct for the specified MTA
			even if the one specified is the current version.
  -n			Show commands to do selection but do not execute them.
  -h, --help		Display this help info.
  -v, --verbose		Display informational messages
  -V, --version		Display mta_select version number.

  <mta>			The MTA to activate as the default.  Possible options:
      sendmail		Select sendmail as the default MTA
      postfix		Select postfix as the default MTA
"
}

#######################################################################

MTA_SELECT_VERSION="1.0"
MTA_FILES="/usr/bin/newaliases /usr/bin/mailq /usr/sbin/sendmail"
MTA_BACKUP_ROOT="/usr/libexec/postfix/backup"

#######################################################################

#######################################################################
#
## print error string and usage then exit
#
mta_error () {
    echo "error: $1"
    usage;
    exit 1;
}
#######################################################################
#
## verifies a correct installation
#
check_files () {
	if [ "$FORCE" ]; then
	    return
	fi

	for F in $MTA_FILES; do
		if [ ! -f "$F" ]; then
			echo "
Invalid installation detected.	You may try the force option (-f) to
override.
"
		exit 1
		fi
	done
}
#######################################################################
#
## backup current installation as first argument
#
mta_backup () {
	check_files
	
	if [ ! -d "${MTA_BACKUP_ROOT}/${1}" ]; then
		$ECHO mkdir -p "${MTA_BACKUP_ROOT}/${1}"
	fi

	# copy files from for replacement
	for F in $MTA_FILES; do
		$ECHO cp -p "$F" "${MTA_BACKUP_ROOT}/${1}/${F##*/}"
	done

	check_files
}
#######################################################################
#######################################################################
#
## mta_select's main control function
#
mta_switch () {
	check_files

	# remove files from default locations
	for F in $MTA_FILES; do
		$ECHO rm -f "$F"
	done

	# copy files from for replacement
	for F in $MTA_FILES; do
		$ECHO cp -p "${MTA_BACKUP_ROOT}/${1}/${F##*/}" "$F"
	done

	check_files
}
#######################################################################
### main() ############################################################
#######################################################################

COMMAND="${0##*/}"
os_major="`uname -r | sed -e 's/\([^\.]*\).*/\1/'`"

while getopts "bfnhvV-:" ARG; do
    case $ARG in
	b) BACKUP="yes"    ;;
	f) FORCE="yes"     ;;
	n) ECHO="echo"     ;;
	v) VERBOSE="yes";  ;;
	-) case $OPTARG in
	    backup)  BACKUP="yes"    ;;
	    force)   FORCE="yes"     ;;
	    verbose) VERBOSE="yes"   ;;
	    *) mta_error "Invalid option: --$OPTARG"
	   esac
	;;
	V|version)
	    echo $COMMAND version: $MTA_SELECT_VERSION
	    exit
	;;
	*)
	    mta_error "Invalid option  -$OPTARG"
	;;
    esac
done
shift $(($OPTIND - 1))

if [ $os_major -lt 7 ]; then
	echo "You must be using MacOS X 10.3 (Panther) or later."
	exit 1
fi

if [ "$UID" -ne 0 -a -z "$FORCE" ]; then
	echo "You must be root to use this command."
	exit 1
fi

if [ "$#" -eq 1 ]; then
	if [ "$1" != "postfix" -a "$1" != "sendmail" ]; then
		mta_error "Invalid MTA option provided"
	fi

	if [ "$BACKUP" ]; then
		mta_backup "$1"
	elif [ -d "${MTA_BACKUP_ROOT}" ]; then
		if [ "$VERBOSE" ]; then
		    echo "setting default to: $1"
		fi
		mta_switch "$1"
	else
		echo "There is only a single MTA on the system.  There is nothing to do."
		exit 1
	fi
else
	mta_error "invalid number of arguments: $#"
	usage
	exit 1
fi
