#!/bin/sh
### BEGIN INIT INFO
# Provides:          aaa-before-all
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     false
# Short-Description: Configure aaa-before-all
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO
# Policy Info:  http://wiki.debian.org/LSBInitScripts
# also check /etc/init.d/skeleton

# Author: Author


# Defaults
#PIDDIR=/var/run/aaa-before-all
#aaa-before-allPID=$PIDDIR/aaa-before-all.pid
#DESC="Configure aaa-before-all"
SCRIPTNAME=/etc/init.d/aaa-before-all


# Reads config file (will override defaults above)
[ -r /etc/default/aaa-before-all ] && . /etc/default/aaa-before-all

. /lib/lsb/init-functions



do_start(){
    if ! LC_ALL=C grep -Fqs "boot=live" /proc/cmdline ; then
        exit 0
    fi
    if LC_ALL=C grep -Fqs "debug" /proc/cmdline ; then
        set -x
    fi

    #memory="$( cat /proc/meminfo | grep -Fi MemTotal | tail -1 | awk '{print $2}' )"
    #RAM_TOTAL_SIZE_mb="$(( $memory / 1024 ))"
    #_MEM_TOTAL_KB="$(awk '/^MemTotal: / { print $2 }' /proc/meminfo)"
    #_MEM_TOTAL_MB="$(expr ${_MEM_TOTAL_KB} / 1024)"


	# only if /bin/bash
    #source /usr/lib/elive-tools/functions

    #log_daemon_msg "Starting preinit daemons"

    # XXX IMPORTANT XXX this is sh, not bash, do not use bashisms !

    # thanadesk force to run hooks in his preconfigured desktop {{{
    # just a dummy desktop configuration for fast tests
    if LC_ALL=C grep -Fqs "thanadesk" /proc/cmdline ; then
        if [ -d "/home/eliveuser/.e_thanatests" ] ; then
            rm -rf "/home/eliveuser/.e" 2>/dev/null
            mv "/home/eliveuser/.e_thanatests" "/home/eliveuser/.e"

            cat >> "/home/eliveuser/.elxstrt" << EOF
urxvt -geometry 100x60 -T 'Running Hooks' -e bash -c "source /usr/lib/elive-tools/functions ; for file in /usr/lib/deliver/hooks.d/e17-wizard/last/* /usr/lib/deliver/hooks.d/e17-wizard/after/* ; do  if ! echo \"\\\$file\" | grep -qsE \"(virtualbox-checkers|wallpapers-switching|composite-checkers|elive-welcome|crowdfunding|install-elive|precache)\" ; then   el_info \"running  \\\$file\" ; bash \"\\\$file\" ; else el_info \"    skipped  \\\$file\" ; fi ; done ; echo ; echo \"DONE, you can close terminal\" ; sleep 30 "
EOF
        fi
    fi

    # - thanadesk force to run hooks in his preconfigured desktop }}}
    # checks {{{
    if ! grep -Fqs "UNTRUSTED-OS" /proc/cmdline ; then
        if test -d "/run/live/medium/live" ; then
            medium="/run/live/medium"
        else
            if test -d "/lib/live/mount/medium" ; then
                medium="/lib/live/mount/medium"
            fi
        fi
        if test -d "$medium" ; then
            for i in $medium/live/initrd* $medium/live/vmlinuz* $medium/live/filesystem.squashfs
            do
                if test -s "$i" && ! echo "$i" | grep -qs -- "\.sig" && ! grep -Fqs "SIGNATURE" "${i}.sig" ; then
                    # halt the system, any untrusted Elive system imposes a security risk
                    # TODO: we need to find a way to know if the isos on the mirrors has been altered
                    sync ; sleep 10 ; sync

                    log_daemon_msg "This is not a trusted Elive system or your USB/DVD has not been recorded with the correct tools, and Download it only from https://www.elivecd.org"
                    echo "This is not a trusted Elive system or your USB/DVD has not been recorded with the correct tools, and Download it only from https://www.elivecd.org"
                    echo "This is not a trusted Elive system or your USB/DVD has not been recorded with the correct tools, and Download it only from https://www.elivecd.org" 1>&2

                    sleep 30
                    systemctl --force --force poweroff 1>/dev/null 2>&1 || true
                    shutdown now
                fi
            done
        fi
    fi
    # - checks }}}


    # precache it for a fast startup
    # moved to zx startx because seems like we lose the cache before to use it
    #if [ "$( readlink -f /etc/alternatives/x-window-manager )" = *"starte16" ] && test -n "$(which pulseaudio)" ; then
        #( precache --nice "$(which e16 2>/dev/null)" "$(which pulseaudio 2>/dev/null)" "$(which kupfer 2>/dev/null)" /usr/share/e16/themes/Elive_Dark/sound/samples/* /usr/share/applications/* /usr/share/xdgeldsk/applications/*  1>/dev/null  &  )
    #else
        #if test -e "/usr/bin/enlightenment_start" ; then
            #( precache --nice "$(which enlightenment_start 2>/dev/null)" "$(which enlightenment 2>/dev/null)" "$(which pulseaudio 2>/dev/null)" /usr/share/enlightenment/data/backgrounds/* /usr/bin/elementary_config /usr/share/elementary/themes/*edj /usr/share/applications/* /usr/share/xdgeldsk/applications/*  1>/dev/null  &  )
        #fi
    #fi

    # mark done for systemd
    echo "$0" >> /tmp/.live-processes-finished


    # turn off debugging if we have finished
    if LC_ALL=C grep -Fqs "debug" /proc/cmdline ; then
        set +x
    fi

    true
    #log_end_msg 0
}

do_stop(){
    log_daemon_msg "Shutting down computer"
    # log_progress_msg "preparing"
}

case "$1" in
    "")
        echo "Error: aaa-before-all should be called with the 'start' argument." >&2
        ;;
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    restart|reload|force-reload)
        "$0" stop
        "$0" start
        ;;
    *)
        echo "Usage: aaa-before-all [start|stop]" >&2
        exit 3
        ;;
esac


# vim: set filetype=sh foldmethod=marker :
