#!/bin/bash
# /etc/init.d/daemons: Script for manipulating system daemons and services.
# $Id: daemons,v 2.1 2000/03/15 20:53:00 pasky Exp $
# This script starts or stops various system daemons and services, using
# the configuration file /etc/init.d/daemon.config

case "$1" in
  start)  
    echo "Starting daemons and services: "
    cat /etc/init.d/daemon.config | grep "^[0-9].*" |\
    awk '{ if (index($1, ENVIRON["RUNLEVEL"]) >0 && index($1, ENVIRON["PREVLEVEL"]) == 0) \
           { printf("*-* %s\n", $3); \
	     if (toupper($2) == "D") \
               d=system(sh $4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16) ;\
	     else \
	       d=system(sh $4" start") \
	   } \
	 }'
    echo "Done."
    ;;
    
  stop)
    echo "Stopping daemons and services: "
    tac /etc/init.d/daemon.config | grep "^[0-9].*" |\
    awk '{ if (index($1, ENVIRON["PREVLEVEL"]) >0 && index($1, ENVIRON["RUNLEVEL"]) == 0) \
           { printf("*-* %s\n", $3); \
	     if (toupper($2) == "D") \
               d=system("killall" " " $3) ; \
	     else \
	       d=system(sh $4" stop") \
	   } \
	 }'
    echo "Done."
    ;;
    
  update)
    echo "Stopping daemons and services: "
    tac /etc/init.d/daemon.config | grep "^[0-9].*" |\
    awk '{ if (index($1, ENVIRON["PREVLEVEL"]) >0 && index($1, ENVIRON["RUNLEVEL"]) == 0) \
           { printf("*-* %s\n", $3); \
	     if (toupper($2) == "D") \
               d=system("killall" " " $3) ; \
	     else \
	       d=system(sh $4" stop") \
	   } \
	 }'
    echo "Done."
    echo "Starting daemons and services: "
    cat /etc/init.d/daemon.config | grep "^[0-9].*" |\
    awk '{ if (index($1, ENVIRON["RUNLEVEL"]) >0 && index($1, ENVIRON["PREVLEVEL"]) == 0) \
           { printf("*-* %s\n", $3); \
	     if (toupper($2) == "D") \
               d=system(sh $4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16) ;\
	     else \
	       d=system(sh $4" start") \
	   } \
	 }'
    echo "Done."
    ;;
  restart)
    if [ "$2" == "" ]; then
      echo "Required name of service to restart." >&2
      exit 1
    fi
    shift
    echo "Stopping daemons and services: "
    actual=1
    while [ $actual -le $# ]; do
      export TO_KILL=${!actual} # this should be correct, vim syntax highlight is buggy ;-)
      cat /etc/init.d/daemon.config | grep "^[0-9].*" |\
      awk '{ if (ENVIRON["TO_KILL"] == $3) \
    	     { printf("*-* %s\n", $3); \
	       if (toupper($2) == "D") \
                 d=system("killall" " -q " $3) ; \
	       else \
	         d=system(sh $4" stop") \
	     } \
	   }'
      actual=$(( $actual + 1 ));
    done
    echo "Done."
    echo "Starting daemons and services: "
    actual=$#
    while [ $actual -ge 1 ]; do
      export TO_RESURRECT=${!actual}
      cat /etc/init.d/daemon.config | grep "^[0-9].*" |\
      awk '{ if (ENVIRON["TO_RESURRECT"] == $3)
             { printf("*-* %s\n", $3); \
	       if (toupper($2) == "D") \
                 d=system(sh $4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16) ;\
	       else \
	         d=system(sh $4" start") \
	     } \
	   }'
      actual=$(( $actual - 1 ))
    done
    echo "Done.";;
  *)
    echo "Usage: daemons {start|stop|update|restart} [SERVICE1 [SERVICE2 [SERVICEn]]]"
    echo -e "Manipulates system services\n\n\
  start		When changing runlevel, start newbie services.\n\
  stop		When changing runlevel, stop old (unmatched) services.\n\
  update	Call both stop and start, making runlevel change 'stepless'.\n\
  restart	Restart given SERVICEs, if needed kill old process.\n\
  		Note that only this one action requires that parameter.\n"
    exit 1
    ;;
    
esac
# end of daemons
