#!/sbin/runscript

# For pcmcia users. note that pcmcia must be added to the same
# runlevel as the net.* script that needs it.
depend() {
  use hotplug pcmcia
}

checkconfig() {
  if [ ! -e /etc/conf.d/netprofiles-ims ]; then
    eerror "Making a default netprofiles-ims file for you..."
    echo "USE_DEFAULT_PROFILE=1" > /etc/conf.d/netprofiles-ims
    echo "MANAGED_INTERFACES=\"eth0\"" >> /etc/conf.d/netprofiles-ims
    eerror "Please go edit this file and make sure it suits you, then try again."
    return 1
  fi
}

start() {
  local retval=0
  local did_we_have_problems=0
  checkconfig || return 1

  if [ "$USE_DEFAULT_PROFILE" -eq 1 ]; then
    DF_FLAG="-d"
  else
    DF_FLAG=""
  fi

  IFACE=${MANAGED_INTERFACES}

  einfo "Netprofiles IMS by buckminst (buckminst@inconnu.isu.edu)"
  einfo "-----------------------------------"
  einfo "Starting up managed network interfaces:"
  for get_ifaces in ${MANAGED_INTERFACES}
  do
    ebegin "${get_ifaces}"
      /usr/sbin/netprofiles -i ${get_ifaces} -v 0 ${DF_FLAG} start
      retval=$?
      if [ $retval -eq 1 ]; then
        did_we_have_problems=1
      fi
    eend ${retval}
  done
  return ${did_we_have_problems}
}

stop() {
  local retval=0
  einfo "Netprofiles IMS by buckminst (buckminst@inconnu.isu.edu)"
  einfo "-----------------------------------"
  einfo "Shutting down managed network interfaces:"
  
  for get_ifaces in $(find /var/state/netprofiles -iname *.state)
  do
    Current_IFACE=$(basename $get_ifaces .state)
    ebegin "${Current_IFACE}"
      /usr/sbin/netprofiles -i ${Current_IFACE} -v 0 stop
      retval=$?
    eend ${retval}
  done
  return 0
}

status() {
  local card_count=0
  for get_ifaces in $(find /var/state/netprofiles -iname *.state)
  do
    card_count=$((card_count + 1))
  done
  if [ $card_count -ne 0 -a $card_count -lt 2 ]; then
    CARDWORD="card"
  else
    CARDWORD="cards"
  fi
  einfo "${card_count} ${CARDWORD} being managed currently."
  einfo "-----------------------------------"
  einfo "Status of managed cards:"

  for get_ifaces in $(find /var/state/netprofiles -iname *.state)
  do
    Current_IFACE=$(basename $get_ifaces .state)
    /usr/sbin/netprofiles -i ${Current_IFACE} -v 0 status
  done
  return 0
}