#!/usr/bin/evn python
# encoding: utf-8

from pprof.project import ProjectFactory, log_with, log
from pprof.settings import config
from group import PprofGroup

from os import path
from plumbum import FG, local
from plumbum.cmd import cat

class Crafty(PprofGroup):

    """ crafty benchmark """

    class Factory:
        def create(self, exp):
            obj = Crafty(exp, "crafty", "scientific")
            obj.calls_f = path.join(obj.builddir, "papi.calls.out")
            obj.prof_f = path.join(obj.builddir, "papi.profile.out")
            return obj
    ProjectFactory.addFactory("Crafty", Factory())

    src_dir = "crafty-23.4"
    src_file = src_dir + ".zip"
    src_uri = "http://www.craftychess.com/crafty-23.4.zip"

    def download(self):
        from pprof.utils.downloader import Wget
        from plumbum.cmd import wget, unzip

        book_bin = "http://www.craftychess.com/book.bin"
        with local.cwd(self.builddir):
            Wget(self.src_uri, self.src_file)
            wget(book_bin)
            unzip(path.join(self.builddir, self.src_file))

    def configure(self):
        pass

    def build(self):
        from plumbum.cmd import make, ln

        llvm = path.join(config["llvmdir"], "bin")
        llvm_libs = path.join(config["llvmdir"], "lib")
        clang = local[path.join(llvm, "clang")]
        crafty_dir = path.join(self.builddir, self.src_dir)
        with local.cwd(crafty_dir):
            target_cflags = self.cflags
            target_cxflags = [] 
            target_ldflags = self.ldflags
            target_opts = ["-DINLINE64", "-DCPUS=1"]

            make["target=LINUX",
                 "CC=" + str(clang),
                 "CFLAGS=" + " ".join(target_cflags),
                 "CXFLAGS=" + " ".join(target_cxflags),
                 "LDFLAGS=" + " ".join(target_ldflags),
                 "opt=" + " ".join(target_opts),
                 "crafty-make"] & FG

        with local.cwd(self.builddir):
            ln("-sf", path.join(crafty_dir, "crafty"), self.run_f)

    def run_tests(self, experiment):
        (cat[path.join(self.testdir, "test1.sh")] | experiment) & FG
        (cat[path.join(self.testdir, "test2.sh")] | experiment) & FG


@@ -841,18 +841,53 @@
 t, unzip
+, mv
 %0A%0A
+        book_file = %22book.bin%22%0A
         
@@ -925,25 +925,29 @@
 ess.com/
-book.bin%22
+%22 + book_file
 %0A       
@@ -1036,17 +1036,17 @@
         
-w
+W
 get(book
@@ -1049,17 +1049,30 @@
 book_bin
-)
+, %22book.bin%22)%0A
 %0A       
@@ -1082,39 +1082,55 @@
   unzip(
-path.join(self.builddir
+self.src_file)%0A            mv(book_file
 , self.s
@@ -1132,21 +1132,19 @@
 elf.src_
-file)
+dir
 )%0A%0A    d
@@ -1562,17 +1562,16 @@
 ags = %5B%5D
- 
 %0A       
@@ -1981,25 +1981,24 @@
 %22%5D & FG%0A
-%0A
         
 with loc
@@ -1993,60 +1993,20 @@
     
-with local.cwd(self.builddir):%0A            ln(%22-sf%22,
+self.run_f =
  pat
@@ -2037,21 +2037,8 @@
 ty%22)
-, self.run_f)
 %0A%0A  

change book download & avoid using symlink