#!/bin/bash
set -eu
set +f
# This script makes changes before a LiveCD session is run,
# but Anaconda copies the origianl layer of the LiveCD,
# so these changes do not get into an installed system.
# Based on pagure.io/fedora-kickstarts

# Some scripts may need to detect if they are executed by Anaconda or not
export I_AM_ANACONDA=1

# Very important to be able to log into console
# Do it in the beginning to not make not loginable console if this script fails further
passwd -d root

# install live user
groups="wheel"
for i in users nopasswdlogin sambashare
do
	if grep -q "^${i}:" /etc/group ; then
		groups="${groups},${i}"
	fi
done
useradd --uid 500 --comment "ROSA Live User" --groups "$groups" live
cp -rfT /etc/skel /home/live/
# /home/live/Рабочий стол
XDG_DESKTOP_DIR="$(sh -c 'HOME=/home/live; if [ -f /home/live/.config/user-dirs.dirs ]; then . /home/live/.config/user-dirs.dirs && echo $XDG_DESKTOP_DIR; fi')"
if [ -z "$XDG_DESKTOP_DIR" ]; then
	XDG_DESKTOP_DIR=/home/live/Desktop
fi
mkdir -p "$XDG_DESKTOP_DIR"
cp -v /usr/share/applications/anaconda-rosa.desktop "${XDG_DESKTOP_DIR}/"
# For DEs which check x bit on desktop files
# TODO: preadd metadata for GVFS to trust this desktop file
chmod +x "${XDG_DESKTOP_DIR}/anaconda-rosa.desktop"
chown -R live:live /home/live
passwd -d live

# turn off firstboot for livecd boots
systemctl --no-reload disable firstboot-text.service 2> /dev/null || :
systemctl --no-reload disable firstboot-graphical.service 2> /dev/null || :
systemctl stop firstboot-text.service 2> /dev/null || :
systemctl stop firstboot-graphical.service 2> /dev/null || :

# don't use prelink on a running live image
if [ -f /etc/sysconfig/prelink ]; then
	sed -i 's/PRELINKING=yes/PRELINKING=no/' /etc/sysconfig/prelink &>/dev/null || :
fi

# turn off mdmonitor by default
systemctl --no-reload disable mdmonitor.service 2> /dev/null || :
systemctl --no-reload disable mdmonitor-takeover.service 2> /dev/null || :
systemctl stop mdmonitor.service 2> /dev/null || :
systemctl stop mdmonitor-takeover.service 2> /dev/null || :

# don't enable the gnome-settings-daemon packagekit plugin
gsettings set org.gnome.software download-updates 'false' || :

# don't start cron/at as they tend to spawn things which are
# disk intensive that are painful on a live image
systemctl --no-reload disable crond.service 2> /dev/null || :
systemctl --no-reload disable atd.service 2> /dev/null || :
systemctl stop crond.service 2> /dev/null || :
systemctl stop atd.service 2> /dev/null || :

# turn off abrtd on a live image
systemctl --no-reload disable abrtd.service 2> /dev/null || :
systemctl stop abrtd.service 2> /dev/null || :

# Don't sync the system clock when running live (RHBZ #1018162)
sed -i 's/rtcsync//' /etc/chrony.conf

### PLACE FOR ADDITIONAL SCRIPTS ###
# This script may be edited when building a LiveCD,
# additional commands can be inserted bellow

# Scripts to be run may be placed here
if [ -d /etc/anaconda-scripts.d/livecd-init ]; then
	pushd /etc/anaconda-scripts.d/livecd-init
	for i in * ; do
		"./$i" || :
	done
	popd
	for i in /etc/anaconda-scripts.d/livecd-init/*
	do
		if ! rpm -qf "$i" ; then
			rm -fvr "$i"
		fi
	done
fi

# Mark things as configured
touch /.liveimg-configured
 
