#!/bin/sh

# (tpg) this script is executed bu calamares after installation is done
# see https://github.com/OpenMandrivaSoftware/distro-release/blob/master/installer/shellprocess.conf

wipe_locale()
{
    local lang list nosed engb
    lang=$(grep ^LANG= /etc/locale.conf | sed 's/.*=//g' | sed 's/_.*//g')
    [ -z "$lang" ] && printf '%s\n' "Error: Cannot find locale settings" && exit 1
    case $lang in
	    C*) nosed=1 ;;
	    *) nosed=0 ;;
    esac

## let -en* stuff alway be there , some apps may just have stubs
## or incomplete translation and that always has an failback to en_*
    list=$(dnf -C -q list installed | grep ^locales- | awk '{print $1}' | sed 's/\(.*-.*\).[a-z].*/\1/' | grep -v locales-en)

# (crazy) disable for now , myspell build changes and now -en packages
# provides en_GB so we cannot remove anymore like this

#	if [ "$lang" != 'en' ]; then
#	    engb=(libreoffice-l10n-en_GB myspell-en_GB firefox-en_GB)
#	    list=(${list[@]} ${engb[@]})
#	fi

## now simple run remove
    if [[ -n ${list[@]} ]]; then
	printf '%s\n' "Removing not needed language packages.."
	if [ $nosed -eq 0 ]; then
	    dnf --disablerepo="*" -C -y remove ${list[@]/locales-$lang}
	else
	    dnf --disablerepo="*" -C -y remove  ${list[@]}
	fi
    else
	printf '%s\n' "Nothing to do.."
    fi
}

wipe_vbox()
{
    local dmifile
    dmifile=$(cat /sys/class/dmi/id/product_name)
    [ -z "$dmifile" ] && printf '%s\n' "Error: DMI entry empty?" && exit 1

    if [ "$dmifile" != 'VirtualBox' ]; then
	printf '%s\n' "We are not on VirtualBox , removing virtualbox-guest-additions"
	dnf --disablerepo="*" -C -y remove  virtualbox-guest-additions
    else
	printf '%s\n' "Looks like a VirtualBox VM, keeping virtualbox packages."
    fi
}

iso_stuff()
{
    local i
    printf '%s\n' "Cleaning up.."
## for unknow reson firewalld has somewho some script error
## and a file '1' is installed into rootfs..
    [ -e /1 ] && rm -rf /1

## /root/tmp ?
    [ -d /root/tmp ] && rm -rf /root/tmp

## /live
    [ -d /live ] && rm -rf /live

# remove sshd config for live iso
[ -f /etc/ssh/sshd_config.d/50-live-iso.conf ] && rm -rf /etc/ssh/sshd_config.d/50-live-iso.conf

## clean .bak .rpmsave files .. strange to have
## an fresh installed system with the on it
    for i in $(ls /etc/*.{bak,rpmsave}); do
	rm -rf "$i"
    done

# game requested for Live session
    dnf --disablerepo="*" -C -y remove kpat
    chown -R rpm:rpm /var/lib/rpm
    rm -rf /var/lib/rpm/.dbenv.lock
    rm -rf /var/lib/rpm/.rpm.lock
}

wipe_locale
iso_stuff
wipe_vbox
