# This file is part of the 'dragora-installer'.
#
# Purpose: Set super-user (root) password.

# Make sure to have /dev available for passwd(1)
if ! mountpoint -q /media/dragora-root/dev
then
    mount --bind /dev /media/dragora-root/dev
fi

while test "$(grep -m 1 "^root:" /media/dragora-root/etc/shadow | cut -f 2 -d :)" = x
do
    _status=0

    dialog --colors \
     --backtitle "\\ZbSystem administrator password" \
     --title "PASSWORD NOT DETECTED" \
     --ok-button "Continue" --msgbox \
"No password has been detected for the system administrator\\n\
(root) account.\\n\\nIt is important to set it in order to \
have access to the entire system.  Please do it now!" 10 62 || _status=$?
    if test $_status -eq 0
    then
        echo ""
        chroot /media/dragora-root /usr/bin/passwd root
        echo ""
        echo " To continue, press [Enter]..."
        IFS= read -r REPLY < /dev/tty || exit 2;
        unset -v REPLY
        continue;
    elif test $_status -eq 255
    then
        dialog \
         --title "System administrator password" \
         --defaultno --yesno \
"\\nAre you sure you want to leave this without a password?\\n\
You will not have full access to the system." 8 59 || continue;
    fi
    break;
done
unset -v _status

# Refresh shadow files
chroot /media/dragora-root /usr/sbin/shadowconfig off > /dev/null 2>&1 || true
chroot /media/dragora-root /usr/sbin/shadowconfig on  > /dev/null 2>&1 || true

dialog --clear

