#!/bin/bash
SOURCE="$0"
source /usr/lib/elive-tools/functions
EL_REPORTS="1"
el_make_environment
# gettext functions
if [[ -x "/usr/bin/gettext.sh" ]] ; then
    . gettext.sh
else
    # make it compatible
    eval_gettext(){
        echo "$@"
    }
fi

TEXTDOMAIN="deliver"
export TEXTDOMAIN

# if we are in e16, our updated (reconfigured) language must be updated, that's not needed in e17 since the ENV var is set
if [[ -n "$E_ROOT" ]] ; then
    source /etc/default/locale 2>/dev/null || true
fi


main(){
    # debug mode
    if grep -Fqs "debug" /proc/cmdline ; then
        export EL_DEBUG=3
        if grep -Fqs "completedebug" /proc/cmdline ; then
            set -x
        fi
    fi

    # only if we have boot with the persistence (wanted) option in boot, it should be enough clear to not even tell the user from here if he want's it

    # Note: the initscript order for this should be the last one because we need to have all the things finished to be configured before to run it, also it is a good thing to play with ecomorph and others to see how happy the user is before to decide it

    #if ! el_flag check "persistence-setup" ; then
    while read -ru 3 line
    do
        dev="$( echo "$line" | awk '{print $1}' )"
        label="$( partitions-list --show-only="$dev" | awk -v FS="::" '{print $4}' )"

        if [[ "$label" = *persistence* ]] ; then
            is_running=1
        fi
    done 3<<< "$( grep "^/dev/.*mount/persistence" "/proc/mounts")"

    # reload a possible reordering of the dameons and exit
    if ((is_running)) ; then
        sudo -H systemctl daemon-reload
        exit 0
    fi

    # persistence is not already mounted so run the tool:
    persistence-live-helper --auto


    # if we are debugging give it a little pause to see what is going on
    #if grep -Fqs "debug" /proc/cmdline ; then
        #echo -e "debug: sleep 2" 1>&2
        #sleep 2
    #fi

    exit 0
}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
