#! /bin/sh
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed 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:    gce_run_startup_scripts
# Required-Start:    $all
# Required-Stop:     $remote_fs $syslog docker kubelet
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Google Compute Engine user scripts
# Description:       This runs user-specified VM startup and shutdown scripts.
### END INIT INFO

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

DESC="Google Compute Engine user startup scripts"
NAME="run_startup_scripts"

# If we're running under upstart, let the upstart config file handle things.
# Debian 7 and newer have a near-one-liner function to detect this...
if type init_is_upstart >/dev/null 2>&1; then
  # ... which we can use if present.
  init_is_upstart && exit 0
else
  # Otherwise, directly include the core line of Debian 7's version.
  # Authorship credit: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=661109
  if [ -x /sbin/initctl ] && /sbin/initctl version | /bin/grep -q upstart; then
    exit 0
  fi
fi

#
# Function that starts the daemon/service
#
do_start()
{
  /usr/share/google/run-startup-scripts
}

#
# Function that stops the daemon/service
#
do_stop()
{
  /usr/share/google/run-shutdown-scripts
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    case "$?" in
      0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      *) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
      0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      *) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  *)
    echo "Usage: $SCRIPTNAME start" >&2
    exit 3
    ;;
esac

:
