#!/bin/bash

# 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

clear
echo
echo "---------------------------------------------------------------------------" 
echo "Flowbox Setup script"
echo "Hit CTRL+C anytime to exit"
echo "---------------------------------------------------------------------------" 
echo

################ GLOBALS ###################################
readonly FLOW_PI_USER="flowpi"
readonly CONFIG_FILE=$(basename "$0")
readonly BOARD_TYPE=$(sudo cat /sys/firmware/devicetree/base/model | tr -d '\0')
#make os-release variables available to shell
. /etc/os-release 
readonly RELEASE=$(lsb_release -c -s)
PUPPET_SERVER=""
PUPPET_SERVER_ENVIRONMENT=""
################ GLOBALS ###################################


##################################################################################################################
function Run(){
echo "---------------------------------------------------------------------------" 
echo "Board: $BOARD_TYPE found"
echo "OS: $ID ($VERSION_ID)"
echo "RELEASE: $RELEASE"
echo "---------------------------------------------------------------------------" 
echo
echo

#AUTO SELECT BOARD TYPE
	case $BOARD_TYPE in
		"Raspberry Pi 4 Model B Rev 1.5")
			SetUserAndPassword
			SetHostName
			SetPuppetMasterAddress
			RaspberryPI4ModelB
		;;
		"Radxa ROCK 4C+")
			SetUserAndPassword
			SetHostName
			SetPuppetMasterAddress
			rock4C+
		;;
		*)
		echo "Board type not supported yet"
		exit 1
		;;
	esac
}
##################################################################################################################

##################################################################################################################
function SetUserAndPassword() {
	# Create flowpi user
	sudo useradd -m -s /bin/bash ${FLOW_PI_USER}

	# Add user as sudoer
	sudo echo "$FLOW_PI_USER ALL=(ALL:ALL) ALL" >> /etc/sudoers

	while true
	do
		# Prompt for a password
		read -s -p "Enter password for '$FLOW_PI_USER': " NEW_PASSWORD
		echo

		# Ask the user to re-enter the password for validation
		read -s -p "Confirm password for '$FLOW_PI_USER: " NEW_PASSWORD_CONFIRM
		echo

		# Check that the passwords match
		if [ "$NEW_PASSWORD" != "$NEW_PASSWORD_CONFIRM" ]; then			
			echo -e "Passwords do not match. Please try again."
		else
			echo -e "Password set for '$FLOW_PI_USER'."
			break
		fi
	done

	# Change the password for the user 'flowpi'
	sudo usermod --password $(echo -e $NEW_PASSWORD | openssl passwd -6 -stdin) ${FLOW_PI_USER}
	if [ $? -eq 0 ]; then
	    echo "Password changed successfully."
	else
	    echo "Error changing password."
	    exit 1
	fi
}
##################################################################################################################

##################################################################################################################
function SetHostName() {
	echo
	echo
	echo "Please enter a valid hostname (flowboxXX.<location>.<tennant>.<country>)"
	read NEW_HOSTNAME

	if [ "$BOARD_TYPE" == "Raspberry Pi 4 Model B Rev 1.5" ]; then
	    sudo raspi-config nonint do_hostname $NEW_HOSTNAME
	elif [ "$BOARD_TYPE" == "Radxa ROCK 4C+" ]; then
	    sudo hostnamectl set-hostname $NEW_HOSTNAME
	fi

	echo "New hostname: $NEW_HOSTNAME"
}
##################################################################################################################

