#!/bin/sh
# This tools allow to wrap call a minifier and to format its output
# Usage :
#  ./minifier minifier_path output_file input_file_1 [input_file_2 ... input_file_n]
# Arguments
#  minifier path is the path to the minifier to use
#  output_file is the resulting file
#  input_file_* are the input files
#

# first argument is the minifier
minifier=$1
shift
# second argument is the output filename
output=$1
shift
# others arguments are the input filenames
inputs=$@
# the last input is the only one that should be taken
eval "last_input=\${$#}"
# the input filetype
input_ext=${inputs##*.}

echo -n "Will minify (${inputs}) into ${output} with " 

case $minifier in

  *yuicompressor*)

    echo "yuicompressor (${minifier})" 
    tmp_1="tmp_1.${input_ext}"
    cat ${inputs} > ${tmp_1}
    java -jar ${minifier} -o ${output} ${tmp_1}
    rm ${tmp_1}
    ;;

  *juicer*)

    echo "juicer (${minifier}) (last_input=$last_input)" 
    ${minifier} merge --skip-verification --force --output ${output} ${last_input}
    ;;

  *) 

    echo "sed" 
    cat ${inputs} | \
    sed \
    -e "s|/\*\(\\\\\)\?\*/|/~\1~/|g" \
    -e "s|/\*[^*]*\*\+\([^/][^*]*\*\+\)*/||g" \
    -e "s|\([^:/]\)//.*$|\1|" \
    -e "s|^//.*$||" \
    | \
    tr '\n' ' ' \
    | \
    sed \
    -e "s|/\*[^*]*\*\+\([^/][^*]*\*\+\)*/||g" \
    -e "s|/\~\(\\\\\)\?\~/|/*\1*/|g" \
    -e "s|\s\+| |g" \
    -e "s| \([{;:,]\)|\1|g" \
    -e "s|\([{;:,]\) |\1|g" \
    > ${output}
    ;;

esac

