#!/usr/bin/python

import sys
import os
import re

flash = ("panic=3 root=/dev/mtdblock3 rw mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000", # enigma2
		"panic=3 root=/dev/mtdblock3 bigphysarea=1500 quiet", # dgs
		"console=ttyAS1,115200 panic=3 root=/dev/mtdblock3 rw mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000", # enigma2 debug
		"console=ttyAS1,115200 panic=3 root=/dev/mtdblock3 bigphysarea=1500 quiet") # dgs debug
		
usb = ("panic=3 root=/dev/#DEVICE# rw rootdelay=10 init=/bin/devinit mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000", # enigma2
		"panic=3 bigphysarea=10240 root=/dev/#DEVICE# rw rootdelay=10 init=/bin/devinit mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000", # dgs ?
		"console=ttyAS1,115200 panic=3 root=/dev/#DEVICE# rw rootdelay=10 init=/bin/devinit mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000", # enigma2 debug
		"console=ttyAS1,115200 panic=3 bigphysarea=10240 root=/dev/#DEVICE# rw rootdelay=10 init=/bin/devinit mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000") # dgs debug ?

sata = ("panic=3 root=/dev/#DEVICE# rw rootdelay=10 init=/bin/devinit mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000", # enigma2
		"panic=3 bigphysarea=10240 root=/dev/#DEVICE# rw rootdelay=10 init=/bin/devinit mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000", # dgs ?
		"console=ttyAS1,115200 panic=3 root=/dev/#DEVICE# rw rootdelay=10 init=/bin/devinit mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000", # enigma2 debug
		"console=ttyAS1,115200 panic=3 bigphysarea=10240 root=/dev/#DEVICE# rw rootdelay=10 init=/bin/devinit mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000") # dgs debug ?

nfs = ("panic=3 root=/dev/nfs nfsroot=#NFSROOT# rw rootpath=#NFSROOT# ip=\\$ipaddr:\\$serverip:\\$gateway:\\$netmask mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000 init=/bin/devinit", # enigma2
		"panic=3 bigphysarea=10240 root=/dev/nfs nfsroot=#NFSROOT# rw rootpath=#NFSROOT# ip=\\$ipaddr:\\$serverip:\\$gateway:\\$netmask mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000 init=/bin/devinit", # dgs ?
		"console=ttyAS1,115200 panic=3 root=/dev/nfs nfsroot=#NFSROOT# rw rootpath=#NFSROOT# ip=\\$ipaddr:\\$serverip:\\$gateway:\\$netmask mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000 init=/bin/devinit", # enigma2 debug
		"console=ttyAS1,115200 panic=3 bigphysarea=10240 root=/dev/nfs nfsroot=#NFSROOT# rw rootpath=#NFSROOT# ip=\\$ipaddr:\\$serverip:\\$gateway:\\$netmask mem=128m coprocessor_mem=4m@0x10000000,4m@0x10400000 init=/bin/devinit") # dgs debug ?

fw_setenv_bin = "/var/bin/fw_setenv"
fw_printenv_bin = "/var/bin/fw_printenv"

menu_items = [ "", "", "", "", "", "", "", "", "" ]
ipaddr = ""
netmask = ""
gateway = ""
serverip = ""

def show_menu(id):
	if len(menu_items[id]) == 0:
		return
		
	print "*** MENU %d ***" % (id + 1)
	items = menu_items[id].split("\"")
	last_action = ""
	skip = False
	for item in items:
		if skip:
			last_action += "\"%s\"" % item
			skip = False
		else:
			actions = item.split(";")
			count = len(actions)
			if count > 1:
				i = 0
				for action in actions:
					last_action += action
					if i < count - 1:
						print last_action
						last_action = ""
					else:
						skip = True
					i += 1
			else:
				last_action += actions[0]
				skip = True
				
	if len(last_action) > 0:
		print last_action
	
def action_show():
	proc = os.popen(fw_printenv_bin)
	for line in proc:
		res = re.search("^menu_(\d)=(.*)", line)
		if res:
			menu_items[int(res.group(1))-1] = res.group(2)
		elif line.find("ipaddr=") == 0:
			ipaddr = line[7:].strip()
		elif line.find("netmask=") == 0:
			netmask = line[8:].strip()
		elif line.find("gateway=") == 0:
			gateway = line[8:].strip()
		elif line.find("serverip=") == 0:
			serverip = line[9:].strip()
			
	proc.close()
	for i in range (0, 9):
		show_menu(i)
	print "*** NETWORK ***"
	print "IP address: %s" % ipaddr
	print "Gateway: %s" % gateway
	print "Subnet mask: %s" % netmask
	print "Server IP: %s" % serverip

def set_nfs(menuid, label, image, nfsroot):
	cmd = "%s menu_%d \"front_puts \\\"%s\\\";setenv bootargs \\\"%s\\\";" % (fw_setenv_bin, menuid, label, nfs[image])
	cmd += "setenv bootcmd \\\"nfs a5ff0000 \\$serverip:#NFSROOT#/boot/uImage;bootm a5ff0000\\\";saveenv;boot\""
	cmd = cmd.replace("#NFSROOT#", nfsroot)
	os.system(cmd)

