#!/bin/bash
echo "Installing certificates"
echo '<authorized_keys xmlns="http://www.suse.com/1.0/yast2ns" xmlns:config="http://www.suse.com/1.0/configns" config:type="list">' > /tmp/rootkeys.xml
for pub in /ssh/*pubkey; do
    echo '<listentry>'$(cat $pub)'</listentry>' >> /tmp/rootkeys.xml
done
echo '</authorized_keys>' >> /tmp/rootkeys.xml
/usr/bin/cp /tls/*.0 /var/lib/ca-certificates/openssl/
/usr/bin/cp /tls/*.0 /etc/ssl/certs/
echo "LineMode: 1" > /etc/linuxrc.d/01-confluent
autocons=""
if ! grep console /proc/cmdline > /dev/null; then
	autocons=$(/opt/confluent/bin/autocons)
	if [ ! -z "$autocons" ]; then
		echo "Serial console detected from firmware: $autocons" > ${autocons%,*}
	fi
fi
mkdir -p /etc/confluent
chmod 700 /etc/confluent
cd /sys/class/net
echo -n "Discovering confluent..."
touch /etc/confluent/confluent.info
TRIES=0
while ! awk -F'|' '{print $3}' /etc/confluent/confluent.info |grep 1 >& /dev/null && [ "$TRIES" -lt 60 ]; do
	TRIES=$((TRIES + 1))
	for nic in *; do
		ip link set $nic up
	done
	/opt/confluent/bin/copernicus -t > /etc/confluent/confluent.info
done
nodename=$(grep ^NODENAME: /etc/confluent/confluent.info | head -n 1 | sed -e 's/NODENAME: //')
echo "done ($nodename)"
echo "Hostname: $nodename" >> /etc/linuxrc.d/01-confluent
mgr=$(grep ^EXTMGRINFO: /etc/confluent/confluent.info |awk -F'|' '{print $1 " " $3}'|grep " 1$" | head -n 1 | awk '{print $2}')
if [ -z "$mgr" ]; then
    mgr=$(grep ^MANAGER: /etc/confluent/confluent.info | head -n 1 | sed -e 's/MANAGER: //')
fi
mgtiface=$(grep $mgr /etc/confluent/confluent.info | grep EXTMGRINFO: | head -n 1 | awk -F'|' '{print $2}')
echo -n "Acquiring configuration from $mgr..."
bootifidx=${mgr#*%}
for nic in *; do
	if [ "$(cat $nic/ifindex)" = "$bootifidx" ]; then
		bootif=$nic
	fi
done
cd -
echo "NetDevice: $bootif" >> /etc/linuxrc.d/01-confluent
/opt/confluent/bin/clortho $nodename $mgr > /etc/confluent/confluent.apikey
mgr="[$mgr]"
if [ -z "$mgtiface" ]; then
    curl -H "CONFLUENT_NODENAME: $nodename" -H "CONFLUENT_APIKEY: $(cat /etc/confluent/confluent.apikey)" https://$mgr/confluent-api/self/deploycfg > /etc/confluent/confluent.deploycfg
else
    curl -H "CONFLUENT_MGTIFACE: $mgtiface" -H "CONFLUENT_NODENAME: $nodename" -H "CONFLUENT_APIKEY: $(cat /etc/confluent/confluent.apikey)" https://$mgr/confluent-api/self/deploycfg > /etc/confluent/confluent.deploycfg
fi
profilename=$(grep ^profile: /etc/confluent/confluent.deploycfg)
profilename=${profilename#profile: }
curl https://$mgr/confluent-public/os/$profilename/profile.yaml > /tmp/profile.yaml
dnsdomain=$(grep ^dnsdomain: /etc/confluent/confluent.deploycfg)
dnsdomain=${dnsdomain#dnsdomain: }
if [ ! -z "$dnsdomain" ] && [ "$dnsdomain" != "null" ]; then
	echo "Domain: $dnsdomain" >> /etc/linuxrc.d/01-confluent
fi
textconsole=$(grep ^textconsole: /etc/confluent/confluent.deploycfg)
textconsole=${textconsole#textconsole: }
if [ "$textconsole" = "true" ] && ! grep console= /proc/cmdline > /dev/null && [ ! -z "$autocons" ]; then
	echo "Serial console has been autodected and enabled read-only for install" > ${autocons%,*}
	echo "It will be read-write after install" > ${autocons%,*}
	echo "If a fully functional console is desired over serial, add console=${autocons#/dev/} " > ${autocons%,*}
	echo "to kerneralgs in the profile.yaml file of the profile and run 'osdeploy updateboot <profile>" > ${autocons%,*}
	#echo "Serial console autodetected and enabled, will be available upon install completion" > ${autocons%,*}
	#echo "The installer will run in text mode on the graphics console. When ssh is available," > ${autocons%,*}
	#echo "install progress may be checked by using ssh to access and run the screendump command" > ${autocons%,*}
	#echo "Install time serial console would require the profile to have console=${autocons#/dev/}" > ${autocons%,*}
	#echo "specified in the kernel command line arguments" > ${autocons%,*}
	echo ${autocons%,*} > /tmp/autoconsdev
	sed -e s'/$/ 'console=${autocons#*/dev/}/ /proc/cmdline > /etc/fakecmdline
	mount -o bind /etc/fakecmdline /proc/cmdline
	echo "ConsoleDevice: ${autocons%,*}" >> /etc/linuxrc.d/01-confluent
