#!/bin/bash

###################################################################
# initialization script for new vms
#
# author: Michael Kraus <michael.kraus@dfki.de>
# usage: bash <(wget -qO- init.comem.ai)
#
# meant for usage with debian or ubuntu.
#
# this script installs some system utilities and configures
# the working environment to be similar across all machines.
#
# a custom .bashrc is installed which includes aliases and shell coloring
# based on the hostname of the system.
#
##################################################################


USER=$(whoami)
USER_HOME=~

cat <<ECHO

#################
init.comem.ai
#################

ECHO

cat <<ECHO

#################
1. acquiring root
#################

ECHO

cat <<ECHO

#################
   checking for sudo
#################

ECHO

sudo echo
if [ $? -eq 127 ] # not installed
then
        echo "   sudo not installed."
        if [[ $EUID -ne 0 ]]; then
                echo "   Error. Run this script as root, or install sudo manually, then try again"
                exit 1
        else
                echo "   Installing sudo"
                apt install sudo -y
                sudo echo
                if [ $? -ne 0 ] # still not working, aborting
                then
                        echo "Cannot aquire sudo (retval=$?). Exiting"
                        exit 1
                fi
        fi
fi

sudo -s <<SUDO

cat <<ECHO

#################
2. ensuring locale
#################

ECHO

sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' /etc/locale.gen
locale-gen

cat <<ECHO

#################
3. installing software
#################

ECHO

cat <<ECHO

#################
   apt update && apt dist-upgrade -y
#################

ECHO

apt update && apt dist-upgrade -y

cat <<ECHO

#################
   git, tmux, glances
#################

ECHO

DEBIAN_FRONTEND=noninteractive apt install git tmux glances -y
cat <<ECHO

#################
4. configuration
#################

ECHO

mkdir $USER_HOME/.comem
wget https://git.opendfki.de/snippets/16/raw -O $USER_HOME/.comem/.bashrc
chown -R $USER $USER_HOME/.comem
chmod -R 700 $USER_HOME/.comem

cat << 'EOF' >> $USER_HOME/.bashrc

if [ -f ~/.comem/.bashrc ]; then
      . ~/.comem/.bashrc
fi

EOF

wget https://git.opendfki.de/snippets/17/raw -O $USER_HOME/.tmux.conf
chown $USER $USER_HOME/.tmux.conf
chmod 600 $USER_HOME/.tmux.conf

SUDO

cat <<ECHO

#################
5. starting tmux
#################

ECHO

tmux a || tmux new
