#!/bin/sh

echo "$0: 1=$1 2=$2i 3=$3 4=$4"

echo "Installing the start-up launcher..."

cd /Library/StartupItems

if [ ! -d CommuniGatePro ]; then
  mkdir CommuniGatePro
fi
cat >CommuniGatePro/CommuniGatePro <<!ZEOF
#!/bin/sh
#
# Start the CommuniGate Pro Server. 
#
BASEDIRECTORY=/var/CommuniGate
APPDIRECTORY=/usr/sbin/CommuniGatePro
SUPPLPARAMS=

. /etc/rc.common

[ -f \${APPDIRECTORY}/CGServer ] || exit 1

if [ \$1 = "stop" ]; then
  ConsoleMessage "Stopping CommuniGate Pro..."
  if [ -f \${BASEDIRECTORY}/ProcessID ]; then
    if kill \`cat \${BASEDIRECTORY}/ProcessID\` ; then
      ConsoleMessage "Waiting for CommuniGate Pro Server to stop..."
      while [ -f \${BASEDIRECTORY}/ProcessID ]; do
        ConsoleMessage "Waiting..."; sleep 1;
      done
      ConsoleMessage "CommuniGate Pro stopped"
    fi
  else
    echo "CommuniGate Pro is not running"
  fi
  exit 0
fi

if [ ! -d \${BASEDIRECTORY} ]; then
  ConsoleMessage "Creating CommuniGate directory..."

  if ! mkdir -p -m 770 \${BASEDIRECTORY}; then
    ConsoleMessage "Failed to create \${BASEDIRECTORY}"
    exit 1
  fi

  if ! chgrp mail  \${BASEDIRECTORY}; then
    echo "Failed to open  \${BASEDIRECTORY} for the 'mail' user group"
    exit 1
  fi
fi

ConsoleMessage "Starting CommuniGate Pro"
umask     0
ulimit -d 100000
ulimit -c 200000

# Custom Script Parameters
if [ -f \${BASEDIRECTORY}/Startup.sh ]; then
  . \${BASEDIRECTORY}/Startup.sh
fi

if \${APPDIRECTORY}/CGServer --Base \${BASEDIRECTORY} --Daemon \${SUPPLPARAMS} ; then
  ConsoleMessage "CommuniGate Pro started"
fi

!ZEOF
if ! chmod 0755 CommuniGatePro/CommuniGatePro; then
  echo "Failed to make the startup launcher executable"; exit 1
fi

cat >CommuniGatePro/StartupParameters.plist <<!ZEOF
{
  Description     = "CommuniGate Pro Communication Server";
  Provides        = ("SMTPx","IMAPx","POPx","LDAPx","ACAPx","WebMail","RPOP","SIPx");
  Requires        = ("Network","Resolver","Disks");
  Uses            = ("IPAliases");
  OrderPreference = "Last";
  Messages =
  {
    start = "Starting CommuniGate Pro";
    stop  = "Stopping CommuniGate Pro";
  };
}
!ZEOF

echo "Installing the LaunchDaemons item..."
# for now we start using the obsolete StartupItem
cat >/Library/LaunchDaemons/CommuniGatePro.plist <<!ZEOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>CommuniGatePro</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Library/StartupItems/CommuniGatePro/CommuniGatePro</string>
    <string>start</string>
  </array>
  <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
!ZEOF

cd /System/Library/StartupItems

if [ -d Sendmail ]; then
  echo "Disabling the Sendmail launcher..."
  if ! tar -cf Sendmail.tar Sendmail; then
    echo "Failed to rename the launcher"; exit 1
  else
    rm -rf Sendmail
  fi
fi

if [ -d Postfix ]; then
  echo "Disabling the Postfix launcher..."
  if ! tar -cf Postfix.tar Postfix; then
    echo "Failed to rename the launcher"; exit 1
  else
    rm -rf Postfix
  fi
fi


echo "setting the 'mail' group"
if ! chgrp -R mail /usr/sbin/CommuniGatePro ; then
  echo "Failed to set the 'mail' group"
# exit 1
fi

if [ ! -f /usr/bin/LegacyMail ]; then
  echo "Renaming Legacy Mail"
  if ! mv /usr/bin/mail /usr/bin/LegacyMail ; then
    echo "Failed to rename the mail application"
    exit 1
  fi
  if ! ln -s /usr/sbin/CommuniGatePro/mail /usr/bin/mail; then
    mv /usr/bin/LegacyMail /usr/bin/mail
    echo "Failed to create a link to the CGatePro mail application"
  fi
fi

echo ""
