#!/bin/sh
#
# Removes the live user (blankon) from the installed system,
# but only if there is another user present.
#

CHROOT=$(mount | grep proc | grep calamares | awk '{print $3}' | sed -e "s#/proc##g")
LIVE_USER="blankon"

# Count normal users (UID >= 1000, excluding nobody)
USER_COUNT=$(awk -F: '$3 >= 1000 && $1 != "nobody" { count++ } END { print count }' "$CHROOT/etc/passwd")

if [ "$USER_COUNT" -ge 2 ]; then
    # There are at least 2 normal users, check if blankon is one of them
    if grep -q "^${LIVE_USER}:" "$CHROOT/etc/passwd"; then
        chroot "$CHROOT" userdel -r "$LIVE_USER" 2>/dev/null
    fi
fi

exit 0
