#!/bin/bash

[ -z "${2}" ] && echo "${0} <output.js> <input0.js> ... <inputN.js> | setup closure" && exit 1

BASE_DIR="."

setup_closure() {
	wget -O \
		"closure-compiler-latest.tar.gz" \
		"https://dl.google.com/closure-compiler/compiler-latest.tar.gz"
	CLOSURE_COMPILER="$(tar -tf closure-compiler-latest.tar.gz | grep closure-compiler)"
	tar -C . -xf \
		"closure-compiler-latest.tar.gz" \
		"${CLOSURE_COMPILER}"
	mv \
		"${CLOSURE_COMPILER}" \
		closure-compiler.jar
}
if [ "${1}" = "setup" -a "${2}" = "closure" ] ; then
	echo setup_closure ;
	exit 0;
fi


minimize() { # minimize "${OUTPUT}" "${INPUT_0}" ... "${INPUT_N}"
	local OUTPUT="${1}" ; shift 1
	java -jar "${BASE_DIR}/closure-compiler.jar" \
		--compilation_level SIMPLE \
		--js_output_file "${OUTPUT}" \
		"${@}"
	# gzip -k9 "${OUTPUT}"
}
minimize "${@}"


exit 0
