#!/bin/bash


#TODD: DRY Refactor. 
#TODO: Test script on both rock and raspberry pi 

# Check if the script is being run as root
clear
if [ "$EUID" -ne 0 ]; then
  echo "Please run this script as root (with sudo)."
  exit 1
fi

# GLOBALS
readonly BOARD_TYPE=$(sudo cat /sys/firmware/devicetree/base/model | tr -d '\0')
readonly OLD_PUPPET_MASTER=$(sudo puppet config print server)
readonly PUPPET_AGENT=$(hostname)
HAMMER_COMMAND=""
HOST_GROUP_ID=-1
ENVIRONMENT_ID=-1

clear
echo
echo "---------------------------------------------------------------------------" 
echo "Migrate flowbox to another puppet server script"
echo "IMPORTANT! You need an account on the server you migrate from and the server you migrate to"
echo "Hit CTRL+C anytime to exit"
echo
echo "Board type: $BOARD_TYPE"
echo "Current puppet master: $OLD_PUPPET_MASTER"
echo
echo "Select flowbox environment option"
echo "1. development"
echo "2. production"
echo "-----------------------------------------------------"


while true 
do

	read ENVIRONMENT

	if [ "$ENVIRONMENT" == "1" ]; then
		ENVIRONMENT=development
		break
	elif [ "$ENVIRONMENT" == "2" ]; then
		ENVIRONMENT=production
		break
	else
		echo "Invalid option. Try again."
	fi
done


echo "-------------------------------------------------------------"
echo "Choose a new Puppet server. Current puppet server: $OLD_PUPPET_MASTER"
echo "1. puppet03.prod.flowretail.com"
echo "2. puppet02.prod.flowretail.com"
echo "3. puppet02.dev.flowretail.com"
echo "q(uit)"
echo "-------------------------------------------------------------"
read PUPPET_SERVER_OPTION 
echo



function set_values(){

			if [ "$PUPPET_SERVER" == "$OLD_PUPPET_MASTER" ]; then
				echo "Selected puppetmaster $PUPPET_SERVER is the same as current puppetmaster $OLD_PUPPET_MASTER. Try again"
				continue
			fi

			if [ "$BOARD_TYPE" == "Radxa ROCK 4C+" ] && [ "$ENVIRONMENT" == "development" ]; then
				HOST_GROUP_ID=4
				ENVIRONMENT_ID=2
			elif [ "$BOARD_TYPE" == "Radxa ROCK 4C+" ] && [ "$ENVIRONMENT" == "production" ]; then
				HOST_GROUP_ID=3
				ENVIRONMENT_ID=1
			elif [ "$BOARD_TYPE" == "Raspberry Pi 4 Model B Rev 1.5 ROCK 4C+" ] && [ "$ENVIRONMENT" == "development" ]; then
				HOST_GROUP_ID=5
				ENVIRONMENT_ID=2
			elif [ "$BOARD_TYPE" == "Raspberry Pi 4 Model B Rev 1.5 ROCK 4C+" ] && [ "$ENVIRONMENT" == "production" ]; then
				HOST_GROUP_ID=6
				ENVIRONMENT_ID=1
			else
				echo "$BOARD_TYPE $ENVIRONMENT combination is not valid. Aborting"
				exit 1
			fi
			break
			HAMMER_COMMAND="sudo hammer host update --name $PUPPET_AGENT --puppet-environment-id $ENVIRONMENT_ID --hostgroup-id $HOST_GROUP_ID --new-location \"Default Location\" --new-organization \"Default Organization\""
}

