/*
 * Copyright (c) 2016, Oracle and/or its affiliates.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
 * endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.oracle.truffle.llvm.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Linker {

    public static void main(String[] args) {
        try {
            String outputFileName = null;
            final Collection<String> bitcodeFileNames = new ArrayList<>();

            for (int n = 0; n < args.length; n++) {
                final String arg = args[n];

                switch (arg) {
                    case "-h":
                    case "-help":
                    case "--help":
                    case "/?":
                    case "/help":
                        help();
                        break;

                    case "-o":
                        if (n + 1 >= args.length) {
                            throw new Exception("-o needs to be followed by a file name");
                        }

                        outputFileName = args[n + 1];
                        n++;
                        break;

                    default:
                        bitcodeFileNames.add(arg);
                        break;
                }
            }

            if (outputFileName == null) {
                outputFileName = "out.su";
            }

            link(outputFileName, bitcodeFileNames);
        } catch (Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }

    }

    private static void help() {
        System.err.println("su-link [-o out.su] one.ll two.ll ...");
        System.err.println();
        System.err.println("  Links multiple LLVM bitcode files into a single file which can be loaded by Sulong.");
    }

    public static void link(String outputFileName, Collection<String> bitcodeFileNames) throws IOException {
        final byte[] buffer = new byte[1024];

        try (ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(outputFileName))) {
            for (String bitcodeFileName : bitcodeFileNames) {
                final File bitcodeFile = new File(bitcodeFileName);

                try (InputStream inputStream = new FileInputStream(bitcodeFile)) {
                    outputStream.putNextEntry(new ZipEntry(bitcodeFile.getName()));

                    while (true) {
                        int count = inputStream.read(buffer);

                        if (count == -1) {
                            break;
                        }

                        outputStream.write(buffer, 0, count);
                    }
                }
            }
        }
    }

}

@@ -1750,32 +1750,144 @@
 io.InputStream;%0A
+import java.math.BigInteger;%0Aimport java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0A
 import java.util
@@ -3654,19 +3654,45 @@
 xception
+, NoSuchAlgorithmException
  %7B%0A
-
         
@@ -3962,24 +3962,106 @@
 FileName);%0A%0A
+                final MessageDigest digest = MessageDigest.getInstance(%22SHA-1%22);%0A%0A
             
@@ -4155,68 +4155,636 @@
     
-outputStream.putNextEntry(new ZipEntry(bitcodeFile.getName()
+while (true) %7B%0A                        int count = inputStream.read(buffer);%0A%0A                        if (count == -1) %7B%0A                            break;%0A                        %7D%0A%0A                        digest.update(buffer, 0, count);%0A                    %7D%0A                %7D%0A%0A                final String digestString = new BigInteger(1, digest.digest()).toString(16);%0A                final String entryName = String.format(%22%25s_%25s%22, digestString, bitcodeFile.getName());%0A%0A                try (InputStream inputStream = new FileInputStream(bitcodeFile)) %7B%0A                    outputStream.putNextEntry(new ZipEntry(entryName
 ));%0A

Allow multiple files with the same base name in su-link