#!/bin/bash

SerialNumber=$(ioreg -l | grep IOPlatformSerialNumber | awk '{print $4}' | sed 's/\"//g')

input="/Users/lcast162/Desktop/addresses.csv"
while IFS=, read -r serial key ; do
  # do something... Don't forget to skip the header line!
   if [  "$serial" == "$SerialNumber" ]; then
  		RecoveryKey="$key"
  		break
  fi
done < $input


echo "$RecoveryKey"

if [ "$RecoveryKey" != "" ];then

cat >  /tmp/FV.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0">
<dict>
	<key>RecoveryKey</key> 
	<string>$RecoveryKey</string> 
	<key>SerialNumber</key>
	<string>$SerialNumber</string>
</dict>
</plist>
EOF

	# Upload Key to Addigy
	#stores the path of the FileVault plist
	FDataPath=/tmp/FV.plist
	# Addigy FV Escrow Location
	FVMgr=/Library/Addigy/fv-escrows
	#Create Escrow directory if it does not exist

	if [ -d "$FVMgr" ]; then
		echo "Addigy FV Escrow Directory exists already"
		#copy file to escrow location
		cp -n $FDataPath $FVMgr/fdesetup.plist
		#run escrow
		/Library/Addigy/filevault-manager -escrow
	else
		echo "Addigy FV Escrow Directory does not exist"
		mkdir $FVMgr
		#copy file to escrow location
		cp -n $FDataPath $FVMgr/fdesetup.plist
		#run escrow
		/Library/Addigy/filevault-manager -escrow
	fi
else
	echo "RecoveryKey not found"
	exit 0
fi

rm -rf $FDataPath
