#!/usr/bin/env python

# Import the required modules

# For executing system commands nd getting uid

import os

# For comparing files to get the firmware in use

import filecmp

# For the sys.exit function

import sys

# Check we have root permissions

if os.geteuid() != 0:
	print ("You need root privilages to use this tool - exiting...")
	sys.exit()
	 
# We would have exited by now if we weren't root, so carry on

# Ceate a dictionary with the RAM use in Linux as the key and the
# firmware file path as the value

firmwares = {128:"/boot/arm128_start.elf", 192:"/boot/arm192_start.elf",
 244:"/boot/arm224_start.elf"}

# Constants for easier reading when getting a key or value from
# an item in the dictionary

Key = 0
Path = 1

# Work out which firmware is currently in use by comparing each file
# in the dictionary with start.elf

for firmware in firmwares.items():
	result = filecmp("/boot/start.elf", firmware[Path], False)

# When we find the correct file

if result == True:
        
	# Work out the RAM allocation of Linux and GPU
	
	currentLinux = firmware[Key]
	currentGPU = 256 - currentLinux
	print (str(currentLinux) +"MB of ram is currently being used by Linux.")
	print (str(currentGPU) +"MB of ram is currently being used by the GPU.")
	
# Display a menu

print("\nWhat would you like to do?\n")

# Print an option for each firmware

count = 1
for firmware in firmwares.items():

	# Work out the RAM allocation of Linux and the GPU

	firmwareLinux = firmware[Key]
	firmwareGPU = 256 - firmwareLinux
	print (str(count) + ". Allocate " + str(firmwareLinux) + "MB of RAM to Linux leaving " + str(firmwareGPU) + "MB of RAM for the GPU.")
	count += 1

# Add an exit option
	
print (str(count) + ".Exit.")
	
# Loop to make sure we get a valid reply
	
validOption = False
while validOption == False:
	option = raw_input("\nPlease select an option from above: ")
		
# Try to convert our option to an integer and set isInt either
# way... we don't want to crash on a bad option
		
try:
	option = int(option)
	isInt = True
except ValueError:
	isInt = False
if isInt == True:
	if option <= count:
		
# Stop the while loop
		
		validOption = True
		
# Deal with our options (only in the range of the count as we
# don't need to handle exit)

		if option < count:
			# Work out the RAM allocation of Linux and GPU
			newFirmware = firmwares.keys()[option - 1]
			newGPU = 256 - newFirmware
			print ("\nUsing the firmware which allocates " + str(newFirmware) 
			+ "MB of RAM to Linux and " + str(newGPU) + "MB of RAM to the GPU...")
			
			# Run the cp command with appropriate values
			copyResult = os.system("cp " + firmwares[newFirmware] + " " + "/boot/start.elf")
			if copyResult != 0:
			print (\nThere was an error while copying the requested firmware!") print ("Exiting...")
			else:
				print ("\nThe requested firmware was copied
						  successfully.\n")
						  # Loop to make sure we have a proper response
						  # for restarting
						  restartLoop = False
						  while restartLoop == False:
							  restartResult = raw_input("The device
							  needs to restart " "before the changes
							  take place. Would you like to do this now?
							  (y/n) ")
							  if restartResult == "y":
								  # Stop the while loop
								  restartLoop = True # We need to
								  # restart print("The system is going 
								  down for a restart...")
								  
								  os.system("shutdown -r now")
								  elif restartResult == "n":
									  # Stop the while loop
									  restartLoop = True
									  # We just need to exit (by doing
									  nothing) print ("\nExiting...")# your code goes here