fi

tz=$(grep timezone: /etc/confluent/confluent.deploycfg | awk '{print $2}')
echo "<timezone>${tz}</timezone>" > /tmp/timezone
autoconfigmethod=$(grep ipv4_method /etc/confluent/confluent.deploycfg)
autoconfigmethod=${autoconfigmethod#ipv4_method: }
if [ "$autoconfigmethod" = "dhcp" ]; then
	echo "DHCP: 1" >> /etc/linuxrc.d/01-confluent
else
	v4addr=$(grep ^ipv4_address: /etc/confluent/confluent.deploycfg)
	v4addr=${v4addr#ipv4_address: }
	v4gw=$(grep ^ipv4_gateway: /etc/confluent/confluent.deploycfg)
	v4gw=${v4gw#ipv4_gateway: }
	v4nm=$(grep ipv4_netmask: /etc/confluent/confluent.deploycfg)
	v4nm=${v4nm#ipv4_netmask: }
	echo "HostIP: $v4addr" >> /etc/linuxrc.d/01-confluent
	echo "Netmask: $v4nm" >> /etc/linuxrc.d/01-confluent
	if [ "$v4gw" != "null" ]; then
		echo "Gateway: $v4gw" >> /etc/linuxrc.d/01-confluent
	fi
	nameserversec=0
	while read -r entry; do
		if [ $nameserversec = 1 ]; then
			if [[ $entry == "-"* ]]; then
				echo Nameserver: ${entry#- } >> /etc/linuxrc.d/01-confluent
				continue
			fi
		fi
		nameserversec=0
		if [ "${entry%:*}" = "nameservers" ]; then
			nameserversec=1
			continue
		fi
	done < /etc/confluent/confluent.deploycfg
fi
echo done
mgr=$(grep ^deploy_server: /etc/confluent/confluent.deploycfg)
mgr=${mgr#deploy_server: }
proto=$(grep ^protocol: /etc/confluent/confluent.deploycfg)
proto=${proto#protocol: }

append=$(grep ^installedargs: /tmp/profile.yaml | sed -e 's/^installedargs: //' -e 's/#.*//')
if [ -z "$append" ]; then
    echo "<bootloader/>" > /tmp/bootloader.xml
else
    echo "<bootloader><global><append>$append</append></global></bootloader>" > /tmp/bootloader.xml
fi

echo "<media_url>${proto}://${mgr}/confluent-public/os/${profilename}/product</media_url>" > /tmp/pkgurl

echo "AutoYaST: $proto://$mgr/confluent-public/os/$profilename/autoyast" >> /etc/linuxrc.d/01-confluent
echo "Install: $proto://$mgr/confluent-public/os/$profilename/distribution/1" >> /etc/linuxrc.d/01-confluent
exec /init
