#!/bin/bash


ACTION="$1"

case $ACTION in

	preInstall)
		
		echo "Checking architecture..."
		MACHINE_TYPE=$(uname -m)
		if [ "$MACHINE_TYPE" == "x86_64" ]; then
			echo "Architecture 64 bits"
		else
			echo "Openboard can only be installed on 64-bit machines"
			exit 1
		fi
	;;

	postInstall)

		mv /usr/bin/OpenBoard /usr/bin/OpenBoard_orig
		echo '#!/bin/bash' > /usr/bin/OpenBoard
		echo 'if [[ $LANG = *"@"* ]]; ' >> /usr/bin/OpenBoard
		echo 'then' >> /usr/bin/OpenBoard
		echo 'LANG=ca_ES.UTF-8; /usr/bin/OpenBoard_orig $1' >> /usr/bin/OpenBoard
		echo 'else' >> /usr/bin/OpenBoard
		echo '/usr/bin/OpenBoard_orig' >> /usr/bin/OpenBoard
		echo 'fi' >> /usr/bin/OpenBoard
		chmod +x /usr/bin/OpenBoard

		file='/opt/openboard/etc/OpenBoard.config'
		if [ -f $file ]; then
			echo 'Adapting config file....'
			sed -i 's/EnableAutomaticSoftwareUpdates=true/EnableAutomaticSoftwareUpdates=false/gi' $file
			sed -i 's/EnableSoftwareUpdates=true/EnableSoftwareUpdates=false/gi' $file
		else
			echo 'Config file not exixts, do nothing.'
		fi


	;;
	remove)

		apt-get remove -y openboard
		TEST=$( dpkg-query -s  openboard 2> /dev/null| grep Status | cut -d " " -f 4 )
		if [ "$TEST" == 'installed' ];then

			exit 1
		else
			file='usr/bin/OpenBoard'
			if [ -f $file ]; then
				rm -f $file
			fi
		fi
				
	;;	
esac
