#!/bin/sh
#Usage:
####Debian:
# d-i preseed/early_command string \
# cd /tmp/ && wget  http://$dpserver/osimages/pefiles/initpe.sh && chmod +x initpe.sh; \
# ./initpe.sh $dpserver $deplid $minionid; \
# wget http://$dpserver/osimages/pefiles/errpe.sh && chmod +x errpe.sh; \
# ./errpe.sh $dpserver $deplid $minionid &
# d-i preseed/late_command string \
# cd /tmp/ && wget http://$dpserver/osimages/pefiles/fipe.sh && chmod +x fipe.sh; \
# ./fipe.sh $dpserver $deplid $minionid; \

cd /tmp/
pelog="/tmp/peclient.log"
if [ -f "/tmp/postinstall.log" ]; then
    pelog="/tmp/postinstall.log"
fi

echo "Started error detection script" >>$pelog
if [ -f /tmp/anaconda.log ]; then
    #Anaconda installer, waiting for error
    echo "Anaconda installer, reading error in anaconda.log" >>$pelog
    zone=$(date +%z | sed 's/./&:/3');datetime=$(date +%Y-%m-%dT%H:%M:%S.000000); dateofstart=$datetime$zone
    echo "Error detected at $dateofstart" >>$pelog
    statusDesc=$(grep "stopped" /tmp/anaconda.log | sed -e 's/\"//g')
    echo $statusDesc >>$pelog
elif [ -f "/tmp/pkg-install.log" ]; then
    echo "Altlinux installer detected, waiting for error in pkg-install.log" >>$pelog
    if grep FAILED -q "/tmp/pkg-install.log"; then
       zone=$(date +%z | sed 's/./&:/3');datetime=$(date +%Y-%m-%dT%H:%M:%S.000000); dateofstart=$datetime$zone
        echo "Error detected at $dateofstart" >>$pelog
        statusDesc=$(grep FAILED  "/tmp/pkg-install.log" | sed -e 's/\"//g')
        echo $statusDesc >>$pelog
    fi
else
    echo "Other installer, waiting for error in syslog" >>$pelog
    while ! grep "error code 1" /var/log/syslog >/dev/null; do
        sleep 5
    done
   zone=$(date +%z | sed 's/./&:/3');datetime=$(date +%Y-%m-%dT%H:%M:%S.000000); dateofstart=$datetime$zone
    echo "Error detected at $dateofstart" >>$pelog
    statusDesc=$(grep "error code 1" /var/log/syslog | sed -e 's/\"//g' | sed -e 's/://g' | tr -d '\n' | tr -d '\r')
    echo $statusDesc >>$pelog
fi

interface=$(ip route show default | grep default | sed -n '1p' | cut -d " " -f5)
macaddr=$(cat /sys/class/net/$interface/address)
macaddr=$(echo $macaddr | sed -e 's/:/-/g')
echo $interface >>$pelog
echo $macaddr >>$pelog
echo "Started send start message script" >>$pelog
httpserver=${1}
deploymentid=${2}
minionid=${3}
mqserver="10.21.66.5"
if test -z "$minionid"; then
	ipxeserver="10.21.66.5:8081"
	wget http://${ipxeserver}/osdeployment/ipxe/$macaddr.ipxe -O config.ipxe 2>>$pelog || curl http://${ipxeserver}/osdeployment/ipxe/$macaddr.ipxe -o config.ipxe 2>>$pelog
	minionid=$(cat config.ipxe | grep minionid -m 1  | sed -nE 's/#minionid=(.*)/\1/p')
	deploymentid=$(cat config.ipxe | grep deploymentid -m 1  | sed -nE 's/#deploymentid=(.*)/\1/p')
	httpserver=$(cat config.ipxe | grep kernel -m 1  | sed -nE 's/kernel http:\/\/(.*)\/osimages.*/\1/p')		
fi
vhost="/"
exchange="os-deployment-result"
routingkey="os-deployment-result"

if [ ! -f rmqclient ]; then
	wget http://${httpserver}/osimages/pefiles/rmqclient 2>>$pelog || curl http://${httpserver}/osimages/pefiles/rmqclient -o rmqclient 2>>$pelog
fi

msg_error="{
	\"minionid\": $minionid,
	\"deploymentid\": $deploymentid,
	\"status\": \"Error\",
	\"statusDesc\": \"$statusDesc\",
	\"invokedOn\": \"$dateofstart\"
}
"
msg=$msg_error
zone=$(date +%z | sed 's/./&:/3');datetime=$(date +%Y-%m-%dT%H:%M:%S.000000); dateofstart=$datetime$zone
msg_finish="{
	\"minionid\": $minionid,
	\"deploymentid\": $deploymentid ,
	\"status\": \"Success\",
	\"statusDesc\": \"Deployment finished\",
	\"invokedOn\": \"$dateofstart\"
}
"
if test -z "$statusDesc"; then
    msg=$msg_finish
fi

echo "Message to Colibri:" >>$pelog
echo $msg >>$pelog
echo "Executing rmqclient -h $mqserver -v $vhost -e $exchange -r $routingkey $msg" >>$pelog
chmod +x rmqclient
./rmqclient -h $mqserver -v $vhost -e $exchange -r $routingkey "$msg" 2>>$pelog
#rm rmqclient
