#!/bin/bash
## 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

#shopt -s extglob

##		Check is the functions file exits

sourcing(){
	
	local list
	
	for list in /tmp/obarun-install-tmp/{common_functions,install_functions,install.conf}; do
		if [[ -f "${list}" ]]; then
			source "${list}"
		else
			echo_error " Missing file : ${list}"
			exit	
		fi
	done
	
	unset list
}
sourcing

shellopts_save
shellopts_set_unset "extglob" 0

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

##		Customize menu

customizeChroot_menu

# reload source file before using changed variable
source /tmp/obarun-install-tmp/install.conf

custo_once config_custofile

##		Define resolv.conf
def_resolv(){
	define_resolv 
}
custo_once def_resolv 

##		Uncomment server in mirrorlist
mirrorlist(){
	echo_display " Uncomment server in mirrorlist"
	sed -i "s/#Server/Server/g" /etc/pacman.d/mirrorlist
}
custo_once mirrorlist 

##		Pacman stuff
pac_sync(){
echo_display " Synchronize database..."
if [[ ! -d /var/lib/pacman/sync ]]; then 
    pacman -Syy
else
	pacman -Sy
fi
}
custo_once pac_sync 

## Configure pacman.conf

config_pac(){
	echo_display " Change pacman.conf configuration"
	sed -i "s:SigLevel = Never.*#:SigLevel = Required DatabaseOptional:" /etc/pacman.conf
	sed -i "s:#SigLevel = PackageRequired:SigLevel = PackageRequired:" /etc/pacman.conf
}
custo_once config_pac

## Check if gpg exist, if not launch pacman-key and add gpg signature
custo_once check_gpg

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

##		Prepare socklog and install socklog

create_log_user(){
	echo_display " Create user log, add {root,$NEWUSER} at log group"
	useradd -M -N -s /usr/bin/nologin log
	gpasswd -a root log
	gpasswd -a "$NEWUSER" log
}
custo_once create_log_user 

install_socklog(){
	echo_display " Install socklog-obarun"
	pacman -S socklog-obarun --noconfirm
}
custo_once install_socklog

##		Configure Runit services

config_runit(){
	echo_display " Create symlink for runit"
	ln -sf /etc/sv/dbus /etc/runit/runsvdir/base
	ln -sf /etc/sv/klog /etc/runit/runsvdir/base
	ln -sf /etc/sv/socklog-unix /etc/runit/runsvdir/base
	ln -sf /etc/sv/networkmanager /etc/runit/runsvdir/base
}
custo_once config_runit


rc_local(){
	sed -i '1s/^/#!\/bin\/bash\n/' /etc/rc.local
	sed -i '$a/usr/bin/pacopts applytmp' /etc/rc.local
}
custo_once rc_local

##      Configure pulseaudio

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

##		Remove systemd stuff

rm_systemd(){
	echo_display " Removing systemd stuff"
	rm -r /usr/lib/systemd
}
custo_once rm_systemd

##		Install bootloader

install_boot(){
	echo_display " Do you want to install ${green}[syslinux]${reset}${bold} bootloader [y|n] :"
	reply_answer 
	if (( ! $? )); then
		syslinux_menu
	fi
}
custo_once install_boot

shellopts_restore

echo_valid " Customization terminate"