#!/bin/sh

TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
T_PX="`cat $TMP/SeTT_PX`"
#while [ "`cat $T_PX/etc/shadow | grep 'root:' | cut -f 2 -d :`" = "" ]; do
  # There is no root password
  dialog --title "Password setup" --msgbox \
"You will be presented with some dialogs to setup a password for the \
root user. Make sure that this is a strong password.\n\nAfter that you \
will be presented with a menu that allows you to setup user accounts. \
You are advised to create at least one non-privileged user account for \
everyday use.\n\nUsing the system as the root user is highly not \
recommended. Moreover, if you are doing a \"basic\" or \"full\" installation, \
root user login in the graphical environment is \
disabled by default." 0 0

# Password dialog
passbox(){
        DIALOG='dialog --stdout --insecure --fixed-font --no-cancel --smooth --passwordbox'
        pw="$(${DIALOG} "${1}" 12 80)"
        echo -n "$pw"
}


# Password checking
changepw(){

minlength=5

count=1 ; pw='_'
while [ "${pw}" != "${password}" ]; do
        password="" ; pw='_'

        message="Enter password for the root user:"
        [ $count -ge 2 ] && message="Passwords don't match, enter password for the root user:"
        while [ ${#pw} -lt ${minlength} ]; do
                pw="$(passbox "${message}")"
                message="Password is too short (${minlength} chars minimum), please try another one:"
        done
        password="$pw" ; pw='_'
        message="Enter password for the root user again:"
        pw="$(passbox "${message}")"

        count=$((count+1))

done

echo -n $password
}

# Change password
setpw() {
        pass="$(changepw root)"
        if [ ! "$pass" ] ; then
                dialog --msgbox "Failed to set password for the root user" 8 75
        else
                echo "root:${pass}" | chroot $T_PX chpasswd
        fi
}

setpw
######################################################################
#    chroot $T_PX /usr/bin/passwd root
#    echo
#    echo -n "Press [enter] to continue:"
#    read junk;
#    echo
    # Here we drop through, and if there's still no password the menu
    # runs again.
#  else
    # Don't set a password:
#    break;
#  fi
#done

chroot $T_PX /usr/sbin/usersetup

