d0d307
#!/bin/sh
d0d307
#
d0d307
# chkconfig: - 65 35
d0d307
# description: The Cyrus IMAPD master serves as a master process for the Cyrus \
d0d307
#              IMAP and POP servers.
d0d307
# config: /etc/cyrus.conf
d0d307
# config: /etc/imapd.conf
d0d307
# pidfile: /var/run/cyrus-master.pid
d0d307
cf58a5
# author:	Simon Matter, Invoca Systems <simon.matter@invoca.ch>
cf58a5
# version:	2005111100
cf58a5
# changed:	add quickstart/stop option to init script to bypass db import/export
d0d307
d0d307
# Source function library
d0d307
if [ -f /etc/init.d/functions ]; then
d0d307
  . /etc/init.d/functions
d0d307
elif [ -f /etc/rc.d/init.d/functions ]; then
d0d307
  . /etc/rc.d/init.d/functions
d0d307
else
d0d307
  exit 0
d0d307
fi
d0d307
d0d307
# Source networking configuration.
d0d307
. /etc/sysconfig/network
d0d307
d0d307
# Check that networking is up.
d0d307
[ ${NETWORKING} = "no" ] && exit 0
d0d307
d0d307
# check if the config files are present
d0d307
[ -f /etc/cyrus.conf ] || exit 0
d0d307
[ -f /etc/imapd.conf ] || exit 0
d0d307
 
d0d307
# This is our service name
d0d307
BASENAME=$(basename $0)
d0d307
if [ -L $0 ]; then
d0d307
  BASENAME=$(find $0 -name $BASENAME -printf %l)
d0d307
  BASENAME=$(basename $BASENAME)
d0d307
fi
d0d307
cf58a5
# Source service configuration.
cf58a5
if [ -f /etc/sysconfig/$BASENAME ]; then
cf58a5
  . /etc/sysconfig/$BASENAME
d0d307
else
cf58a5
  echo "$BASENAME: configfile /etc/sysconfig/$BASENAME does NOT exist !"
cf58a5
  exit 1
d0d307
fi
d0d307
cf58a5
# get_config [config default]
cf58a5
# extracts config option from config file
cf58a5
get_config() {
cf58a5
  if config=$(grep "^$1" /etc/imapd.conf); then
cf58a5
    echo $config | cut -d: -f2
cf58a5
  else
cf58a5
    echo $2
cf58a5
  fi
cf58a5
}
cf58a5
cf58a5
# where to find files and directories
cf58a5
CONFIGDIRECTORY=$(get_config configdirectory /var/lib/imap)
d0d307
CYRUSMASTER=/usr/lib/cyrus-imapd/cyrus-master
d0d307
CYRUS_PROC_NAME=$(basename $CYRUSMASTER)
d0d307
ALWAYS_CONVERT=1
d0d307
cf58a5
# fallback to su if runuser not available
cf58a5
if [ -x /sbin/runuser ]; then
cf58a5
  RUNUSER=runuser
d0d307
else
cf58a5
  RUNUSER=su
d0d307
fi
d0d307
d0d307
RETVAL=0
cf58a5
RETVAL2=1
cf58a5
QUICK=0
d0d307
d0d307
start() {
d0d307
  if [ $(/sbin/pidof -s $CYRUSMASTER) ]; then
d0d307
    echo -n $"$BASENAME already running."
d0d307
    false
cf58a5
    echo
d0d307
  else
cf58a5
    if [ $QUICK -eq 0 ]; then
cf58a5
      echo -n $"Importing $BASENAME databases: "
cf58a5
      cd $CONFIGDIRECTORY
cf58a5
      $RUNUSER - cyrus -c "umask 166 ; /usr/lib/cyrus-imapd/cvt_cyrusdb_all > ${CONFIGDIRECTORY}/rpm/db_import.log 2>&1" < /dev/null
cf58a5
      RETVAL=$?
cf58a5
      if [ $RETVAL -eq 0 ]; then
cf58a5
        success $"$BASENAME importing databases"
cf58a5
      else
cf58a5
        failure $"$BASENAME error importing databases, check ${CONFIGDIRECTORY}/rpm/db_import.log"
cf58a5
      fi
cf58a5
      echo
cf58a5
    fi
d0d307
    if [ $RETVAL -eq 0 ]; then
cf58a5
      echo -n $"Starting $BASENAME: "
d0d307
      daemon $CYRUSMASTER -d $CYRUSOPTIONS
cf58a5
      RETVAL2=$?
cf58a5
      echo
d0d307
    fi
d0d307
  fi
cf58a5
  [ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && touch /var/lock/subsys/$BASENAME
d0d307
  return $RETVAL
d0d307
}
d0d307
d0d307
stop() {
d0d307
  echo -n $"Shutting down $BASENAME: "
d0d307
  killproc $CYRUSMASTER
d0d307
  RETVAL=$?
cf58a5
  if [ $QUICK -eq 0 ]; then
cf58a5
    if [ ! $(/sbin/pidof -s $CYRUSMASTER) ]; then
cf58a5
      echo
cf58a5
      echo -n $"Exporting $BASENAME databases: "
cf58a5
      cd $CONFIGDIRECTORY
cf58a5
      $RUNUSER - cyrus -c "umask 166 ; /usr/lib/cyrus-imapd/cvt_cyrusdb_all export > ${CONFIGDIRECTORY}/rpm/db_export.log 2>&1" < /dev/null
cf58a5
      RETVAL2=$?
cf58a5
      if [ $RETVAL2 -eq 0 ]; then
cf58a5
        success $"$BASENAME exporting databases"
cf58a5
      else
cf58a5
        failure $"$BASENAME error exporting databases, check ${CONFIGDIRECTORY}/rpm/db_export.log"
cf58a5
      fi
cf58a5
    fi
cf58a5
  fi
d0d307
  echo
cf58a5
  [ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && rm -f /var/lock/subsys/$BASENAME
d0d307
  return $RETVAL
d0d307
}
d0d307
d0d307
restart() {
d0d307
  stop
d0d307
  start
d0d307
}
d0d307
d0d307
reload() {
d0d307
  echo -n $"Reloading cyrus.conf file: "
d0d307
  killproc $CYRUSMASTER -HUP
d0d307
  RETVAL=$?
d0d307
  echo
d0d307
  return $RETVAL
d0d307
}
d0d307
 
d0d307
condrestart() {
d0d307
  [ -e /var/lock/subsys/$BASENAME ] && restart || :
d0d307
}
d0d307
d0d307
rhstatus() {
d0d307
  status $CYRUSMASTER
d0d307
  RETVAL=$?
d0d307
  return $RETVAL
d0d307
}
d0d307
d0d307
case "$1" in
d0d307
  start)
d0d307
    start
d0d307
    ;;
d0d307
  stop)
d0d307
    stop
d0d307
    ;;
d0d307
  restart)
d0d307
    restart
d0d307
    ;;
d0d307
  reload)
d0d307
    reload
d0d307
    ;;
d0d307
  condrestart)
d0d307
    condrestart
d0d307
    ;;
d0d307
  status)
d0d307
    rhstatus
d0d307
    ;;
cf58a5
  quickstart)
cf58a5
    QUICK=1
cf58a5
    start
cf58a5
    ;;
cf58a5
  quickstop)
cf58a5
    QUICK=1
cf58a5
    stop
cf58a5
    ;;
d0d307
  *)
cf58a5
    echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|quickstart|quickstop}"
d0d307
    RETVAL=1
d0d307
esac
d0d307
d0d307
exit $RETVAL