#!/usr/bin/env python3
"""Vineham Auto - local preview. Run this file (or serve.bat); your browser opens the site."""
import http.server, socketserver, webbrowser, threading, os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

class Handler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header("Cache-Control", "no-store")
        super().end_headers()
    def log_message(self, *a):
        pass

def serve():
    for port in range(5500, 5520):
        try:
            httpd = socketserver.TCPServer(("", port), Handler)
        except OSError:
            continue
        url = "http://localhost:%d/" % port
        print("\n  Vineham Auto preview is running at:  " + url)
        print("  Sales Console:  " + url + "#login   (Car1234 / Car1234!)")
        print("  Leave this window open. Press Ctrl+C to stop.\n")
        threading.Timer(0.8, lambda: webbrowser.open(url)).start()
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("\n  Stopped.")
        return
    print("Could not find a free port (5500-5519).")

if __name__ == "__main__":
    serve()
