#!/bin/bash

# Synopsis: configure_target_update_files
#
# This function is adapted from the Knoppix-Installer
function configure_target_update_files()
{
	send install_step configure_target_update_files
	# set up hostname
	cat <<EOF >$TARGET/etc/hosts
127.0.0.1	localhost
127.0.1.1	$cfg_hostname

# The following lines are desirable for IPv6 capable hosts
# (added automatically by netbase upgrade)

::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
EOF
	echo "$cfg_hostname" > $TARGET/etc/hostname
	echo "$cfg_hostname" > $TARGET/etc/mailname
	
	# remove LiveCD-user from /etc/sudoers
	cat > "$TARGET/etc/sudoers" <<EOF
# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults	env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root	ALL=(ALL) ALL
EOF
	chown root:root "$TARGET/etc/sudoers"
	chmod 0440 "$TARGET/etc/sudoers"
	
	# "normalize" /etc/inittab
	rm -f "$TARGET/etc/inittab"
	sed 's/id\:[0-6]\:initdefault\:/id\:5\:initdefault\:/;s/\([1-6]:23\):/\145:/' "$TARGET/usr/share/sysvinit/inittab" > "$TARGET/etc/inittab"
	
	# create "real" /tmp with mode 1777
	rm -f $TARGET/tmp 2>/dev/null
	mkdir -p $TARGET/tmp
	chmod 1777 $TARGET/tmp
	
	# create file to mount efivars (needed since bullseye)
	cat <<EOF >$TARGET/tmp/fixmount
#!/bin/sh
modprobe efivarfs
mount -t efivarfs efivarfs /sys/firmware/efi/efivars
EOF
	chmod ugo+x $TARGET/tmp/fixmount

	# create /etc/mtab as a regular file
	rm -f $TARGET/etc/mtab
	touch $TARGET/etc/mtab

	# configure /etc/kernel-pkg.conf
	if [ -f "$TARGET/etc/kernel-pkg.conf" ]; then
		perl -pi -e "s/^maintainer.*\=.*/maintainer \:\= $cfg_realname/;s/^email.*\=.*/email \:\= $cfg_username\@$cfg_hostname\.local/" "$TARGET/etc/kernel-pkg.conf"
		NUMCPUS=$(grep -c "model name" /proc/cpuinfo)
		[ $NUMCPUS -ge 2 ] && echo CONCURRENCY_LEVEL := $NUMCPUS >> $TARGET/etc/kernel-pkg.conf
	fi
	
	# enable KDE sounds
	rm -f "$TARGET/home/$cfg_username/.kde/share/config/knotifyrc"
		
	# profile
	cat "$TARGET/usr/share/base-files/profile" > "$TARGET/etc/profile"
	
	# For us users use an us-mirror
	case "$LANGUAGE" in
		us*)
			#perl -pi -e "s/ftp2.de.debian.org/ftp.uk.debian.org/" $TARGET/etc/apt/sources.list
			:
		;;
	esac
	
	# if build with debpool install selected packages
	# make a distinction between uefi and legacy packages
	# TODO implement ia32
	if [ -d /run/live/medium/debian ]; then
		if [ -d /sys/firmware/efi/efivars ]; then
		cp -f /run/live/medium/debian/pool/main/g/grub2/*.deb "$TARGET/tmp/"
		#cp -f /run/live/medium/debian/pool/main/m/mokutil/*.deb "$TARGET/tmp/"
		#cp -f /run/live/medium/debian/pool/main/r/refind/*.deb "$TARGET/tmp/"
		fi
	#cp -f /run/live/medium/debian/pool/main/o/openssh/*.deb "$TARGET/tmp/"
	ls "$TARGET/tmp/" >/tmp/target.log
	fi
	
	# install hooks
	if [ "$(ls $TARGET/usr/share/acritoxinstaller/target-config)" ]; then
		for hook in $TARGET/usr/share/acritoxinstaller/target-config/*
		do
			chroot_it "${hook#$TARGET}"
		done
	fi
	
	# install overlay
	if [ "$(ls $TARGET/usr/share/acritoxinstaller/target-overlay)" ]; then
		cp -a --target-directory=$TARGET $TARGET/usr/share/acritoxinstaller/target-overlay/*
	fi

	# add correct keymap
	[ -f /etc/sysconfig/keyboard ] && . /etc/sysconfig/keyboard

	[ -n "$KEYTABLE" ] && chroot_it install-keymap "$KEYTABLE" 2>/dev/null

        # remove live-repo from sources.list and delete key
        sed -i -e '/^# Live Media$/{N;\|\ndeb file:/live/image/debian |{N;/\n$/d}}' $TARGET/etc/apt/sources.list
        sed -i -e '/^# Live Media$/{N;\|\ndeb file:/run/live/medium/debian |{N;/\n$/d}}' $TARGET/etc/apt/sources.list
        sed -i -e '/^# Live Media$/{N;\|\ndeb \[signed-by=\/usr\/share\/keyrings\/live-media.gpg\] file:/run/live/medium/debian |{N;/\n$/d}}' $TARGET/etc/apt/sources.list
        rm -f $TARGET/etc/apt/trusted.gpg.d/live-media.asc $TARGET/usr/share/keyrings/live-media.gpg
}

# Synopsis: configure_target_purge_live_only_stuff
#
# This function removes everything that is only useful on live-systems
function configure_target_purge_live_only_stuff()
{
	send install_step configure_target_purge_live_only_stuff
	# remove live-only-packages
	chroot_it dpkg --purge \
			acritoxinstaller \
			acritoxinstaller-icons \
			acritoxinstaller-kanotix \
			kanotix-enable-persistent-live \
			calamares \
			calamares-settings-kanotix \
			live-boot-initramfs-tools \
			live-boot \
			live-initramfs \
			live-build-cgi \
			live-build \
			live-helper \
			live-config-runit \
			live-config-sysvinit \
			live-config-upstart \
			live-config-systemd \
			live-config \
			live-magic \
			live-manual-epub \
			live-manual-html \
			live-manual-odf \
			live-manual-pdf \
			live-manual-txt \
			live-manual \
			gfxoverlay \
			zram	 	&> /dev/null

	# remove unused stuff
	chroot_it apt-get autoremove --purge --yes	&> /dev/null
	
	# install copied file from debpool
	if [ -d /run/live/medium/debian ]; then
	DEBIAN_FRONTEND=noninteractive chroot_it dpkg -R -i /tmp	&> /dev/null
	fi
	
	# execute above configured fix for efi (new since bullseye)
	if [ -d /sys/firmware/efi/efivars ]; then
	chroot_it /tmp/fixmount
	fi

        # remove kde-config-touchpad if no touchpad is available
        [ "$(su "$FLL_LIVE_USER" -c "qdbus org.kde.synaptiks /modules/synaptiks org.kde.Synaptiks.isTouchpadAvailable")" = true ] && \
                chroot_it dpkg --purge kde-config-touchpad
	
	#  disable live config
	[ -f "$TARGET/etc/default/distro" ] && \
		perl -pi -e "s/^FLL_DISTRO_MODE\=.*/FLL_DISTRO_MODE\=\"installed\"/" "$TARGET/etc/default/distro"
	
	# remove temporary kde- user files
	rm -f "$TARGET/home/$cfg_username/.DCOPserver_*_*"
	rm -f "$TARGET/home/$cfg_username/.kde/cache-*"
	rm -f "$TARGET/home/$cfg_username/.kde/socket-*"
	rm -f "$TARGET/home/$cfg_username/.kde/tmp-*"
	rm -f "$TARGET/home/$cfg_username/.*uthority"
	
	rm -rf "$TARGET/etc/sysconfig"
	rm -f "$TARGET/home/$cfg_username/Desktop/install-gui.desktop"

	# remove kdm live shutdown hack
	rm -f "$TARGET/home/$cfg_username/.kde/shutdown/kdm-force-shutdown-hack"

	# remove with live-boot created powermanagementprofilesrc ,will be automatically recreated at plasma5 session-startup
	rm -f "$TARGET/home/$cfg_username/.config/powermanagementprofilesrc"

        # remove live-only hooks
        rm -rf $TARGET/lib/live
        rm -f $TARGET/etc/profile.d/zz-live.sh
        rm -f $TARGET/usr/local/bin/enable_persistent_live.bash
	rm -f "$TARGET/home/$cfg_username/Desktop/enable_persistent.desktop"

	#remove appimage desktop files
	if [ -e /run/live/medium/appimages ]; then
	for appimages in /run/live/medium/appimages/*
	do
	appimage=$(basename $appimages)
	rm -f "$TARGET/home/$cfg_username/Desktop/$appimage.desktop"
	done
	fi
}

