#!/bin/bash
# /etc/rc — AnotherOS system startup, run once by init (PID 1).
#
# FreeBSD model: read /etc/rc.conf, then run every /etc/rc.d/<name> script whose
# <name>_enable is YES. Each script takes start|stop. init no longer hardcodes
# any service; this file is the single place boot-time services come from.

# Package DEFAULTS first, then the machine's own rc.conf, so a line in rc.conf
# always wins. /etc/defaults/services/<name> is how a service shipped by a
# package (coresystem) becomes enabled on a machine that was installed before
# the service existed: /etc/rc.conf is the administrator's file and no package
# may edit it, so without this a new rc.d script reached an installed disk and
# was never started (#112 — the MacBook had no kernelconfirm at all).
for d in /etc/defaults/services/*; do
    [ -r "$d" ] && . "$d"
done
[ -r /etc/rc.conf ] && . /etc/rc.conf

quiet=0
grep -qwE 'quiet|graphical' /proc/cmdline 2>/dev/null && quiet=1
log() { [ "$quiet" = 1 ] || echo "rc: $*"; }

# Hostname.
if [ -n "$hostname" ]; then
    hostname "$hostname" 2>/dev/null || true
fi

# Writable runtime dirs some daemons expect.
mkdir -p /tmp /var/run /var/log 2>/dev/null
chmod 1777 /tmp 2>/dev/null || true

for svc in /etc/rc.d/*; do
    [ -f "$svc" ] || continue
    name=${svc##*/}
    eval "enable=\${${name}_enable}"
    case "$enable" in
        [Yy][Ee][Ss])
            log "starting $name"
            /bin/bash "$svc" start
            ;;
        *)
            ;;
    esac
done

log "startup complete"
exit 0
