#!/bin/bash
# A script that sets up a basic Ubuntu environment (non-graphical)
# version 0.2 - schlueterville@pm.me
#=====================================================
function ASK {
    echo; read -p "LLE Setup Script: $1 [y|n]? " var
    [ "$var" == "y" ] && return 1 ; return 0
}
#=====================================================


ASK "configure timezone" || {
dpkg-reconfigure tzdata 
}


ASK "install root ssh key" || {
curl apt.schlutech.com/temp/authorized_keys >> /root/.ssh/authorized_keys
}


ASK "colorize root's bash prompt" || {
echo "
Red='\[\e[1;31m\]'
White='\[\e[00m\]'
Green='\[\e[1;32m\]'
Blue='\[\e[01;34m\]'" > /root/.bash_prompt
echo '
PS1="${Red}\u${White}@${Green}\h${White}:${Blue}\w${White}\$ "' >> /root/.bash_prompt
echo '. ~/.bash_prompt' >> /root/.bashrc
touch /root/.hushlogin 
}


ASK "update grub" || {
line='GRUB_CMDLINE_LINUX_DEFAULT'
cmdline='ipv6.disable=1 net.ifnames=0 fsck.mode=force fsck.repair=yes'
sed -i "s/$line.*/$line=\"$cmdline\"/g" /etc/default/grub
update-grub
}


ASK "update respositories" || {
apt-get update
}


ASK "remove unwanted software" || {
apt-get --purge autoremove                                                    \
  bcache-tools btrfs-progs byobu                                              \
  cloud-*                                                                     \
  dirmngr                                                                     \
  ed                                                                          \
  firmware-sof-signed friendly-recovery ftp                                   \
  git git-man                                                                 \
  mdadm motd-news-config multipath-tools modemmanager                         \
  netplan.io networkd-dispatcher                                              \
  open-iscsi overlayroot                                                      \
  packagekit pollinate publicsuffix                                           \
  snapd sosreport systemd-timesyncd                                           \
  telnet tmux tnftp thin-provisioning-tools                                   \
  ufw update-notifier-common update-manager-core ubuntu-release-upgrader-core \
  usbmuxd usrmerge                                                            \
  vim-tiny                                                                    \
  xfsprogs                                                                    \
  zerofree
}


ASK "install wanted administrative software" || {
apt-get install                                 \
  bash-completion bind9-utils bmon              \
  chrony                                        \
  dns-root-data                                 \
  fwupd                                         \
  gdebi-core                                    \
  htop                                          \
  iftop ifupdown iotop iptables-persistent      \
  iputils-arping iputils-ping iputils-tracepath \
  ncdu net-tools nmon ntpdate                   \
  opensc opensc-pkcs11                          \
  p7zip-full pcsc-tools pcscd                   \
  squashfs-tools                                \
  tpm2-tools                                    \
  vim
}


ASK "configure lle.rochester.edu domain time servers" || {
echo '
server sierra.lle.rochester.edu prefer iburst
server sioux.lle.rochester.edu  prefer iburst

stratumweight 0' > /etc/chrony/conf.d/00_lle.conf
}


ASK "update installed packages" || {
apt-get dist-upgrade
}


ASK "disable unwanted services" || {
list='systemd-networkd.service
systemd-networkd.socket
systemd-networkd-wait-online.service
systemd-network-generator
systemd-resolved'

for service in $list
  do
    systemctl stop $service
    systemctl disable $service
    systemctl mask $service
done
}


ASK "configure basic dhcp on eth0" || {
echo '
auto eth0
  iface eth0 inet dhcp
' > /etc/network/interfaces
}


ASK "set google (8.8.8.8) as the resolver" || {
rm -rf /etc/resolv.conf
echo 'nameserver 8.8.8.8' > /etc/resolv.conf
}


