#!/bin/bash
# Under the terms of the 'COPYRIGHT.TXT' file.

# Variables por defecto:
UID_NAME=root
GID_NAME=root
THE_HOME=/root

# Cambia los valores de las variables,
# segn los resultados obtenidos:
printf -v UID_NAME "$(chroot /mnt /usr/bin/id -un)"
printf -v GID_NAME "$(chroot /mnt /usr/bin/id -gn)"
THE_HOME=$(grep -m1 "^${UID_NAME}:" /mnt/etc/passwd | cut -f6 -d:)

# Pregunta si se desea cambiar el nombre del super-usuario:
  dialog \
   --backtitle "Setting the super user name" \
   --title "SUPER USER ACCOUNT (${UID_NAME})" \
   --defaultno \
   --yesno \
"The account of the super user has been detected.\n\n
You have the possibility to change the name of the super user (usually root). \
This can increase the security of the system.\n\n\
It is not recommended to use in conjunction with D-BUS, HAL, \
or related programs.\n\n\
Do you want to change the name of the super user?" 14 55
if (( $? == 0 )); then
  dialog \
   --inputbox "Enter the name of the new super-user:" 8 43 \
    2> ${TMP}/set-name-superuser$$
  if (( $? == 0 )); then
    SU=$(cat ${TMP}/set-name-superuser$$)
    sed -i "s/${UID_NAME}/${SU}/g" /mnt/etc/passwd
    sed -i "s/${GID_NAME}/${SU}/g" /mnt/etc/group
    UID_NAME=$SU
    unset -v SU
    rm -f /mnt/etc/shadow* /mnt/etc/gshadow*
    chroot /mnt /sbin/shadowconfig on > /dev/null 2>&1
    # Creamos el $HOME:
    rmdir /mnt/${THE_HOME} &>/dev/null || :
    mkdir -p -m 0750 /mnt/${UID_NAME}
  fi
fi

if [[ -r /mnt/etc/shadow ]]; then
  while [[ $(grep -m1 "^${UID_NAME}:" /mnt/etc/shadow | cut -f2 -d:) = x ]]; do
    dialog \
     --backtitle "Setting the password" \
     --title "PASSWORD FOR (${UID_NAME}) NOT DETECTED" \
     --yesno \
"You have not set up a password for the system's administrator. \
We suggest setting one up, would you like to do it now?" 6 66
    if (( $? == 0 )); then
      echo
      chroot /mnt /usr/bin/passwd $UID_NAME
      echo
      read -p "Press ENTER to continue..."
    else
      break;
    fi
  done
fi

chroot /mnt /sbin/shadowconfig off >/dev/null 2>&1
chroot /mnt /sbin/shadowconfig on  >${OUTPUT_TTY} 2>&1

