#!@BINDIR@/bash
# Copyright (c) 2015-2018 Eric Vidal <eric@obarun.org>
# All rights reserved.
# 
# This file is part of Obarun. It is subject to the license terms in
# the LICENSE file found in the top-level directory of this
# distribution.
# This file may not be copied, modified, propagated, or distributed
# except according to the terms contained in the LICENSE file.

out_progress() {
    oblog "[ $1 ]"
    oblog "      " 
}

sourcing(){

    local list

    for list in /tmp/obarun-install-tmp/install.conf /usr/lib/obarun/util.sh; do
        if [[ -f "${list}" ]]; then
            source "${list}"
        else
            oblog -f "Missing file : ${list}"
            exit	
        fi
    done
    
    unset list
}
sourcing

custo_once() {
    local _tmp cmd
    cmd="${1}"
    _tmp="/tmp/obarun-install-tmp"
    
    if [[ ! -d $_tmp ]]; then
        mkdir -p -m0755 $_tmp || die "Impossible to create $_tmp"
    fi
    if [[ ! -e $_tmp/customize.${cmd} ]]; then
        "${cmd}" || die "Cannot execute $_"
        touch $_tmp/customize.${cmd}
    else
        return
    fi
    unset _tmp
}

############################## only modifie this script after this line ############################

create_log_user(){
    oblog "add ${NEWUSER} to log group"
    gpasswd -a "$NEWUSER" log
}
custo_once create_log_user

config_66(){

    oblog "Create [boot] tree"
    out_progress "Starting ..."
    sleep 2
        66-tree -v3 -n boot

    out_progress "Done"
    sleep 1

    oblog "Enable (boot) service to [boot] tree"
    out_progress "Starting ..."
    sleep 2
        66-enable -v3 -t boot boot

    out_progress "Done"
    sleep 1

    oblog "Create and enable [root] tree"
    out_progress "Starting ..."
    sleep 2
        66-tree -v3 -nE root

    out_progress "Done"
    sleep 1

    oblog "Enable (tty1) (tty2) services to [root] tree"
    out_progress "Starting ..."
    sleep 2
        66-enable -v3 -t root tty@tty1 tty@tty2

    out_progress "Done"
    sleep 1

    oblog "Create and enable [system] tree"
    out_progress "Starting ..."
    sleep 2
        66-tree -v3 -nE system

    out_progress "Done"
    sleep 1

    oblog "Enable (sshd) service to [system] tree"
    out_progress "Starting ..."
    sleep 2
        66-enable -v3 -t system sshd

    out_progress "Done"
    sleep 1

    oblog "Create and enable [${NEWUSER}-session] tree"
    out_progress "Starting ..."
    sleep 2
        66-tree -v3 -nE ${NEWUSER}-session

    out_progress "Done"
    sleep 1

    oblog "Configure and enable (boot-user@${NEWUSER}) service to [${NEWUSER}-session] tree"
    out_progress "Starting ..."
    sleep 2
        66-mods.sh boot-user@${NEWUSER}
        66-enable -t ${NEWUSER}-session All-${NEWUSER}

    out_progress "Done"
    sleep 1

    oblog "Create and enable [desktop] tree. Set it as the current tree"
    out_progress "Starting ..."
    sleep 2
        66-tree -v3 -nEc desktop

    out_progress "Done"
    sleep 1

    oblog "Enable (dbus) (networkmanager) (sddm) (consolekit) services to [desktop] tree"
    out_progress "Starting ..."
    sleep 2
        66-enable -v3 -t desktop dbus networkmanager sddm consolekit

    out_progress "Done"
    sleep 1

    oblog "For ${NEWUSER}: Create and enable [${NEWUSER}] tree. Set it as the current user tree"
    out_progress "Starting ..."
    sleep 2
        su "${NEWUSER}" -c "66-tree -v3 -nEc ${NEWUSER}"

    out_progress "Done"
    sleep 1

    oblog "Enable (xdg-user-dirs) (dbus-session@${NEWUSER}) services to [${NEWUSER}] tree"
    out_progress "Starting ..."
    sleep 2
        su "${NEWUSER}" -c "66-enable -v3 xdg-user-dirs dbus-session@${NEWUSER}"

    out_progress "Done"
    sleep 1
}
custo_once config_66

oblog "Configuration completed"