#!/bin/sh
setpassword(){
  printf "RFC 2119: MUST means an absolute requirement\n"
  printf "You *MUST* set a password to continue. Do not forget your password.\n"
  sudo passwd pentoo
}

changeuser="${USER}"

if sudo grep -q "${changeuser}:!" /etc/shadow; then
  printf "For your security, Pentoo does not come with a default password.\n"
  printf "To protect your session you must set a password now.\n"
  printf "Do not forget your password.\n"
  sudo passwd "${changeuser}"
fi

while sudo grep -q "${changeuser}:!" /etc/shadow; do
  setpassword
done

printf "\nRemember your password, you will need it to log in as user \"${changeuser}\"\n"

if ! sudo grep -q "{changeuser}:!" /etc/shadow; then
  # restore real inittab and tell init about it
  if [ -r /etc/inittab.old ]; then
    sudo mv /etc/inittab.old /etc/inittab
    sudo init q
  fi

  #update /etc/issue to remind users to log in as pentoo
  if [ -r /etc/issue.pentoo.logo ]; then
    sudo cp /etc/issue.pentoo.logo /etc/issue
  else
    sudo cp /etc/issue /etc/issue.old
    printf "Welcome to Pentoo Live!\n" | sudo tee /etc/issue > /dev/null
  fi
  printf "Username is \"pentoo\", password is whatever you set at boot.\n" | sudo tee -a /etc/issue > /dev/null

  #we have set the password, pull livecd-setpass out of bashrc, our work is done
  grep -v livecd-setpass ~/.bashrc > .bashrc_setpass_temp
  mv .bashrc_setpass_temp ~/.bashrc
fi
