#!/usr/bin/env bash

set -e

package=$1
standalone=$2
compress=$3

name=$(./scripts/info $package name)
main=$(./scripts/info $package main)
version=$(./scripts/info $package version)
description=$(./scripts/info $package description)

if [ "$package" != "." ]; then
    package="./node_modules/$package"
fi 

mkdir -p dist

browserify -r "$package/$main:$name" \
           -o "dist/$name.bundle.js" \
           $([ -n "$standalone" ] && echo "-s $standalone")

uglifyjs dist/$name.bundle.js \
         -o dist/$name.min.js \
         -c $compress \
         -m \
         $([ -n "$standalone" ] && echo "--source-map dist/$name.min.js.map -r $standalone") \
         --preamble "/* $name $version: $description */"

if [ -z "$standalone" ]; then
    rm dist/$name.bundle.js
fi
