# all the imports
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, \
    abort, render_template, flash
from contextlib import closing

import os

VERSION = "0.0.0"

# Read configuration from environment variables - these are set by the secret script that we don't commit...
DATABASE = os.environ.get('CUTE_DB')
DEBUG = os.environ.get('CUTE_DEBUG') in ['True', 'true', '1', 'yes']
SECRET_KEY = os.environ.get('CUTE_SECRET_KEY')
USERNAME = os.environ.get('CUTE_USERNAME')
PASSWORD = os.environ.get('CUTE_PASSWORD')

if USERNAME is None:
    print("Username not set! Run through the secret shell script.")
    exit(1)

if PASSWORD is None:
    print("Password not set! Run through the secret shell script.")
    exit(1)

if SECRET_KEY is None:
    print("Secret key not set! Run through the secret shell script.")
    exit(1)

print("Starting CuteCasa backend " + VERSION + "...")

app = Flask(__name__)
app.config.from_object(__name__)


def connect_db():
    return sqlite3.connect(app.config['DATABASE'])


# Bringup and teardown
@app.before_request
def before_request():
    g.db = connect_db()


@app.teardown_request
def teardown_request(exception):
    db = getattr(g, 'db', None)
    if db is not None:
        db.close()


@app.route('/')
def splash():
    return render_template('splash.html')


if __name__ == '__main__':
    app.run(host='0.0.0.0')


def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql', mode='r') as f:
            db.cursor().executescript(f.read())
        db.commit()

@@ -536,16 +536,51 @@
 SSWORD')
+%0APORT = os.environ.get('CUTE_PORT')
 %0A%0Aif USE
@@ -883,16 +883,113 @@
 xit(1)%0A%0A
+if PORT is None:%0A    print(%22Port is not set! Run through the secret shell script.%22)%0A    exit(1)%0A%0A
 print(%22S
@@ -1512,16 +1512,27 @@
 0.0.0.0'
+, port=PORT
 )%0A%0A%0Adef 

add port export