#!/bin/bash
source /.profile
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
TRIES=5
while [ ! -e /dev/disk/by-label ] && [ $TRIES -gt 0 ]; do
    sleep 2
    TRIES=$((TRIES - 1))
done
idntdir=""
if [ -e /dev/disk/by-label/CNFLNT_IDNT ]; then
    tmnt=$(mktemp -d)
    mount /dev/disk/by-label/CNFLNT_IDNT $tmnt
    if [ ! -e $tmnt/cnflnt.yml ]; then
        echo "No cnflnt.yml found on CNFLNT_IDNT partition, ignoring"
    else
        idntdir=$(mktemp -d)
        /usr/bin/cp $tmnt/cnflnt.* $idntdir/
    fi
    umount $tmnt
    rmdir $tmnt
fi
if [ ! -z "$idntdir" ]; then
    tmpcfg=$(mktemp)
    echo "Using confluent identity image configuration"
    deploysrvs=$(sed -n '/^deploy_servers:/, /^[^-]/p' $idntdir/cnflnt.yml |grep ^-|sed -e 's/^- //'|grep -v :)
    nodename=$(grep ^nodename: $idntdir/cnflnt.yml|awk '{print $2}')
    sed -n '/^net_cfgs:/, /^[^- ]/{/^[^- ]/!p}' $idntdir/cnflnt.yml |sed -n '/^-/, /^-/{/^-/!p}'| sed -e 's/^[- ]*//'> $tmpcfg
    autoconfigmethod=$(grep ^ipv4_method: $tmpcfg)
    autoconfigmethod=${autoconfigmethod#ipv4_method: }
    allnics=""
    for i in /sys/class/net/*; do
        ip link set $(basename $i) down
        udevadm info $i | grep ID_NET_DRIVER=cdc_ether > /dev/null &&  continue
        ip link set $(basename $i) up
        ip link show dev $(basename $i) | grep -q "LOOPBACK" && continue
        allnics="$allnics $(basename $i)"
    done
    TRIES=30
    DETECTED=0
    while [ "$DETECTED" = 0 ] && [ "$TRIES" -gt 0 ]; do
        if [ "$autoconfigmethod" == "dhcp" ]; then
             for nic in $allnics; do
                dhcpcd $nic --timeout=10
                for srv in $deploysrvs; do
                    curl -m 10 -H "CONFLUENT_IDS: " https://$srv/confluent-api/self/whoami >/dev/null 2>&1
                    curlret=$?
                    if [ $curlret -eq 0 ] || [ $curlret -eq 22 ]; then
                        DETECTED=1
                        break
                    fi
                done
                if [ "$DETECTED" = 1 ]; then
                    break
                fi
                dhcpcd -k $nic
            done
        elif [ "$autoconfigmethod" == "static" ]; then
            ipv4addr=$(grep ^ipv4_address: $tmpcfg| awk '{print $2}')
            for nic in $allnics; do
                ip addr add $ipv4addr dev $nic
                ip link set $nic up
                for srv in $deploysrvs; do
                    curl -m 10 -H "CONFLUENT_IDS: " https://$srv/confluent-api/self/whoami >/dev/null 2>&1
                    curlret=$?
                    if [ $curlret -eq 0 ] || [ $curlret -eq 22 ]; then
                        DETECTED=1
                        break
                    fi
                done
                if [ "$DETECTED" = 1 ]; then
                    break
                fi
            done
        fi
        if [ "$DETECTED" = 0 ]; then
             TRIES=$((TRIES - 1))
             sleep 2
        fi
    done
	if [ "$DETECTED" = 1 ]; then
		echo "Confluent identity image configuration detected"
		echo NODENAME: $nodename > /etc/confluent/confluent.info
		echo MANAGER: $srv >> /etc/confluent/confluent.info
		bootifidx=$(cat /sys/class/net/$nic/ifindex)
		bootif=$(basename $nic)
		hmackeyfile=/tmp/cnflnthmackeytmp
		echo -n $(grep ^apitoken: $idntdir/cnflnt.yml|awk '{print $2}') > $hmackeyfile
		passfile=/tmp/cnflnttmppassfile
	    passcrypt=/tmp/cnflntcryptfile
    	hmacfile=/tmp/cnflnthmacfile
    	ln -s /opt/confluent/bin/clortho /opt/confluent/bin/genpasshmac
		/opt/confluent/bin/genpasshmac $passfile $passcrypt $hmacfile $hmackeyfile
		if curl --capath /tls/ -f -H "CONFLUENT_NODENAME: $nodename" -H "CONFLUENT_CRYPTHMAC: $(cat $hmacfile)" -d@$passcrypt https://$srv/confluent-api/self/registerapikey; then
		    cp $passfile /etc/confluent/confluent.apikey
		fi

	fi
else
	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)"
	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
	/opt/confluent/bin/clortho $nodename $mgr > /etc/confluent/confluent.apikey
fi
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..."
if [ -z "$bootifidx" ]; then
	bootifidx=${mgr#*%}
fi
for nic in *; do
	if [ "$(cat $nic/ifindex)" = "$bootifidx" ]; then
	bootif=$nic
fi
done
cd -
echo "NetDevice: $bootif" >> /etc/linuxrc.d/01-confluent
if [[ "$mgr" == *":"* ]]; then
    mgr="[$mgr]"
fi
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
if [ ! -z "$autocons" ] && grep textconsole: true /etc/confluent/confluent.deploycfg > /dev/null; then /opt/confluent/bin/autocons -c > /dev/null; 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 kernelargs 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
    echo "Textmode: 1" >> /etc/linuxrc.d/01-confluent
fi

tz=$(grep timezone: /etc/confluent/confluent.deploycfg | awk '{print $2}')
echo "<timezone>${tz}</timezone>" > /tmp/timezone
echo "${tz}" > /tmp/timezone.txt
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 grep console= /etc/fakecmdline >& /dev/null &&  [[ "$append" != *console=* ]]; then
    append="$append console=${autocons#*/dev/}"
fi
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
