#!/usr/bin/bash
#
# Authors:
# Eric Vidal <eric@obarun.org>
#
# Copyright (C) 2015-2017 Eric Vidal <eric@obarun.org>
#
## This script was made for provide obarun environment. This scripts is under license BEER-WARE.
# "THE BEER-WARE LICENSE" (Revision 42):
# <eric@obarun.org> wrote this file.  As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return.   Eric Vidal

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
			out_error "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(){
	out_action "add ${NEWUSER} at log group"
	gpasswd -a "$NEWUSER" log
}
custo_once create_log_user 

##		Configure S6 service

config_s6(){
	
	out_action "Enable syslogds6 service"
	cp -ra /etc/s6-serv/available/classic/syslogds6 /etc/s6-serv/enabled/classic/
	
	out_action "Create Live database"
	s6opts add live "bundle-Dbus bundle-Networkmanager"
	s6opts compile Live live	
	
	out_action "Make Live as current database"
	rm /etc/s6-serv/enabled/rc/compiled/current
	ln -sf /etc/s6-serv/enabled/rc/compiled/Live /etc/s6-serv/enabled/rc/compiled/current
}
custo_once config_s6

rc_local(){
	sed -i '1s/^/#!\/bin\/bash\n/' /etc/s6/data/scripts/s6.local
	sed -i '$a/usr/bin/pacopts applytmp' /etc/s6/data/scripts/s6.local
	sed -i 's:^S6LOCAL.*$:S6LOCAL=yes:' /etc/s6/s6.conf
}
custo_once rc_local

##      Configure pulseaudio

config_pulse(){
	echo "load-module module-device-manager" >> /etc/pulse/default.pa
}
custo_once config_pulse

out_valid "Customization terminate"