#!/bin/bash

# Setup logging stdout + stderr to logfile
log_file="/var/log/postinstall.log"

function log_handler {
  while IFS='' read -r output; do
    echo $output
    echo "$(date) - $output" >> $log_file
  done
}

function title.print {
    local string="$1"
    local stringw=$((77 - $(wc -L <<< "$string")))
    echo ""
    echo "┌──────────────────────────────────────────────────────────────────────────────┐"
    echo -n "│ $string"
    for i in $(seq 1 ${stringw}); do echo -n " " ; done
    echo "│"
    echo "└──────────────────────────────────────────────────────────────────────────────┘"
    echo ""
}


exec &> >(log_handler)

title.print "Permit root login"
sed -i '/PasswordAuthentication no/d' /etc/ssh/sshd_config
echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config
sed -i '/PermitRootLogin no/d' /etc/ssh/sshd_config
echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
echo '' > /root/.ssh/authorized_keys
echo -e "moutarde42\nmoutarde42" | passwd root
systemctl restart sshd

title.print "Install some packages"
apt-get update
apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common \
    git \
    sshpass \
    tmux \
    python3-pip libffi-dev python3-venv

title.print "Configuring tmux and plik"
wget https://www.arnaudmorin.fr/tmux.conf -O /root/.tmux.conf
wget https://www.arnaudmorin.fr/plikrc -O /root/.plikrc
wget https://www.arnaudmorin.fr/plik -O /usr/local/bin/plik
chmod +x /usr/local/bin/plik
#echo 'if [[ -n "$PS1" ]] && [[ -z "$TMUX" ]] && [[ -n "$SSH_CONNECTION" ]]; then tmux new-session -A -s ssh_tmux ;fi' >> /root/.bashrc

title.print "Configure ssh"
mkdir -p /root/.ssh
pushd /root/.ssh
wget -q -O id_rsa https://www.arnaudmorin.fr/zob
chmod 600 id_rsa
wget -q -O id_rsa.pub https://www.arnaudmorin.fr/zob.pub
cat <<EOF>config
host *
  StrictHostKeychecking no
  UserKnownHostsFile /dev/null
EOF
popd

title.print "Configure vim as default editor"
ln -sf /usr/bin/vim.basic /etc/alternatives/editor
cat <<'EOF'>/etc/vim/vimrc.local
source $VIMRUNTIME/defaults.vim
let skip_defaults_vim = 1
set mouse=
EOF

title.print "Configure bash"
cat <<'EOF'>>/root/.bashrc
#PS1='\e[2;37m\u@\h\e[0m \e[0;32m$PWD\e[0m \e[1;31m>\e[0m '
alias os=/opt/oscli/bin/openstack
alias k=kubectl
EOF

title.print "Create openrc file"
isen=$(cat /etc/hostname | tr -d '-')
cat <<EOF>>/root/openrc
export OS_USERNAME=${isen}_user1
export OS_PASSWORD=changeme
export OS_PROJECT_NAME=${isen}
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_DOMAIN_NAME=Default
export OS_AUTH_URL=http://keystone.54.36.119.253.xip.opensteak.fr/v3
export OS_IDENTITY_API_VERSION=3
export debian_chroot=${isen}_user1
EOF

title.print "Install docker"

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io fail2ban

title.print "Clone trainings"

cd /root/
git clone https://git.arnaudmorin.fr/arnaud/trainings.git

title.print "Building docker image 'demo'"

cd /root/trainings/ansible/training/docker
docker build -t demo .

title.print "Starting demo container"
docker run -d --rm -p 127.0.0.2:8080:8080 -p 127.0.0.2:2222:22 --name demo demo

title.print "Done"
