#!/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 "${changeuser}"
}

x_or_no(){
  if [ ! -x "/etc/init.d/xdm" ] && [ ! -x "/etc/init.d/display-manager" ]; then
    return 1
  fi
  printf "Would you like to securely start X now?\n"
  printf "[y/n] "
  read -r choose_x
  case ${choose_x} in
    y|n) printf "%s\n" "${choose_x}";;
    *) printf "only y or n accepted\n"
      x_or_no
      ;;
  esac
}

stupid_or_no(){
  printf "You are using an x86/i686 iso but your processor supports amd64\n"
  printf "It is STRONGLY recommended to stop now and switch to the amd64 iso.\n"
  printf "Are you absolutely certain you want to use your 64bit CPU in 32 bit mode???\n"
  printf "[y/n]"
  read -r choose_stupid
  case ${choose_stupid} in
    y|n) print "%s\n" "${choose_stupid}";;
    *) printf "only y or n accepted\n"
      stupid_or_no
      ;;
  esac
}

if [ "$(uname -m)" = "i686" ]; then
  if grep -q -w 'lm' /proc/cpuinfo; then
    stupid_or_no
    if [ "${choose_stupid}" = "n" ]; then
      sudo poweroff
    else
      printf "This is probably unwise, but it's your choice\n"
      printf "Continuing using 32 bit on 64bit CPU at request of the user...\n\n"
    fi
  fi
fi

changeuser="${USER}"

if sudo grep -q "${changeuser}:!" /etc/shadow; then
  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 \"%s\"\n" "${changeuser}"

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 ${changeuser}
  printf "Username is \"%s\", password is whatever you set at boot.\n" "${changeuser}" | 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
else
  printf "Something went wrong, aborting password set.  Please report this issue\n"
fi

#prep X to run no matter what
sudo /etc/init.d/display-manager-setup start > /dev/null 2>&1
if [ -f "/run/.nogui" ]; then
  # /etc/init.d/display-manager-setup runs 'touch /run/.nogui' now
  sudo rm -f "/run/.nogui"
elif [ -f "/etc/.noxdm" ]; then
  # but it used to touch /etc/.noxdm
  sudo rm -f "/etc/.noxdm"
fi

RAMSIZE=$(awk '/MemTotal/ {printf( "%.0f\n", int ( $2 / 1024 ) + 1)}' /proc/meminfo)
if [ "${RAMSIZE}" -gt "700" ]; then
  x_or_no
else
  printf "Your system doesn't have enough ram to run X properly.  Please increase the amount of ram if possible.\n"
  choose_x="n"
fi
if [ "${choose_x}" = "y" ]; then
  printf "Remember to log in with user \"%s\" and the password you just set\n" "${changeuser}"
  printf "*Press any key to start X securely.*\n"
  read -r
  if [ -x "/etc/init.d/display-manager" ]; then
    sudo /etc/init.d/display-manager restart && exit 0
  fi
else
  exec /bin/bash -l
fi