##################################################################################################################
function SetPuppetMasterAddress() {
	clear
	while true
		echo
		echo "choose a Puppet server"
		echo "1. puppet03.prod.flowretail.com"
		echo "2. puppet02.prod.flowretail.com"
		echo "3. puppet02.dev.flowretail.com"
		echo "4. custom puppet server address"
		read PUPPET_SERVER_OPTION 
		do
			case $PUPPET_SERVER_OPTION in
				1)
					PUPPET_SERVER="puppet03.prod.flowretail.com"
					PUPPET_SERVER_ENVIRONMENT="production"
					break
				;;
				2)
					PUPPET_SERVER="puppet02.prod.flowretail.com"
					PUPPET_SERVER_ENVIRONMENT="production"
					break
				;;
				3)
					PUPPET_SERVER="puppet02.dev.flowretail.com"
					PUPPET_SERVER_ENVIRONMENT="development"
					break
				;;
				4)
					echo "Set the puppet server (must be a valid DNS) - ex. puppet02.prod.flowretail.com"
  					read -p "Enter puppet server: " PUPPET_SERVER
					break
				;;
				*)
					clear
					echo
					echo "Invalid Puppet server option. Try again"
					exit 1
				;;
			esac
	done
	echo "You selected puppet server :$PUPPET_SERVER"
}
##################################################################################################################

################################### RaspberryPI4ModelB ###########################################################
function  RaspberryPI4ModelB() {
	clear
	echo "Running setup for $BOARD_TYPE"
	echo

	echo "update & upgrade"

	sudo wget https://apt.puppet.com/puppet7-release-bullseye.deb
	sudo dpkg -i puppet7-release-bullseye.deb

	sudo apt-get update
	sudo apt-get install puppet-agent=7.27.0-1bullseye
	sudo apt-get upgrade -y
	sudo apt-get autoremove -y

	echo "Install puppet"
	sudo /opt/puppetlabs/puppet/bin/puppet config set server $PUPPET_SERVER
	#sudo puppet config set environment ${PUPPET_SERVER_ENVIRONMENT} --section server

	clear
	echo "Registration of certificat - You must now approve the certificat on $PUPPET_SERVER"
	sudo /opt/puppetlabs/puppet/bin/puppet agent -t --waitforcert 30

	echo "Enable and start puppet"
	sudo systemctl daemon-reload
	sudo systemctl enable puppet
	sudo systemctl start puppet

	echo "Remove puppetlabs puppet7-release-bullseye.deb"
	sudo rm puppet7-release-bullseye.deb

	clear
	echo
	echo "---------------------------------------------------------------------------" 
	echo
	echo "$BOARD_TYPE setup done!"
	echo
	echo "---------------------------------------------------------------------------" 
	echo

	echo "Rebooting in 5 seconds..."
	sleep 5
	sudo reboot
}

################################### rock4C+ ######################################################################
function rock4C+() {
	clear
	echo "Apt and Puppet Running setup for $BOARD_TYPE"
	echo

	echo "update & upgrade"´
	sudo wget -O - https://apt.radxa.com/${RELEASE}-stable/public.key | sudo apt-key add -
	sudo wget -P /tmp https://apt.puppetlabs.com/puppet7-release-${RELEASE}.deb 
	sudo dpkg -i /tmp/puppet7-release-${RELEASE}.deb
	sudo apt update
	sudo apt upgrade -y

	clear
	echo "Install puppet"
	sudo apt install -y puppet-agent
	sudo apt autoremove -y

	source /etc/profile.d/puppet-agent.sh

	# Add puppetlabs path to sudoers secure_path
	sudo sed -i 's/\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/snap\/bin/\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/snap\/bin:\/opt\/puppetlabs\/bin\//' /etc/sudoers

	#Set puppet master
	sudo puppet config set server ${PUPPET_SERVER} --section main
	#sudo puppet config set environment ${PUPPET_SERVER_ENVIRONMENT} --section server

sudo  -i -u  ${FLOW_PI_USER} bash << EOF
echo "Registration of certificate - You must now approve the certificate on $PUPPET_SERVER"
sudo puppet agent -t --waitforcert 30
exit 0
EOF

	echo "Enable and start puppet"
	sudo systemctl enable puppet
	sudo systemctl start puppet

	sudo puppet agent -t 

	clear
	echo
	echo "---------------------------------------------------------------------------" 
	echo
	echo "$BOARD_TYPE setup done!"
	echo
	echo "---------------------------------------------------------------------------" 
	echo

}

Run