#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

### BEGIN INIT INFO
# Provides:          xend
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: XEN control daemon
# Description:       XEN control daemon
### END INIT INFO

PATH=/usr/lib/xen-common/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Xen daemons"

VERSION=$(xen-version)
ROOT=/usr/lib/xen-$VERSION

XEND="$ROOT"/bin/xend
XENCONSOLED="$ROOT"/bin/xenconsoled
XENCONSOLED_PIDFILE="/var/run/xenconsoled.pid"
XENSTORED="$ROOT"/bin/xenstored
XENSTORED_DIR="/var/run/xenstored"
XENSTORED_PIDFILE="/var/run/xenstore.pid"

[ "$VERSION" ] || exit 0
[ -x "$XEND" ] || exit 0

[ -r /etc/default/xend ] && . /etc/default/xend

. /lib/init/vars.sh
. /lib/lsb/init-functions

modules_setup()
{
        modprobe xenfs 2>/dev/null
        modprobe xen-evtchn 2>/dev/null
        modprobe xen_blkback 2>/dev/null
        modprobe xen_netback 2>/dev/null
        modprobe xen_gntdev 2>/dev/null
}

xenfs_setup()
{
        [ -e "/proc/xen/capabilities" ] && return 0
        log_progress_msg "xenfs"
        [ -d "/proc/xen" ] || return 1
        mount -t xenfs xenfs /proc/xen || return 1
        return 0
}

capability_check()
{
        [ -e "/proc/xen/capabilities" ] || return 1
        grep -q "control_d" /proc/xen/capabilities || return 1
        return 0
}

xend_start()
{
        log_progress_msg "xend"
        $XEND status && return 1
        $XEND start || return 2

        i=0
        while [ $i -lt 10 ]; do
                $XEND status && return 0 || true
                i=$(($i + 1))
                sleep 1
        done
        return 2
}

xend_stop()
{
        log_progress_msg "xend"
        $XEND status || return 0
        $XEND stop || return 1
}

xenconsoled_start()
{
        log_progress_msg "xenconsoled"
        start-stop-daemon --start --quiet --pidfile "$XENCONSOLED_PIDFILE" --exec "$XENCONSOLED" --test > /dev/null \
                || return 1
        start-stop-daemon --start --quiet --pidfile "$XENCONSOLED_PIDFILE" --exec "$XENCONSOLED" -- \
                $XENCONSOLED_ARGS --pid-file="$XENCONSOLED_PIDFILE" \
                || return 2
}

xenstored_start()
{
        log_progress_msg "xenstored"
        start-stop-daemon --start --quiet --pidfile "$XENSTORED_PIDFILE" --exec "$XENSTORED" --test > /dev/null \
                || return 1
        [ -d "$XENSTORED_DIR" ] || mkdir -p "$XENSTORED_DIR"
        export XENSTORED_ROOTDIR="$XENSTORED_DIR"
        start-stop-daemon --start --quiet --pidfile "$XENSTORED_PIDFILE" --exec "$XENSTORED" -- \
                $XENSTORED_ARGS --pid-file="$XENSTORED_PIDFILE" \
                || return 2
}

case "$1" in
  start)
        log_daemon_msg "Starting $DESC"
        modules_setup
        xenfs_setup
        case "$?" in
                0) ;;
                *) log_end_msg 1; exit ;;
        esac
        capability_check
        case "$?" in
                0) ;;
                *) log_end_msg 255; exit ;;
        esac
        xenstored_start
        case "$?" in
                0|1) ;;
                *) log_end_msg 1; exit ;;
        esac
        xenconsoled_start
        case "$?" in
                0|1) ;;
                *) log_end_msg 1; exit ;;
        esac
        #xend_start
        case "$?" in
                0|1) ;;
                *) log_end_msg 1; exit ;;
        esac
        log_end_msg 0
        ;;
  stop)
        capability_check
        case "$?" in
                0) ;;
                *) exit ;;
        esac
        log_daemon_msg "Stopping $DESC"
        #xend_stop
        case "$?" in
                0|1) log_end_msg 0 ;;
                *) log_end_msg 1 ;;
        esac
        ;;
  restart|force-reload)
        capability_check
        case "$?" in
                0) ;;
                *) exit ;;
        esac
        log_daemon_msg "Restarting $DESC"
        #xend_stop
        case "$?" in
                0|1)
                #xend_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        *) log_end_msg 1 ;;
                esac
                ;;
                *) log_end_msg 1 ;;
        esac
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac

exit 0
