from fabric.api import env, run, cd

tables_options = {
    'lineitem' : 'L',
    'customers' : 'c',
    'nation' : 'n',
    'orders' : 'O',
    'parts' : 'P',
    'region' : 'r',
    'suppliers' : 's',
    'partsupp' : 'S'
}

def dbgen(sf=1, table=" "):
    if not table:
       table = "-T {tbl_opt}".format(tbl_opt=tables_options[table])
    chunks = len(env.hosts)
    if chunks > 0:
        chunk = env.hosts.index(env.host) + 1
    else: chunk = 0
    # print "Scale Factor = ", sf
    # print "Chunks = ", chunks
    # print "Chunk = ", chunk
    # print "Table = ", table
    with cd("~/tpch-kit/tpch/tpch_2_17_0/dbgen"):
        dbgen_exec = "./dbgen -f -s {sf} -C {chunks} " \
                     "-S {chunk} {tbl}".format(sf=sf, chunks=chunks, chunk=chunk, tbl=table)
        # print dbgen_exec
        run(dbgen_exec)
        run("for file in $(ls *.tbl*); "
            "do "
            "mv $(basename $file) ~/tpch-kit-datasets/${file%.*};"
            "done")
    with cd("~/tpch-kit-datasets"):
        run("for file in $(ls *.tbl); "
            "do "
            "gzip $file;"
            "done")


def list():
    with cd("~/tpch-kit/tpch/tpch_2_17_0/dbgen"):
        run("ls -lh *.tbl*")

def clean():
    with cd("~/tpch-kit/tpch/tpch_2_17_0/dbgen"):
        run("rm *.tbl*")

def install():
    with cd("~/"):
        run("git clone https://github.com/alexpap/tpch-kit.git")
        run("mkdir ~/tpch-kit-datasets")

def update():
    with cd("~/tpch-kit"):
        run("git pull")

def uninstall():
    with cd("~/"):
        run("rm -rf tpch-kit")

@@ -941,18 +941,8 @@
 ets/
-$%7Bfile%25.*%7D
 ;%22%0A 
@@ -1046,32 +1046,66 @@
           %22do %22%0A
+            %22mv $file $%7Bfile%25.*%7D%22%0A
             %22gzi
@@ -1107,20 +1107,25 @@
  %22gzip $
+%7B
 file
+%25.*%7D
 ;%22%0A     
@@ -1176,39 +1176,25 @@
 tpch-kit
-/tpch/tpch_2_17_0/dbgen
+-datasets
 %22):%0A    
@@ -1211,20 +1211,18 @@
 s -lh *.
-tbl*
+gz
 %22)%0A%0Adef 
@@ -1253,39 +1253,25 @@
 tpch-kit
-/tpch/tpch_2_17_0/dbgen
+-datasets
 %22):%0A    
@@ -1284,20 +1284,18 @@
 n(%22rm *.
-tbl*
+gz
 %22)%0A%0Adef 

update fabfile