#!/live/bin/sh

### BEGIN INIT INFO
# Provides:          live-disable-services
# Required-Start:    checkfs checkroot-bootclean live-init
# Required-Stop:
# Should-Start:
# Default-Start:     S
# Default-Stop:
# Short-Description: Disable services for LiveCD/USB
# Description:       Temporarily disable services for LiveCD/USB
### END INIT INFO

export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin

LEAN_SERVICES="
    acpi-fakekey
    acpi-support
    bootlogs
    bluetooth
    #cherokee
    cpufrequtils
    cron
    cups
    gpm
    ifplugd
    irqbalance
    loadcpufreq
    nfs-common
    rpcbind
    rsync
    rsyslog
    saned
    smartmontools
    ssh
    stop-bootlogd
    sudo
    #transmission-daemon
"

LAPTOP_SERVICES="
    acpid
    wicd
"

XTRA_LEAN_SERVICES="
    bootlogd
    cryptdisks
    cryptdisks-early
    dns-clean
    #eeepc-acpi-scripts
    hdparm
    hwclock.sh
    hwclockfirst.sh
    ifupdown-clean
    lm-sensors
    #lvm2
    mountnfs-bootclean.sh
    mountoverflowtmp
    nfs-common
    pcmciautils
    policykit
    pppd-dns
    #svgalib-bin
    ufw
    urandom
"

MEAN_SERVICES="
    networking
    resolvconf
"

NO_DBUS_SERVICES="
    dbus
"

PATH=/live/bin:$PATH

prep_main() {

    case $1 in
        start)          ;;
         stop) exit 0   ;;
            *) echo "Usage: $0 {start|stop}"
               exit 1   ;;
    esac

    : ${CMDLINE:=$(cat /proc/cmdline)}
    for param in $CMDLINE; do
        case "$param" in
            antiX=*|aX=*) antiX_param=${param#*=}; must_run=true ;;
           disable_srv=*) antiX_param=${param#*=}; must_run=true ;;
       disable_service=*) antiX_param=${param#*=}; must_run=true ;;
                    lean)        LEAN=true;        must_run=true ;;
                    mean)        MEAN=true;        must_run=true ;;
                Xtralean)   XTRA_LEAN=true;        must_run=true ;;
                  nodbus)     NO_DBUS=true;        must_run=true ;;
                    db++)   DEBUGGING=true                       ;;
        esac
    done

    if [ "$antiX_param" ]; then

        # If the ## expression matches then the resulting string length is zero
        [ "${antiX_param##*[lL]*}" ] ||      LEAN=true
        [ "${antiX_param##*[mM]*}" ] ||      MEAN=true
        [ "${antiX_param##*[xX]*}" ] || XTRA_LEAN=true
        [ "${antiX_param##*[dD]*}" ] ||   NO_DBUS=true
    fi

    [ "$must_run" ] || exit 0
    . /live/lib/live-init-utils.sh

    start_init_logging
    load_translation
}

main() {
    # Prevent other messages stepping on ours
    # sleep 2
    echo_script "$_Customizing_services_" $0

    if [ "$LEAN" ]; then

        if test -e /live/config/laptop; then
            echo_live "$_Detected_laptop_mobile_device_"
        else
            LEAN_SERVICES="$LEAN_SERVICES $LAPTOP_SERVICES"
        fi

        echo_live "$_Disabling_services_X_" "$(paren L, lean)"
        disable_services "$LEAN_SERVICES" "rc3.d rc5.d"
    fi

    if [ "$MEAN" ]; then
        echo_live "$_Disabling_network_auto_start_X_" "$(paren M, mean)"
        disable_services "$MEAN_SERVICES" "rcS.d"
    fi

    if [ "$XTRA_LEAN" ]; then
        echo_live "$_Disabling_more_services_X_" "$(paren X, Xtralean)"
        disable_services "$XTRA_LEAN_SERVICES" "rcS.d"
    fi

    if [ "$NO_DBUS" ]; then
        echo_live "$_Disabling_dbus_X_" "$(paren D, nodbus)"
        disable_services "$NO_DBUS_SERVICES" "rc3.d rc5.d"
    fi
} 


#----------------------------------------------------------------------------
# function disable_services "$names" [ "$rc_dirs" ]
#
# Temporarily disable services at boot-time by moving their /etc/rc?.d/
# symlinks.  See also live-restore-services which is the script
# that returns them to their proper place at the end of initialization.
#
# Move service symlinks out of various runlevels and into the matching
# /etc/antiX-live/rc?.d  directory.
#
# If no rc_dirs are given then rc?.d is used.
#----------------------------------------------------------------------------

disable_services() {

    local names="$1" rc_dirs="${2:-rc?.d}"

    local rc_dir dest name slink moved length

    for rc_dir in $rc_dirs; do

        # DISABLED_RC_DIR is defined in the library
        dest=$DISABLED_RC_DIR/$rc_dir
        mkdir -p $dest

        for name in $names; do
            [ -z "${name###*}" ]  && continue
            for slink in /etc/$rc_dir/S*$name; do
                [ -e "$slink" ] || continue
                mv -f $slink $dest

                #[ "$DEBUGGING" ] || continue

                [ "$moved" ] || echo -n "    "

                # Only print out each name once
                case ",$moved," in
                    *,$name,*)
                        continue;
                        ;;
                esac

                # Wrap long lines
                if [ "${#length}" -ge "60" ]; then
                    unset length
                    echo
                    echo -n "    "
                fi
                length="$length $name"
                moved="$moved,$name"
                echo -n "${PARAM_COLOR}$name$NO_COLOR "

            done
        done
    done

    [ "$moved" ] && echo

    [ "$DEBUGGING" ] || return

    local unused
    for name in $names; do
        case ",$moved," in
            *,$name,*)
                continue
                ;;
        esac
        unused="$unused $name"
    done
    [ "$unused" ] && echo_live '  Unused: %s' "$AMBER$unused"

}

prep_main "$@"
main  2>&1 | tee -a $INIT_LOG_FILE

exit 0

