set -x

HOSTNAME=$1

export DEBIAN_FRONTEND=noninteractive

sudo apt-get update
sudo apt-get -y install gnupg curl unzip libuser net-tools

# sudo für student ohne PW 
echo "student ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/student

if [[ -d "/vagrant" ]]
then
  USERNAME=student
  PASSWORD=123456

  : "Set hostname" && {
    sudo hostname $HOSTNAME
    echo $HOSTNAME | sudo tee /etc/hostname > /dev/null
    sudo sed -i "s/bullseye/$HOSTNAME/g" /etc/hosts
  }
  : "Create user student" && {
    sudo useradd -m -G sudo,vagrant -s /bin/bash $USERNAME
    echo -e "$PASSWORD\n$PASSWORD" | sudo passwd $USERNAME
  }
  : "Enable SSH password authentication" && {
    sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
    sudo systemctl restart sshd.service
  }
  : "Run install" && {
    sudo -u $USERNAME bash /vagrant/install.sh
  }
else
  bash /vagrant/install.sh
fi