package com.heroku.sdk.deploy.lib.sourceblob;

import com.heroku.sdk.deploy.lib.OutputAdapter;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public final class SourceBlobPackager {

    public static Path pack(SourceBlobDescriptor sourceBlobDescriptor, OutputAdapter outputAdapter) throws IOException {
        Path tarFilePath = Files.createTempFile("heroku-deploy", "source-blob.tgz");

        TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
                new GzipCompressorOutputStream(new FileOutputStream(tarFilePath.toFile())));

        tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        outputAdapter.logInfo("-----> Packaging application...");
        for (Path sourceBlobPath : sourceBlobDescriptor.getContents().keySet()) {
            SourceBlobDescriptor.SourceBlobContent content = sourceBlobDescriptor.getContents().get(sourceBlobPath);

            if (content.isHidden()) {
                outputAdapter.logDebug("       - including: " + sourceBlobPath + " (hidden)");
            } else {
                outputAdapter.logInfo("       - including: " + sourceBlobPath);
            }

            addIncludedPathToArchive(sourceBlobPath.toString(), content, tarArchiveOutputStream);
        }

        // TODO: Info about filesize and location?

        tarArchiveOutputStream.close();
        return tarFilePath;
    }

    private static void addIncludedPathToArchive(String path, SourceBlobDescriptor.SourceBlobContent content, TarArchiveOutputStream tarArchiveOutputStream) throws IOException {
        // TODO: Symlink handling
        if (content.isLocalPath()) {
            TarArchiveEntry entry = new TarArchiveEntry(content.getLocalPath().toFile(), path);
            tarArchiveOutputStream.putArchiveEntry(entry);

            Files.copy(content.getLocalPath(), tarArchiveOutputStream);

            tarArchiveOutputStream.closeArchiveEntry();
        } else {
            TarArchiveEntry entry = new TarArchiveEntry(path);
            entry.setSize(content.getSyntheticFileContents().getBytes(StandardCharsets.UTF_8).length);

            tarArchiveOutputStream.putArchiveEntry(entry);

            OutputStreamWriter writer = new OutputStreamWriter(tarArchiveOutputStream, StandardCharsets.UTF_8);
            writer.write(content.getSyntheticFileContents());
            writer.flush();

            tarArchiveOutputStream.closeArchiveEntry();
        }
    }
}

@@ -1650,92 +1650,273 @@
     
-// TODO: Info about filesize and location?%0A%0A        tarArchiveOutputStream.close(
+tarArchiveOutputStream.close();%0A%0A        outputAdapter.logInfo(%22-----%3E Creating build...%22);%0A        outputAdapter.logInfo(%22       - file: %22 + tarFilePath);%0A        outputAdapter.logInfo(String.format(%22       - size: %25dMB%22, Files.size(tarFilePath) / 1024 / 1024)
 );%0A
+%0A
     

Add 'creating build' section