#!/bin/sh

### BEGIN INIT INFO
# Provides:          antiX-disable-services 
# Required-Start:    checkfs checkroot-bootclean antiX-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

# GETTEXT_KEYWORD="echo_live"
# GETTEXT_KEYWORD="echo_script"

LEAN_SERVICES="
    acpid
    acpi-fakekey
    anacron
    bluetooth
    bootlogs
    bluetooth
    cpufrequtils
    cron
    cups
    gpm
    loadcpufreq
    nfs-common
    nfs-kernel-server
    openvpn
    rpcbind
    rmnologin
    rsync
    rsyslog
    saned
    smartmontools
    spamassassin 
    ssh
    stop-bootlogd
    sudo
"

XTRA_LEAN_SERVICES="
    bootlogd
    dns-clean
    hdparm
    hwclock.sh
    lm-sensors
    lvm2
    mountnfs-bootclean.sh
    mountoverflowtmp
    networking
    nfs-common
    pppd-dns
    rpcbind
    urandom
    winbind
"

MEAN_SERVICES="
    network-manager
    nmbd
    samba
    samba-ad-dc
    smbd
"

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

CMDLINE=${CMDLINE:-$(cat /proc/cmdline)}
for param in $CMDLINE; do
    case "$param" in
        antiX=*|aX=*) 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

#----------------------------------------------------------------------------
# function disable_services "$names" [ "$rc_dirs" ]
#
# Temporarily disable services at boot-time by moving their /etc/rc?.d/
# symlinks.  See also antiX-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_DIRS" ] || RC_DIRS="rc?.d"

    local rc_dir dest name slink moved length

    for rc_dir in $RC_DIRS; do

        dest=$DISABLED_RC_DIR/$rc_dir
        mkdir -p $dest

        for name in $NAMES; do

            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

    [ "$DEBUGGING" ] || return

    [ "$moved" ] && echo


    local unused
    for name in $NAMES; do
        case ",$moved," in
            *,$name,*)
                continue
                ;;
        esac
        unused="$unused $name"
    done
    [ "$unused" ] && echo_live "$_Unused_X_" "$AMBER$unused"

}

. /usr/share/antiX/lib/antiX-init-utils.sh

start_init_logging
load_translation

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

    if [ "$LEAN" ]; then
        echo_live "$_Disabling_services_X_" "$(paren lean)"
        disable_services "$LEAN_SERVICES" "rc3.d rc5.d"
    fi

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

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

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

} 2>&1 | tee -a $INIT_LOG_FILE

exit 0

