class NarBundleUtil {

    public static BundleDetails fromNarDirectory(final File narDirectory) throws IOException, IllegalStateException {
        if (narDirectory == null) {
            throw new IllegalArgumentException("NAR Directory cannot be null");
        }

        final File manifestFile = new File(narDirectory, "META-INF/MANIFEST.MF");
        try (final FileInputStream fis = new FileInputStream(manifestFile)) {
            final Manifest manifest = new Manifest(fis);
            final Attributes attributes = manifest.getMainAttributes();

            final BundleDetails.Builder builder = new BundleDetails.Builder();
            builder.workingDir(narDirectory);

            final String group = attributes.getValue(NarManifestEntry.NAR_GROUP.getManifestName());
            final String id = attributes.getValue(NarManifestEntry.NAR_ID.getManifestName());
            final String version = attributes.getValue(NarManifestEntry.NAR_VERSION.getManifestName());
            builder.coordinate(new BundleCoordinate(group, id, version));

            final String dependencyGroup = attributes.getValue(NarManifestEntry.NAR_DEPENDENCY_GROUP.getManifestName());
            final String dependencyId = attributes.getValue(NarManifestEntry.NAR_DEPENDENCY_ID.getManifestName());
            final String dependencyVersion = attributes.getValue(NarManifestEntry.NAR_DEPENDENCY_VERSION.getManifestName());
            if (!StringUtils.isBlank(dependencyId)) {
                builder.dependencyCoordinate(new BundleCoordinate(dependencyGroup, dependencyId, dependencyVersion));
            }

            builder.buildBranch(attributes.getValue(NarManifestEntry.BUILD_BRANCH.getManifestName()));
            builder.buildTag(attributes.getValue(NarManifestEntry.BUILD_TAG.getManifestName()));
            builder.buildRevision(attributes.getValue(NarManifestEntry.BUILD_REVISION.getManifestName()));
            builder.buildTimestamp(attributes.getValue(NarManifestEntry.BUILD_TIMESTAMP.getManifestName()));
            builder.buildJdk(attributes.getValue(NarManifestEntry.BUILD_JDK.getManifestName()));
            builder.builtBy(attributes.getValue(NarManifestEntry.BUILT_BY.getManifestName()));

            return builder.build();
        }
    }

}