#!/usr/bin/env python
################################################################################### 
# Create a directory, a file, or both
################################################################################### 
import sys
import cgi
import os.path
from datetime import datetime

print("Content-Type: text/html \n")                 #required http response header (w/ extra line)

elements = cgi.FieldStorage()                       #obtain the http parameters
dir  = str(elements.getvalue('dir'))                #get directory name from web param
file = str(elements.getvalue('file'))               #get file name from web param

if len(sys.argv) > 2:                               #if batch param
    dir  = sys.argv[1]                              #get directory name from batch param
    file = sys.argv[2]                              #get file name from web param  
    
homeDir = "/home/sultans/web/python/demo/data/"     #must have permissions 777 

if dir  == 'None': dir  = "dir1" 
if file == 'None': file = "file1"

#======================================================================================
# Create a directory, a file, or both
#======================================================================================
def create(dir,file):

    print()
    print("You can enter a directory and/or file name at end of the url?dir=name&file=name <br>")

    print("Create Directory and/or File -<br>")

    if dir == "" : print("Using default directory:", homeDir, '<br>') 

    dirname  = homeDir + dir                            #append requested dir 

    try:
        if os.path.exists(dirname):                     #if directory already exists 
            print("Directory:", dirname, "exists <br>") 
        else:                                           #if directory does not exist
            os.makedirs(dirname)                        #create the directory
            os.chmod(dirname, 0o777)                    #change permissions to 777
            print("Directory:", dirname, "created <br>") 

        if file != '':                                  #if file is provided
            filename = dirname +'/'+ file               #append the directory name
            if os.path.exists(filename):                #if the file already exists
                print("File:", file, "exists <br>")
            else: 
                f = open(filename, 'w')                 #open the file in write mode. if not exist create
                print("File:", file, "created <br>") 

    except OSError as e:
        print(e)

#======================================================================================
create(dir,file)                                #call he above function



#=== 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
#=================================================================================