#! /usr/bin/python

import sys
import os
import shutil

def dbHelp():
    print "Command:"
    print "\tcreate DBName: create the database"
    print "\tdelete DBName: delete the database"
    print "\tlist: list the table infomation in the database"
    print "\ttranslate SQL: translate SQL into CUDA file"
    print "\texecute SQL: translate and execute given SQL on GPU"
    print "\tload TableName data: load data into the given table"
    print "\texit"

def dbCreate(dbName):

    ret = 0
    dbTop = "database"

    if not os.path.exists(dbTop):
        os.makedirs(dbTop)

    dbPath = dbTop + "/" + dbName

    if os.path.exists(dbPath):
        return -1

    os.makedirs(dbPath)

    cmd = 'python XML2MapReduce/main.py ' + schemaFile
    ret = os.system(cmd)

    if ret !=0 :
        exit(-1)

    cmd = 'make -C src/GPUCODE/ loader &> /dev/null'
    ret = os.system(cmd)

    if ret != 0:
        exit(-1)

    cmd = 'cp src/GPUCODE/gpuDBLoader ' + dbPath
    ret = os.system(cmd)

    if ret != 0:
        exit(-1)

    return 0

def dbDelete(dbName):

    dbTop = "database"

    dbPath = dbTop + "/" + dbName
    if os.path.exists(dbPath):
        shutil.rmtree(dbPath)

if len(sys.argv) != 2:
    print "./gpudb.py schemaFile"
    exit(-1)

schemaFile = sys.argv[1]

while 1:
    ret = 0
    dbCreated = 0
    dbName = ""

    cmd = raw_input(">")
    cmdA = cmd.lstrip().rstrip().split()

    if len(cmdA) == 0:
        continue

    if cmdA[0].upper() == "HELP":
        dbHelp()

    elif cmdA[0].upper() == "?":
        dbHelp()

    elif cmdA[0].upper() == "EXIT":
        break

    elif cmdA[0].upper() == "CREATE":

        if dbCreated !=0:
            print "Already created database. Delete first."
            continue


        if len(cmdA) !=2:
            print "usage: create DBName"

        else:
            ret = dbCreate(cmdA[1].upper())
            if ret == -1:
                print cmdA[1] + " already exists"
            else:
                dbCreated = 1
                dbName = cmdA[1].upper()
                print cmdA[1] + " has been successfully created"
                


    elif cmdA[0].upper() == "DELETE":
        if len(cmdA) !=2:
            print "usage: delete DBName"

        dbCreated = 0
        dbDelete(cmdA[1].upper())

    else:
        print "Unknown command"

os.system("clear")


@@ -47,16 +47,75 @@
 t shutil
+%0Aimport pickle%0Asys.path.insert(0, %22XML2CODE%22)%0Aimport ystree
 %0A%0Adef db
@@ -258,16 +258,23 @@
  %22%5Ctlist
+ DBName
 : list t
@@ -302,32 +302,98 @@
 n the database%22%0A
+    print %22%5Ctload TableName data: load data into the given table%22%0A
     print %22%5Cttra
@@ -504,74 +504,8 @@
 PU%22%0A
-    print %22%5Ctload TableName data: load data into the given table%22%0A
     
@@ -531,32 +531,44 @@
  dbCreate(dbName
+, schemaFile
 ):%0A%0A    ret = 0%0A
@@ -789,17 +789,12 @@
 XML2
-MapReduce
+CODE
 /mai
@@ -812,16 +812,34 @@
 hemaFile
+ + ' &%3E /dev/null'
 %0A    ret
@@ -1021,10 +1021,10 @@
  = '
-cp
+mv
  src
@@ -1112,24 +1112,132 @@
   exit(-1)%0A%0A
+    cmd = 'mv src/GPUCODE/.metadata ' + dbPath%0A    ret = os.system(cmd)%0A%0A    if ret != 0:%0A        exit(-1)%0A%0A
     return 0
@@ -1381,16 +1381,390 @@
 bPath)%0A%0A
+def dbList(dbName):%0A%0A    dbTop = %22database%22%0A    dbPath = dbTop + %22/%22 + dbName%0A%0A    if not os.path.exists(dbPath):%0A        return -1%0A%0A    metaPath = dbPath + %22/.metadata%22 %0A%0A    if not os.path.exists(metaPath):%0A        return -2%0A%0A    metaFile = open(metaPath, 'rb')%0A    tableDict = pickle.load(metaFile)%0A    metaFile.close()%0A%0A    for tn in tableDict.keys():%0A        print tn%0A%0A
 if len(s
@@ -2442,16 +2442,28 @@
 .upper()
+, schemaFile
 )%0A      
@@ -2684,16 +2684,17 @@
  created
+.
 %22%0A      
@@ -2759,32 +2759,33 @@
  if len(cmdA) !=
+ 
 2:%0A            p
@@ -2870,16 +2870,282 @@
 per())%0A%0A
+        print cmdA%5B1%5D + %22 has been successfully deleted.%22%0A%0A    elif cmdA%5B0%5D.upper() == %22LIST%22:%0A        if len(cmdA) != 2:%0A            print %22usage: list DBName%22%0A            continue%0A%0A        dbList(cmdA%5B1%5D.upper())%0A%0A    elif cmdA%5B0%5D.upper() == %22LOAD%22:%0A        pass%0A%0A
     else

save for wrapper, not finish yet