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

# Generate a random hostname which will be used in the installed system
# This allows to identify machines inside a network
( set -eu
  if [ -f /etc/.nogenhostname ] || grep -q genhostname=0 /proc/cmdline ; then exit; fi
  . /etc/os-release || :
  # systemd is built with -Dfallback-hostname="%{distsuffix}%{mandriva_release}"
  # ROSA_OS_PLATFORM=rosa2021.1 in /etc/os-release (abf.io/import/branding-configs)
  # Do nothing if hostname was set somehow else then systemd default
  # (e.g. hostname= or ip= in kernel cmdline)
  if ! hostnamectl | grep -q "^Transient hostname: ${ROSA_OS_PLATFORM:-}$"; then exit; fi
  if [ -z "${ID:-}" ]; then ID=rosa; fi
  # Filter o, O, 0, i, I, l, v, w, V, W because they can be confused
  # Make the 3rd symbol be a number so that a bad word does not get generated
  p1="$(tr -dc A-HJ-KM-NP-UX-Za-hj-km-np-ux-z < /dev/urandom | head -c 2)"
  p2="$(tr -dc 1-9 < /dev/urandom | head -c 1)"
  p3="$(tr -dc A-HJ-KM-NP-UX-Za-hj-km-np-ux-z < /dev/urandom | head -c 3)"
  hostname="${ID}-${p1}${p2}${p3}"
  # To lower case
  hostname="${hostname,,}"
  [ -n "$hostname" ]
  hostnamectl set-hostname "$hostname"
)

# 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

# Set up livecd language from grub menus
# (see grub configs in livecd-tools)
if [ -n "${LANGUAGE:-}" ] && [ "${LANGUAGE:0:2}" != "en" ]; then
	localectl set-locale "$LANGUAGE"
	localectl set-x11-keymap "us,${LANGUAGE:0:2}" pc105 "" grp:alt_shift_toggle
# for gnome wayland
	gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'us'), ('xkb', '${LANGUAGE:0:2}')]" || :
fi

### 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
	find . -maxdepth 1 -type f -print | sort | while read -r i
	do
		"$i" || :
	done
	popd
fi

# Mark things as configured
touch /.liveimg-configured
 
