#!/bin/bash
set -euf -o pipefail

# CIP Hosts
# (online 24/7 according to https://wwwcip.cs.fau.de/documentation/services.en.html )
CIPHOSTS=( \
		"cip2a0.cip.cs.fau.de" "cip2a1.cip.cs.fau.de" "cip2a2.cip.cs.fau.de" "cip2a3.cip.cs.fau.de" "cip2a4.cip.cs.fau.de" "cip2a5.cip.cs.fau.de"  "cip2a6.cip.cs.fau.de" "cip2a7.cip.cs.fau.de" \
		"cip2b0.cip.cs.fau.de" "cip2b1.cip.cs.fau.de" "cip2b2.cip.cs.fau.de" \
		"cip2c0.cip.cs.fau.de" "cip2c1.cip.cs.fau.de" "cip2c2.cip.cs.fau.de" \
		"cip2d0.cip.cs.fau.de" "cip2d1.cip.cs.fau.de" "cip2d2.cip.cs.fau.de" \
		"cip2e0.cip.cs.fau.de" "cip2e1.cip.cs.fau.de" "cip2e2.cip.cs.fau.de" \
		"cip2g3.cip.cs.fau.de" "cip2g4.cip.cs.fau.de" "cip2g5.cip.cs.fau.de" "cip2g6.cip.cs.fau.de" "cip2g7.cip.cs.fau.de" \
		"cip1e0.cip.cs.fau.de" "cip1e1.cip.cs.fau.de" "cip1e2.cip.cs.fau.de" "cip1e3.cip.cs.fau.de" "cip1e4.cip.cs.fau.de" "cip1e5.cip.cs.fau.de"  "cip1e6.cip.cs.fau.de" "cip1e7.cip.cs.fau.de" \
	)

# SSH Key
CIPSSHKEY="$HOME/.ssh/i4cip"

# Welcome message
echo -e "\n\e[1;4muInoffizieller i4 CIP SSH Helfer\e[0m\n"

# Check tools
function toolreq(){
	if ! command -v $1 &> /dev/null ; then
		echo -e "\e[31m$1 nicht gefunden!\e[0m"
		echo "Die Anwendung $2 wird gebraucht, ist jedoch nicht installiert!"
		echo
		exit 1
	fi
}

toolreq ssh "SSH"
toolreq ssh-keygen "SSH-Keygen"

# Read CIP user name
echo "Du brauchst einen gueltigen CIP Account."
echo "Registration ist ueber https://account.cip.cs.fau.de/ moeglich"
user=""
while true ; do
	read -p "Bitte gib deinen CIP Benutzernamen ein: " -n 8 user
	echo
	if [[ $user =~ ^[a-z]{2}[0-9]{2}[a-z]{4}|s[a-z0-9]{7}$ ]] ; then
		break
	else
		echo -e "\e[31mUngueltiger Benutzernamen!\e[0m"
	fi
done

# Generate SSH key
mkdir -p $(dirname $CIPSSHKEY)
if [[ ! -f $CIPSSHKEY ]] ; then
	echo -e "\n\e[1mErstelle neuen SSH Schluessel...\e[0m"
	ssh-keygen -f $CIPSSHKEY -N ""
fi

# Check CIP hosts
echo -e "\n\e[1mSuche CIP Zielrechner...\e[0m"
for host in $(shuf -e "${CIPHOSTS[@]}" ); do
	# Online?
	if timeout 11 ping -c1 -W3 $host >/dev/null 2>&1 ; then

		# Copy SSH key
		echo -e "\n\e[1mKopiere SSH Schluessel auf CIP Rechner $host...\e[0m"
		ssh-copy-id -i $CIPSSHKEY -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$host

		# Generate config
		if [[ ! -f "$HOME/.ssh/config" ]] || ! grep -q "Host \*.cip.cs.fau.de" ~/.ssh/config ; then
			echo -e "\n\e[1mGeneriere SSH Konfiguration...\e[0m"
			echo "Host *.cip.cs.fau.de
    User $user
    IdentityFile $CIPSSHKEY
    ConnectTimeout 5
    Protocol 2
    HashKnownHosts no
    UserKnownHostsFile=/dev/null
    StrictHostKeyChecking=no
    TCPKeepAlive yes" >> $HOME/.ssh/config

			# Enable Multiplexing
			read -p "Soll SSH die Verbindung multiplexen (j/n)? " -n 1 -r
			echo
			if [[ $REPLY =~ ^[Jj]$ ]] ; then
				echo -e "\n\e[1mAktiviere multiplexen der Verbindung...\e[0m"
				mkdir -p "$HOME/.ssh/controlmasters/"
				echo "    ControlPath ~/.ssh/controlmasters/%r@%h:%p
    ControlMaster auto
    ControlPersist 10m
    ServerAliveInterval 10" >> $HOME/.ssh/config
			fi
			echo >> $HOME/.ssh/config
		fi

		# Test Settings
		ssh -t $host "figlet \"Es laeuft.\" | lolcat"

		# Exit
		exit 0
	fi
done

# Error
echo -e "\e[31mKein CIP Zielrechner erreichbar - fehlerhafte Internetverbindung?\e[0m"
exit 1
