#!/bin/bash
# Copyright 2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#

. /sbin/functions.sh

wrap_rcscript() { return 0; }

eerror() { true; }
einfo() { true; }
ewarn() { true; }

svcs_started=" "
export svcs_order=" "

. /sbin/runscript.sh "/etc/init.d/clock" "status" 2>/dev/null

service_started() {
  if [ -z "${svcs_started/* ${1} */}" ]; then
    return 0
  else
    return 1
  fi
}

service_failed() {
  return 1
}

mark_service_started() {
  svcs_started="${svcs_started}${1} "
}

mark_service_failed() {
  return 0
}

start_service() {
  if [ -n "${1}" ]; then
    local b_myscript="${myscript}"
    local b_mylevel="${mylevel}"
    local b_myservice="${myservice}"
    local b_IFACE="${IFACE}"
    local b_NETSERVICE="${NETSERVICE}"

    export myscript mylevel myservice IFACE NETSERVICE

    local svc="${1}"
    myscript="/etc/init.d/${svc}"

    if [ -h "${1}" -a ! -h "/etc/init.d/${svc}" ]; then
      myservice="$(/bin/readlink "${myscript}")"
    else
      myservice="${1}"
    fi

    myservice="${myservice##*/}"
    mylevel="${LISTLEVEL}"

    if [ "${myservice%%.*}" == "net" -a "${myservice##*.}" != "${myservice}" ]; then
      IFACE="${myservice##*.}"
      NETSERVICE="yes"
    else
      IFACE=""
      NETSERVICE=""
    fi

    if ! service_started "${1}"; then
      if is_fake_service "${1}" "${LISTLEVEL}"; then
        mark_service_started "${1}"
      else
        svc_start
      fi
      svcs_order="${svcs_order}${1} "
    fi

    myscript="${b_myscript}"
    mylevel="${b_mylevel}"
    myservice="${b_myservice}"
    IFACE="${b_IFACE}"
    NETSERVICE="${b_NETSERVICE}"

    return 0
  else
    return 1
  fi
}

mark_service_failed() {
  return 0
}

is_runlevel_start() {
  return 0
}

start()	{
  return 0
}

get_critical_services() {
  local x
  CRITICAL_SERVICES=""

  if [ -f "/etc/runlevels/${BOOTLEVEL}/.critical" ]; then
    for x in $(<"/etc/runlevels/${BOOTLEVEL}/.critical"); do
      CRITICAL_SERVICES="${CRITICAL_SERVICES} ${x##*/}"
    done
  else
    CRITICAL_SERVICES="checkroot hostname modules checkfs localmount clock"
  fi

  export CRITICAL_SERVICES

  return 0
}

is_net_up() {
  local netcount="0"

  case "${RC_NET_STRICT_CHECKING}" in
    lo)
      netcount="$( \
        echo -e "${svcs_started// /\n}" | \
        /bin/grep -c '\/net\..*$' \
      )"
    ;;
    *)
      netcount="$( \
        echo -e "${svcs_started// /\n}" | \
        /bin/grep -v 'net\.lo' | \
        /bin/grep -c '\/net\..*$' \
      )"
    ;;
  esac

  if [ "${netcount}" -lt "1" -o "${RC_NET_STRICT_CHECKING}" = "yes" ]; then
    return 1
  fi

  return 0
}

ss=($(dolisting "/etc/runlevels/${SOFTLEVEL}/"))
sb=($(dolisting "/etc/runlevels/${BOOTLEVEL}/"))
sd=($(dolisting "/etc/runlevels/${DEFAULTLEVEL}/"))

LISTLEVEL="${BOOTLEVEL}"
for i in ${CRITICAL_SERVICES} ${sb[@]##*/}; do
  start_service "${i}"
done

LISTLEVEL="${DEFAULTLEVEL}"
for i in ${LOGGER_SERVICE} ${sd[@]##*/} ${ss[@]##*/} ${1}; do
  start_service "${i}"
done

svcs_order=( ${svcs_order} )
echo "${svcs_order[@]}"

exit 0
