package org.web3j.mavenplugin.solidity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;

/**
 * Wrapper class to the native solc execution on different platforms.
 *
 * Inspired by https://github.com/ethereum/ethereumj/tree/develop/ethereumj-core/src/main/java/org/ethereum/solidity
 */
public class SolC {

    private File solc = null;

    private String canonicalPath;
    private String canonicalWorkingDirectory;
    private File workingDirectory;

    SolC() {
        try {
            initBundled();

            canonicalPath = solc.getCanonicalPath();
            canonicalWorkingDirectory = solc.getParentFile().getCanonicalPath();
            workingDirectory = solc.getParentFile();

        } catch (IOException e) {
            throw new RuntimeException("Can't init solc compiler: ", e);
        }
    }

    private void initBundled() throws IOException {
        File tmpDir = new File(System.getProperty("java.io.tmpdir"), "solc");
        tmpDir.setReadable(true);
        tmpDir.setWritable(true);
        tmpDir.setExecutable(true);
        tmpDir.mkdirs();

        InputStream is = getClass().getResourceAsStream("/native/" + getOS() + "/solc/file.list");
        Scanner scanner = new Scanner(is);
        while (scanner.hasNext()) {
            String s = scanner.next();
            File targetFile = new File(tmpDir, s);
            InputStream fis = getClass().getResourceAsStream("/native/" + getOS() + "/solc/" + s);
            Files.copy(fis, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            if (getOS().equalsIgnoreCase("mac")) {
                Runtime.getRuntime().exec("chmod u+x " + targetFile.getAbsolutePath());
            }
            if (solc == null) {
                // first file in the list denotes executable
                solc = targetFile;
                solc.setExecutable(true);
            }
            targetFile.deleteOnExit();
        }
        tmpDir.deleteOnExit();
    }

    private String getOS() {
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("win")) {
            return "win";
        } else if (osName.contains("linux")) {
            return "linux";
        } else if (osName.contains("mac")) {
            return "mac";
        } else {
            throw new RuntimeException("Can't find solc compiler: unrecognized OS: " + osName);
        }
    }

    public String getCanonicalPath() {
        return canonicalPath;
    }

    public String getCanonicalWorkingDirectory() {
        return canonicalWorkingDirectory;
    }

    public File getWorkingDirectory() {
        return workingDirectory;
    }


}

@@ -1196,24 +1196,121 @@
 .mkdirs();%0A%0A
+        String solcPath = %22/native/%22 + getOS() + %22/solc/%22;%0A        System.out.println(solcPath);%0A
         Inpu
@@ -1357,38 +1357,20 @@
 eam(
-%22/native/%22 + getOS() + %22/solc/
+solcPath + %22
 file
@@ -1492,24 +1492,59 @@
 ner.next();%0A
+            System.out.println(s);%0A
             
@@ -1578,24 +1578,70 @@
 tmpDir, s);%0A
+            System.out.println(solcPath + s);%0A
             
@@ -1693,43 +1693,54 @@
 eam(
-%22/native/%22 + getOS() + %22/solc/%22 + s
+solcPath + s);%0A            System.out.println(
 );%0A 
@@ -1829,161 +1829,8 @@
 G);%0A
-            if (getOS().equalsIgnoreCase(%22mac%22)) %7B%0A                Runtime.getRuntime().exec(%22chmod u+x %22 + targetFile.getAbsolutePath());%0A            %7D%0A
     
@@ -2088,32 +2088,33 @@
 nExit();%0A    %7D%0A%0A
+%0A
     private Stri

debug infor for mac