def set_disk(menuid, label, image, device):
	cmd = "%s menu_%d \"front_puts \\\"%s\\\";setenv bootsata \\\"run bootargssata;ide reset;ext2load sata 0:1 a5ff0000 boot/uImage;bootm a5ff0000\\\";" % (fw_setenv_bin, menuid, label)
	cmd += "setenv bootargssata \\\"setenv bootargs %s\\\";setenv bootcmd \\\"run bootsata\\\";saveenv;boot\"" % sata[image]
	cmd = cmd.replace("#DEVICE#", device)
	os.system(cmd)

def set_usb(menuid, label, image, device):
	cmd = "%s menu_%d \"front_puts \\\"%s\\\";setenv bootusb \\\"run bootargsusb;usb reset;ext2load usb 0:1 a5ff0000 boot/uImage;bootm a5ff0000\\\";" % (fw_setenv_bin, menuid, label)
	cmd += "setenv bootargsusb \\\"setenv bootargs %s\\\";setenv bootcmd \\\"run bootusb\\\";saveenv;boot\"" % usb[image]
	cmd = cmd.replace("#DEVICE#", device)
	os.system(cmd)

def set_flash(menuid, label, image):
	cmd = "%s menu_%d \"front_puts \\\"%s\\\";setenv bootargs \\\"%s\\\";setenv bootcmd \\\"bootm a0060000\\\";saveenv;boot\"" % (fw_setenv_bin, menuid, label, flash[image])
	os.system(cmd)
	
def set_ipaddress(zipaddr, zserverip, zgateway, znetmask):
	os.system("%s ipaddr %s" % (fw_setenv_bin, zipaddr))
	os.system("%s serverip %s" % (fw_setenv_bin, zserverip))
	os.system("%s gateway %s" % (fw_setenv_bin, zgateway))
	os.system("%s netmask %s" % (fw_setenv_bin, znetmask))
	
def show_help():
	print "SIFTeam uboot menu editor"
	print
	print "Usage:"
	print "uboot show"
	print "uboot set [menu_id] [label] [image] [type] [arg]"
	print "uboot net [ipaddr] [netmask] [gateway] [serverip]"
	print
	print "Options:"
	print "menu_id\t\tnumber from 1 to 9 (the key in your remote)"
	print "label\t\tmenu description (will be print on cuberevo display at boot)"
	print "image\t\t'enigma2' or 'dgs' or 'enigma2_debug' or 'dgs_debug'"
	print "type\t\t'flash' or 'usb' or 'disk' or 'nfs'"
	print "arg\t\tdepend from type:"
	print "\t\tflash\tno argument is required"
	print "\t\tusb\t[device]"
	print "\t\tdisk\t[device]"
	print "\t\tnfs\t[nfs_root]"
	print
	print "Examples:"
	print "# add dgs flash to button 1"
	print "uboot set 1 \"DGS FLASH\" dgs flash"
	print "# add enigma2 flash to button 2"
	print "uboot set 2 \"ENIGMA2 FLASH\" enigma2 flash"
	print "# add enigma2 usb to button 3"
	print "uboot set 3 \"ENIGMA2 USB\" enigma2 usb sdb1"
	print "# add enigma2 disk to button 4"
	print "uboot set 4 \"ENIGMA2 HDD\" enigma2 disk sda1"
	print "# add enigma2 nfs to button 5"
	print "uboot set 5 \"ENIGMA2 NFS\" enigma2 nfs /home/9000hd"
	print
	print "# set ip address in uboot"
	print "uboot net 192.168.0.6 255.255.255.0 192.168.0.1 192.168.0.3"
	
image = 0

if len(sys.argv) < 2:
	show_help()
	exit(0)

if sys.argv[1] == "show":
	action_show()
elif sys.argv[1] == "set":
	if len(sys.argv) < 6:
		show_help()
		exit(0)
		
	if sys.argv[4] == "enigma2":
		image = 0
	elif sys.argv[4] == "dgs":
		image = 1
	elif sys.argv[4] == "enigma2_debug":
		image = 2
	elif sys.argv[4] == "dgs_debug":
		image = 3
	else:
		show_help()
		exit(0)
		
	if sys.argv[5] == "flash":
		set_flash(int(sys.argv[2].strip()), sys.argv[3].strip(), image)
	else:
		if len(sys.argv) < 7:
			show_help()
			exit(0)
		if sys.argv[5] == "usb":
			set_usb(int(sys.argv[2].strip()), sys.argv[3].strip(), image, sys.argv[6].strip())
		if sys.argv[5] == "disk":
			set_disk(int(sys.argv[2].strip()), sys.argv[3].strip(), image, sys.argv[6].strip())
		if sys.argv[5] == "nfs":
			set_nfs(int(sys.argv[2].strip()), sys.argv[3].strip(), image, sys.argv[6].strip())
elif sys.argv[1] == "net":
	if len(sys.argv) < 5:
		show_help()
		exit(0)
		
	set_ipaddress(sys.argv[2].strip(), sys.argv[5].strip(), sys.argv[4].strip(), sys.argv[3].strip())
else:
	show_help()
