#
# Copyright (C) 2012 The Android Open Source Project
#
# 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.
#

description   "Run the shill network connection manager"
author        "chromium-os-dev@chromium.org"

# shill termination may take more than 5s (the default kill timeout) in some
# cases (e.g. it needs to disconnect a cellular modem from the network). A
# longer kill timeout is used to ensure that shill terminates properly in most
# cases. Also, stop shill on "starting pre-shutdown" so its exiting blocks the
# chromeos_shutdown script from running (jobs that "stop on stopping
# boot-services" may still be running in parallel with chromeos_shutdown if
# they do not exit quickly).
start on @expected_started_services@
stop on starting pre-shutdown
kill timeout 10
respawn

# Default parameters to be overridden by upstart configuration.
env SHILL_LOG_LEVEL=0
env SHILL_LOG_SCOPES=
env SHILL_PASSIVE_MODE=
env SHILL_PREPEND_DNS_SERVERS=
env SHILL_ACCEPT_HOSTNAME_FROM=
env SHILL_MINIMUM_MTU=
env BLACKLISTED_DEVICES=
env DHCPV6_ENABLED_DEVICES=
env IGNORE_UNKNOWN_ETHERNET=

# Hook for mod_for_test_scripts/100setupTestingInterface.
env SHILL_TEST_DEVICES=""

pre-start script
  bootstat shill-start

  # Create state directory
  mkdir -p /var/run/shill

  # Create storage for the shill global profile.
  mkdir -p /var/cache/shill

  # Use flimflam's default profile if shill doesn't have one.
  if [ ! -f /var/cache/shill/default.profile -a \
         -f /var/cache/flimflam/default.profile ]; then
    mv /var/cache/flimflam/default.profile /var/cache/shill/default.profile
    chmod a+r /var/cache/shill/default.profile
  fi

  # Set up dhcpcd's /var/{lib|run} dirs to run as user 'dhcp'.
  mkdir -m 0755 -p /var/lib/dhcpcd
  mkdir -m 0755 -p /var/run/dhcpcd
  chmod -R u+rwX,g+rX,o+rX /var/lib/dhcpcd
  chown -R dhcp:dhcp /var/lib/dhcpcd
  chown -R dhcp:dhcp /var/run/dhcpcd

  # This option is no longer supported.
  rm -f /home/chronos/.disable_shill
end script

post-stop script
  bootstat shill-stop
end script

script
  DAEMONBIN="shill"
  ARGS="--log-level=${SHILL_LOG_LEVEL} --log-scopes=${SHILL_LOG_SCOPES}"
  if [ -n "${BLACKLISTED_DEVICES}" ] && [ -n "${SHILL_TEST_DEVICES}" ]; then
    ARGS="${ARGS} --device-black-list=${BLACKLISTED_DEVICES},${SHILL_TEST_DEVICES}"
  elif [ -n "${BLACKLISTED_DEVICES}" ]; then
    ARGS="${ARGS} --device-black-list=${BLACKLISTED_DEVICES}"
  elif [ -n "${SHILL_TEST_DEVICES}" ]; then
    ARGS="${ARGS} --device-black-list=${SHILL_TEST_DEVICES}"
  fi
  if [ -n "${SHILL_PASSIVE_MODE}" ]; then
    ARGS="${ARGS} --passive-mode"
  fi
  if [ -n "${SHILL_PREPEND_DNS_SERVERS}" ]; then
    ARGS="${ARGS} --prepend-dns-servers=${SHILL_PREPEND_DNS_SERVERS}"
  fi
  if [ -n "${SHILL_ACCEPT_HOSTNAME_FROM}" ]; then
    ARGS="${ARGS} --accept-hostname-from=${SHILL_ACCEPT_HOSTNAME_FROM}"
  fi
  if [ -n "${SHILL_MINIMUM_MTU}" ]; then
    ARGS="${ARGS} --minimum-mtu=${SHILL_MINIMUM_MTU}"
  fi
  if [ -n "${DHCPV6_ENABLED_DEVICES}" ]; then
    ARGS="${ARGS} --dhcpv6-enabled-devices=${DHCPV6_ENABLED_DEVICES}"
  fi
  if [ -n "${IGNORE_UNKNOWN_ETHERNET}" ]; then
    ARGS="${ARGS} --ignore-unknown-ethernet"
  fi
  ARGS="${ARGS} ${SHILL_TEST_ARGS}"

  # If OOBE has not completed (i.e. EULA not agreed to), do not run
  # portal checks.
  if [ ! -f /home/chronos/.oobe_completed ]; then
    ARGS="${ARGS} --portal-list="
  fi

  exec ${DAEMONBIN} ${ARGS}
end script