while true
do
	case $PUPPET_SERVER_OPTION in
		1)
			PUPPET_SERVER="puppet03.prod.flowretail.com"
			set_values
		;;
		2)
			PUPPET_SERVER="puppet02.prod.flowretail.com"
			set_values


			if [ "$PUPPET_SERVER" == "$OLD_PUPPET_MASTER" ]; then
				echo "Selected puppetmaster $PUPPET_SERVER is the same as current puppetmaster $OLD_PUPPET_MASTER. Try again"
				continue
			fi

			echo "NOTE! PUPPET02.dev has less options. This server will become obsolete in the future, puppet03.prod.flowretail.com will be the new puppet master in production."
			HOST_GROUP_ID=1
			ENVIRONMENT_ID=1
			HAMMER_COMMAND="sudo hammer host update --name $PUPPET_AGENT --puppet-environment-id $ENVIRONMENT_ID --hostgroup-id $HOST_GROUP_ID --new-location \"Default Location\" --new-organization \"Default Organization\""
			break
		;;
		3)
			PUPPET_SERVER="puppet02.dev.flowretail.com"

			if [ "$PUPPET_SERVER" == "$OLD_PUPPET_MASTER" ]; then
				echo "Selected puppetmaster $PUPPET_SERVER is the same as current puppetmaster $OLD_PUPPET_MASTER. Try again"
				continue
			fi

			if [ "$BOARD_TYPE" == "Radxa ROCK 4C+" ] && [ "$ENVIRONMENT" == "development" ]; then
				HOST_GROUP_ID=2
				ENVIRONMENT_ID=4
			elif [ "$BOARD_TYPE" == "Radxa ROCK 4C+" ] && [ "$ENVIRONMENT" == "production" ]; then
				HOST_GROUP_ID=3
				ENVIRONMENT_ID=1
			elif [ "$BOARD_TYPE" == "Raspberry Pi 4 Model B Rev 1.5 ROCK 4C+" ] && [ "$ENVIRONMENT" == "development" ]; then
				HOST_GROUP_ID=1
				ENVIRONMENT_ID=4
			elif [ "$BOARD_TYPE" == "Raspberry Pi 4 Model B Rev 1.5 ROCK 4C+" ] && [ "$ENVIRONMENT" == "production" ]; then
				#Production environment is not set in foreman, use development environment
				HOST_GROUP_ID=1
				ENVIRONMENT_ID=1
			else
				echo "$BOARD_TYPE $ENVIRONMENT combination is not valid. Aborting"
				exit 1
			fi
			#command without sudo 
			HAMMER_COMMAND="hammer host update --name $PUPPET_AGENT --puppet-environment-id $ENVIRONMENT_ID --hostgroup-id $HOST_GROUP_ID --new-location \"Default Location\" --new-organization \"Default Organization\""
			break
		;;
		q)
			exit 0
		;;
		*)
			echo
			echo "Invalid Puppet server option. Try again"
			exit 1
		;;
	esac
done

sudo puppet config set server $PUPPET_SERVER

NEW_PUPPET_MASTER=$(sudo puppet config print server)
echo "Migrating flowbox $PUPPET_AGENT to new puppet master: $NEW_PUPPET_MASTER"

sudo rm -fr /etc/puppetlabs/puppet/ssl

echo [ssh] enter a valid username on $NEW_PUPPET_MASTER 
read SSH_USERNAME

sudo puppet agent -t --waitforcert 30 & 
ssh -t $SSH_USERNAME@$NEW_PUPPET_MASTER "sudo -S /opt/puppetlabs/server/bin/puppetserver ca sign --certname $PUPPET_AGENT" 
wait

echo "setting basic host params on $NEW_PUPPET_MASTER" 
ssh -t $SSH_USERNAME@$NEW_PUPPET_MASTER '$HAMMER_COMMAND'



echo "$PUPPET_AGENT migrated to $NEW_PUPPET_MASTER"



echo "----------------------------------------------------------------------------"
echo "Revoke certificate on $OLD_PUPPET_MASTER? y(es)|n(o)"
echo "----------------------------------------------------------------------------"
read REMOVE_OLD_CERT
if [ "$REMOVE_OLD_CERT" == "y" ]; then
	echo [ssh] enter a valid username on $OLD_PUPPET_MASTER
	read SSH_USERNAME
	ssh -t $SSH_USERNAME@$OLD_PUPPET_MASTER "sudo -S /opt/puppetlabs/server/bin/puppetserver ca clean --certname $PUPPET_AGENT && hammer host delete --name $PUPPET_AGENT"
elif [ "$REMOVE_OLD_CERT" == "n" ]; then
	echo Ok, will not remove old certificate on $OLD_PUPPET_MASTER
fi


sudo puppet agent -t