#!/usr/bin/python3
###################################################################################
# Redirect to another page if "cust_id" exists
###################################################################################
import os
import cgi, cgitb

cgitb.enable()

print('Content-Type: text/html')

#====================================================================================
# getCookies(): Get all cookies  
#      returns: all cookies as a dictionary
#====================================================================================
def getCookies():

    cookieStr = os.environ.get('HTTP_COOKIE')     #obtain the HTTP cookies
    if cookieStr is None: return

    cookiesDict = {}                              #create a dictionary

    cookiesArray = cookieStr.split('; ')          #split on semi-colon space
    for cookie in cookiesArray:
        (name, value) = cookie.split('=',1)       #split on first =
        cookiesDict[name] = value                 #build the cookies dictionary

    return cookiesDict 

#====================================================================================
# getCookie(): Get a single cookie
#        args: cookie name
#     returns: cookie value  
#====================================================================================
def getCookie(name):
    
    allCookies = getCookies()
    value = allCookies.get(name)                    #get the value of a single cookie
    return value

#====================================================================================
# Main code
#====================================================================================
username = getCookie('login')                       #get login cookie

if not username:                                 	#if there is a cust_id cookie for customer                           
    print('Location: login.py')                     #redirect to another page
    

print('\n')                                         #end the headers

print("<html>")
print("<body bgcolor=lightyellow>")
print("<h1><center>Welcome to our Site</center></h1>")
print("<h2>You are here or have been redirected to this page</h2>")
print("<p><b>Because you logged in properly with user: " + username + "</b>")
print('<br>')
print("</body>")
print("</html>")





#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================