#!/usr/bin/python
from bottle import Bottle, run, template, static_file, error, redirect, post, request, response
import glob
import re
import time
import random
import string
import os
import shutil
import subprocess

app = Bottle()

APP_ROOT = "/home/pi"

def run_command(cmd):
    proc = subprocess.Popen([cmd], stdout=subprocess.PIPE, shell=True)
    (out, err) = proc.communicate()
    return out

@app.route('/')
def index():
    data = { 
             'xyz': 1
           }

    return template('%s/web-root/index.tpl' % (APP_ROOT), data)

@app.route('/internet_on')
def internet_on():
    os.system('/home/pi/root-bin/connect_to_internet') 

@app.route('/internet_off')
def internet_off():
    os.system('/home/pi/root-bin/disconnect_from_internet') 

@app.route('/get_balance')
def get_balance():
    response.status = 200
    myout = run_command('/home/pi/root-bin/get_balance.sh')
    return myout 

@app.route('/get_status')
def get_status():
    response.status = 200
    myout = run_command('/home/pi/root-bin/get_status.sh')
    return myout 

@app.route('/shutdown')
def shutdown_proxypi():
    os.system('/sbin/init 0') 

@app.route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='%s/web-root/static' % (APP_ROOT))

@app.error(404)
def error404(error):
    return index()

run(app, host='192.168.255.1', port=80)
