#!/bin/sh
#
# chkconfig: - 65 35
# description: The Cyrus IMAPD master serves as a master process for the Cyrus \
#              IMAP and POP servers.
# config: /etc/cyrus.conf
# config: /etc/imapd.conf
# pidfile: /var/run/cyrus-master.pid

# author:       Simon Matter, Invoca Systems <simon.matter@invoca.ch>
# version:	2002091900
# changed:	2002020200 chkconfig modified
#               2002042500 rewrote start function
#               2002091800 added auto db converting functionality
#		2003020400 modified to use builtin daemon mode
#		2003050200 modified exec path
#		2003050900 return RETVAL from rhstatus(), did some cleanup
#		2003060700 added umask for cvt_cyrusdb_all log
#		2004012300 modified auto db converting functionality for 2.2
#		2004012700 fixed startup procedure
#		2004022400 change su within init script to get input from
#			   /dev/null, this prevents hang when running in SELinux
#		2004052000 don't enable cyrus-imapd per default
#               2004111900 use runuser instead of su if available

# Source function library
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# check if the config files are present
[ -f /etc/cyrus.conf ] || exit 0
[ -f /etc/imapd.conf ] || exit 0
 
# This is our service name
BASENAME=$(basename $0)
if [ -L $0 ]; then
  BASENAME=$(find $0 -name $BASENAME -printf %l)
  BASENAME=$(basename $BASENAME)
fi

# fallback to su if runuser not available
if [ -x /sbin/runuser ]; then
  RUNUSER=runuser
else
  RUNUSER=su
fi

CYRUSMASTER=/usr/lib64/cyrus-imapd/cyrus-master
CYRUS_PROC_NAME=$(basename $CYRUSMASTER)
ALWAYS_CONVERT=1

# Source service configuration.
if [ -f /etc/sysconfig/$BASENAME ]; then
  . /etc/sysconfig/$BASENAME
else
  echo "$BASENAME: configfile /etc/sysconfig/$BASENAME does NOT exist !"
  exit 1
fi

RETVAL=0

start() {
  echo -n $"Starting $BASENAME: "
  if [ $(/sbin/pidof -s $CYRUSMASTER) ]; then
    echo -n $"$BASENAME already running."
    false
  else
    echo -n $"preparing databases... "
    $RUNUSER - cyrus -c "umask 166 ; /usr/lib64/cyrus-imapd/cvt_cyrusdb_all > /var/lib/imap/rpm/cvt_cyrusdb_all.log 2>&1" < /dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
      echo -n $"done. "
      daemon $CYRUSMASTER -d $CYRUSOPTIONS
    else
      echo -n $"error! "
      initlog -n $BASENAME -s "error converting databases, check /var/lib/imap/rpm/cvt_cyrusdb_all.log"
      failure $"$BASENAME startup"
    fi
  fi
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$BASENAME
  return $RETVAL
}

stop() {
  echo -n $"Shutting down $BASENAME: "
  killproc $CYRUSMASTER
  RETVAL=$?
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BASENAME
  echo
  return $RETVAL
}

restart() {
  stop
  start
}

reload() {
  echo -n $"Reloading cyrus.conf file: "
  killproc $CYRUSMASTER -HUP
  RETVAL=$?
  echo
  return $RETVAL
}
 
condrestart() {
  [ -e /var/lock/subsys/$BASENAME ] && restart || :
}

rhstatus() {
  status $CYRUSMASTER
  RETVAL=$?
  return $RETVAL
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  condrestart)
    condrestart
    ;;
  status)
    rhstatus
    ;;
  *)
    echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status}"
    RETVAL=1
esac

exit $RETVAL
