#!/usr/bin/python
# -*- coding: utf-8 -*-

import cgi, cgitb, os, sys, time, subprocess

cgitb.enable()

form = cgi.FieldStorage()
action = form['action'].value

print "Content-type:text/html\r\n\r\n"

if action == "1":
    os.system("echo 1 > /var/tmp/wui_actions")
    print("Defaults Restored Successfully")

elif action == "2":
    os.system("echo 2 > /var/tmp/wui_actions")
    print("Resetting Codec")

elif action == "3":
    os.system("echo 3 > /var/tmp/wui_actions")
    print("Rebooting Unit")

elif action == "4":
    cid = form['cid'].value
    os.system("echo 4" + cid + " > /var/tmp/wui_actions")
    print("Custom ID " + cid + " Set, Rebooting Unit")

elif action == "5":
    os.system("echo 5 > /var/tmp/wui_actions")
    print("Upgrading Controller")

elif action == "6":
    fs = form['fs'].value
    os.system("echo 6" + fs + " > /var/tmp/wui_actions")
    print("Formatting Device " + fs)

elif action == "7":
    pw = form['pw'].value
    if pw == "":
        os.system("echo 7 > /var/tmp/wui_actions")
        print("Clearing admin Password")
    else:
        os.system("echo 7" + pw + " > /var/tmp/wui_actions")
        print("Setting admin Password")

elif action == "8":
    os.system("echo 8 > /var/tmp/wui_actions")
    print("Creating download package")

elif action == "9":
    os.system("echo 9 > /var/tmp/wui_actions")
    print("Restarting Recorder")

elif action == "a":
    offset = form['offset'].value
    if offset == "0":
        os.system("echo a" + offset + " > /var/tmp/wui_actions")
        print("Stopping Playback")
    else:
        os.system("echo a" + offset + " > /var/tmp/wui_actions")
        print("Starting Playback from -" + offset + " minute(s)")

elif action == "b":
    os.system("echo b > /var/tmp/wui_actions")
    print("Toggling Recording")

elif action == "c":
    time = form['time'].value
    os.system("echo c" + time + " > /var/tmp/wui_actions")
    print("Synchronising Time " + time)

elif action == "d":
    if form.has_key('start'):
        start = form['start'].value
        os.system("echo d" + start + " > /var/tmp/wui_actions")
        print("Starting RF Calibration")
    else:
        os.system("echo d > /var/tmp/wui_actions")
        print("Stopping RF Calibration")

else:
    print("Invalid Action")
	