--- /home/andrew/Github/AlexaPi/auth_web.py	2017-02-14 13:01:33.083942183 -0800
+++ /home/andrew/Github/AlexaPi2/auth_web.py	2017-02-16 16:24:01.182276986 -0800
@@ -3,6 +3,8 @@
 import cherrypy
 import os
 from cherrypy.process import servers
+import subprocess
+import time
 import requests
 import json
 from creds import *
@@ -32,14 +34,39 @@
 		url = "https://api.amazon.com/auth/o2/token"
 		r = requests.post(url, data = payload)
 		resp = r.json()
-		line = 'refresh_token = "{}"'.format(resp['refresh_token'])
-		with open("creds.py", 'a') as f:
-			f.write(line)
-		return "Success!, refresh token has been added to your creds file, you may now reboot the Pi <br>{}".format(resp['refresh_token'])
+		line = "refresh_token = \"" + resp['refresh_token'] + "\""
+                subprocess.call('echo -n > alexaToken.py',shell=True)
+                with open("alexaToken.py", 'a') as f:
+                        f.write(line)
+
+		return "Success! The refresh token has been added to your directory. Alexa is now starting on your device. You may close this browser.<br>"
+		quit()
 	index.exposed = True
 	code.exposed = True
-		
-cherrypy.config.update({'server.socket_host': '0.0.0.0',})
-cherrypy.config.update({'server.socket_port': int(os.environ.get('PORT', '5000')),})
-cherrypy.quickstart(Start())
+
+ip = None
+ipCmd = "sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\\2/p'"
+ip = subprocess.check_output("ifconfig wlan0 | " + ipCmd, shell=True)
+
+if ip == None or ip == "":
+        ip = subprocess.check_output("ifconfig wlan1 | " + ipCmd, shell=True)
+
+if ip == None or ip == "":
+        ip = subprocess.check_output("ifconfig eth0 | "  + ipCmd, shell=True)
+
+if ip == None or ip == "" or ip == " ":
+        raise Exception('No host IP address found! Please check our internet connection.')
+	os.system("exit")
+else:
+        ip = ip.rstrip()
+	subprocess.Popen('sleep 1 && echo "" && echo "To validate your settings, do the following steps:"', shell=True)
+	subprocess.Popen('sleep 1.1 && printf "Go to your https://developer.amazon.com account, \nnavigate to the Alexa settings, \nclick on Security Profile, \nand add these addresses under Web Settings:\n"', shell=True)
+	subprocess.Popen('sleep 1.2 && echo "" && echo "Allowed Origins: http://' + ip + ':5000"', shell=True)
+	subprocess.Popen('sleep 1.3 && echo "Allowed Return URL: http://' + ip + ':5000/code"', shell=True)
+	subprocess.Popen('sleep 1.4 && echo "" && echo "Then in your web browser, go to http://' + ip + ':5000 and follow the instructions."', shell=True)
+
+	cherrypy.config.update({'server.socket_host': ip,})
+	cherrypy.config.update({'server.socket_port': int(os.environ.get('PORT', '5000')),})
+	cherrypy.quickstart(Start())
+
 
