#!/usr/bin/env python3

import sys
import os

print("Content-type: text/html")
print()   # må ha tom linje mellom HTTP Headers og Body

start = """\
<html>
<head>
  <meta charset="utf-8">
  <title>Hello World</title>
</head>
<body>
  <h1>What do I get from CGI?</h1>
"""

end = """\
</body>
</html>
"""

def debug():
  print("<p>sys.argv</p>")
  print("<pre>")
  print(sys.argv)
  print("</pre>")

  print("<p>os.getenv</p>")
  print("<pre>")
  for k,v in os.environ.items():
    print(k,":",v)
  print("</pre>")


print(start)


try:
  path = os.getenv("PATH_INFO")
  
  if path.startswith("/hello"):
    
    request,name = os.getenv("QUERY_STRING").split('=')
    if request == "name":
       print(f"<h2>Hello, {name}</h2>")
       print(f"<p>Your name has {len(name)} letters.</p>")
    else:
       print(f"Hmm, don't know what to do with {request}={name}")
       debug()

  else:
    debug()

except BaseException as e:
  print(f"Python exception:\n{e}")


print(end)
