#!/bin/sh
#Usage
#Debian:
# cd /tmp/; \
# wget  http://{{OsImage.Server}}/osimages/pefiles/initpe.sh; \
# chmod +x initpe.sh; \
# ./initpe.sh {{OsImage.Server}} {{OsDeployment.Id}} {{Computer.Id}}; \
# wget http://{{OsImage.Server}}/osimages/pefiles/errpe.sh ; \
# chmod +x errpe.sh; \
# ./errpe.sh {{OsImage.Server}} {{OsDeployment.Id}} {{Computer.Id}} &
# d-i preseed/late_command string \
# cd /tmp/; \
# wget http://{{OsImage.Server}}/osimages/pefiles/fipe.sh ; \
# chmod +x fipe.sh; \
# ./fipe.sh {{OsImage.Server}} {{OsDeployment.Id}} {{Computer.Id}}
############ variables  ###########
httpserver=${1}
deploymentid=${2}
minionid=${3}
mqserver="10.21.66.5"
saltsrv="10.21.66.5"
ipxeserver="10.21.66.5:8081"
vhost="/"
exchange="os-deployment-result"
routingkey="os-deployment-result"
pelog="/tmp/peclient.log"
################################
cd /tmp/
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
#############  workaround for wrong responce file  #########
if test -z "$minionid"; then	
	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
################################################################

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

#############  minion installation #############
echo "Started minion pre setup" >>$pelog
[ -d "/target" ] && target='/target'
if [ -f $target/usr/lib/os-release ]; then
	release=$target/usr/lib/os-release
else
	release=$target/etc/os-release
fi
os_release_out=$(cat $release)
arch_out=$(uname -m)
platform_root="http://$httpserver:5002/minion/1/linux"
linux_config_file="$platform_root/linux.conf"
common_files="$platform_root/packages/common"
wget $linux_config_file 2>>$pelog || curl $linux_config_file -o linux.conf 2>>$pelog
echo "Identify Linux type and version" >>$pelog
os_id=$(echo "$os_release_out" | sed -n 's/^ID=\(.*\)$/\1/p' | sed -e 's/^"//' -e 's/"$//')
version_id=$(echo "$os_release_out" | sed -n 's/^VERSION_ID=\(.*\)$/\1/p' | sed -e 's/^"//' -e 's/"$//')
echo "Detected OS: [$arch_out]:[$os_id]:[$version_id]" >>$pelog
versioned_script_path=$(cat linux.conf | sed -n "s/^$arch_out:$os_id:$version_id='\(.*\)'$/\1/p")
if [ -z "$versioned_script_path" ]; then
	echo "The version [$version_id] of OS [$os_id] with [$arch_out] architecture is not supported yet, trying to find common OS script" >>$pelog
	common_script_path=$(cat linux.conf | sed -n "s/^$arch_out:$os_id:common='\(.*\)'$/\1/p")
	if [ -z "$common_script_path" ]; then
		echo "The OS [$os_id] with [$arch_out] architecture is not supported yet, please contact support team" >>$pelog
	else
		os_script_path="$common_script_path"
	fi
else
	os_script_path="$versioned_script_path"
fi

bootstrap_script="$platform_root/packages/$os_script_path/bootstrap.sh"
package_archive="$platform_root/packages/$os_script_path/pkg.tar.gz"
echo "$bootstrap_script" >>$pelog
src_dir="$target/opt/colibri/minion_src"
installdir="/opt/colibri/minion_push"
[ ! -d "$src_dir" ] && mkdir -p $src_dir
wget -P $src_dir/ "$bootstrap_script"
wget -P $src_dir/ "$package_archive"
wget -nH --no-parent --cut-dirs 4 -r "$common_files"/ -P $src_dir/

chmod +x $src_dir/bootstrap.sh
chmod +x $src_dir/common/chaser/chaser.sh
chmod +x $src_dir/common/rmq/rmq_client*
echo "@reboot root /opt/colibri/minion_src/bootstrap.sh $saltsrv $minionid '' $installdir && rm /etc/cron.d/minstartup" >$target/etc/cron.d/minstartup
####################################################

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\"
}
"
#rename machine
# if test -z "$hostname"; then
# 	echo PC$(cat /proc/sys/kernel/random/uuid | md5sum | head -c 6) >/target/etc/hostname
# fi
echo "Message to Colibri:" >>$pelog
echo $msg_finish >>$pelog
echo "Executing rmqclient -h $mqserver -v $vhost -e $exchange -r $routingkey $msg_finish" >>$pelog
chmod +x rmqclient
./rmqclient -h $mqserver -v $vhost -e $exchange -r $routingkey "$msg_finish" 2>>$pelog
cp $pelog $target/var/log/
#rm rmqclient
