import os
import sys
from http.server import HTTPServer as S
from http.server import SimpleHTTPRequestHandler as H


class C(H):
    def do_GET(self):
        p = self.path
        if p.endswith("/"):
            base_path = p.rstrip("/")
            html_file = base_path + ".html"
            if os.path.exists(os.path.join(os.getcwd(), html_file.lstrip("/"))):
                p = html_file
            else:
                p += "index.html"
        elif "." not in p.split("/")[-1]:
            r = os.path.join(os.getcwd(), p.lstrip("/"))
            if os.path.exists(r + ".html"):
                p += ".html"
            elif os.path.isdir(r):
                p += "/index.html"
        self.path = p
        return H.do_GET(self)


if __name__ == "__main__":
    d = sys.argv[1] if len(sys.argv) > 1 else "."
    p = int(sys.argv[2]) if len(sys.argv) > 2 else 8000
    os.chdir(d)
    S(("0.0.0.0", p), C).serve_forever()
