#!/bin/sh

export PATH=$PATH:/sred/bin:/sred/usr/bin

# load the configuration information
. /etc/rc.d/rc.conf

if [ -f /etc/rc.d/rc.local.conf ]; then
    . /etc/rc.d/rc.local.conf
fi

if [ -f /etc/rc.d/rc.net.conf ]; then
    /bin/ash -n /etc/rc.d/rc.net.conf
    if [ $? -eq 0 ]; then
        . /etc/rc.d/rc.net.conf || true
    fi
fi

if [ -f /etc/rc.d/rc.platform.conf ]; then
    . /etc/rc.d/rc.platform.conf
fi

mode=${1:-start}
# Stop daemons from rc.local
if [ $mode = "stop" ]; then
    /etc/rc.d/rc.local $mode
    
    /etc/rc.d/init.d/splashscreen $mode

    /etc/platform/04-random $mode
    /etc/platform/33-networkmanager $mode
    /etc/platform/34-versioninfo $mode
    /etc/platform/99-other $mode

    platform_process_names="
        wpa_supplicant
        installer
        registry-srv
        securityservice
        androidpay
        applepay
        arb-printer
        SystemFastCGI
        sredd
        WebBrowser
        camerad
        tmcore
        bluetooth-daemon
        power-manager
        audiopayd
        sshd
        lighttpd
        avahi-daemon
        dbus-daemon
        udevd"

    kill `pidof $platform_process_names`
    
    rm -f /var/lock/subsys/lighttpd   \
          /var/lock/subsys/messagebus \
          /var/run/messagebus.pid
fi

if [ $mode = "start" ]; then
    services=$cfg_services
else
    services=$cfg_services_r
    if [ -d /etc/down ]; then 
        run-parts -a $mode /etc/down
    fi
fi
cfg_services=${2:-$services}

# run the configured sequence
if [ "$mode" = "stop" ]; then
    for i in $cfg_services
    do
        case "$i" in
            sshd|lighthttpd|avahi-daemon|messagebus|syslog|udev|splashscreen)
                continue
                ;;
            *)
                if [ -x /etc/rc.d/init.d/$i ]; then
                    /etc/rc.d/init.d/$i $mode
                fi
                ;;
        esac
    done
else
    for i in $cfg_services
    do
        if [ -x /etc/rc.d/init.d/$i ]; then
            /etc/rc.d/init.d/$i $mode
        fi
    done
fi

if [ $# -ge 2 ]; then 
    exit 0
fi
# show all kernel log messages
#echo 8 >  /proc/sys/kernel/printk

# run rc.local if present and executable
if [ $mode = "start" ]; then
    /etc/rc.d/rc.local $mode
fi

