#!/usr/pkg/bin/bash

##
# Usage
##
usage()
{
	echo ""
	echo -e "  \033[1;33mMinify scripts using YUI compressor or UglifyJS (requires node.js)\033[0m"
	echo -e "  Usage: system/bin/minify <command> [<file>]"
	echo ""
	echo -e "  \033[1;33m<command>\033[0m"
	echo -e "  \033[0;36mall\033[0m           Execute all the functions"
	echo ""
	echo -e "  Contao"
	echo -e "  \033[0;36mcontao\033[0m        Minify the Contao assets (assets/contao)"
	echo -e "  \033[0;36mtheme\033[0m         Minify the default back end theme (system/themes/default)"
	echo ""
	echo -e "  jQuery"
	echo -e "  \033[0;36mcolorbox\033[0m      Minify the colorbox plugin (assets/jquery/colorbox)"
	echo -e "  \033[0;36mmediaelement\033[0m  Minify the mediaelement plugin (assets/jquery/mediaelement)"
	echo -e "  \033[0;36mtablesorter\033[0m   Minify the tablesorter plugin (assets/jquery/tablesorter)"
	echo ""
	echo -e "  MooTools"
	echo -e "  \033[0;36mmootools\033[0m      Minify the MooTools core (assets/mootools/core)"
	echo -e "  \033[0;36mchosen\033[0m        Minify the chosen plugin (assets/mootools/chosen)"
	echo -e "  \033[0;36mcolorpicker\033[0m   Minify the colorpicker plugin (assets/mootools/colorpicker)"
	echo -e "  \033[0;36mdatepicker\033[0m    Minify the datepicker plugin (assets/mootools/datepicker)"
	echo -e "  \033[0;36mmediabox\033[0m      Minify the mediabox plugin (assets/mootools/mediabox)"
	echo -e "  \033[0;36mmootao\033[0m        Minify the Contao MooTools enhancements (assets/mootools/mootao)"
	echo -e "  \033[0;36msimplemodal\033[0m   Minify the SimpleModal plugin (assets/mootools/simplemodal)"
	echo -e "  \033[0;36mslimbox\033[0m       Minify the slimbox plugin (assets/mootools/slimbox)"
	echo -e "  \033[0;36mstylect\033[0m       Minify the stylect plugin (assets/mootools/stylect)"
	echo -e "  \033[0;36mtablesort\033[0m     Minify the tablesort plugin (assets/mootools/tablesort)"
	echo ""
	echo -e "  Other"
	echo -e "  \033[0;36mhighlighter\033[0m   Minify the SyntaxHighlighter plugin (assets/highlighter)"
	echo -e "  \033[0;36mswipe\033[0m         Minify the swipe.js plugin (assets/swipe)"
	echo ""
}

##
# assets/contao
##
contao()
{
	# Single file only
	if [[ $1 != "" ]]; then
		jsgrep="$1"
		cssgrep="$1"
	else
		jsgrep=""
		cssgrep=""
	fi

	version=$folder/assets/contao
	copyright="/* Contao Open Source CMS, (c) 2005-2016 Leo Feyer, LGPL license */"

	# CSS
	for file in $(ls $version/css | grep "$cssgrep\-uncompressed"); do
		echo "$copyright" > $version/css/${file/-uncompressed/}
		$css $version/css/$file >> $version/css/${file/-uncompressed/}
		echo "  Created $version/css/${file/-uncompressed/}"
	done

	# JavaScript
	for file in `ls $version/js | grep "$jsgrep\-uncompressed"`; do
		echo "$copyright" > $version/js/${file/-uncompressed/}
		$js $version/js/$file >> $version/js/${file/-uncompressed/}
		echo "  Created $version/js/${file/-uncompressed/}"
	done
}

##
# system/themes/default
##
theme()
{
  theme=${1:-default}

	# Single file only
	if [[ $2 != "" ]]; then
		jsgrep="$2\.js"
		cssgrep="$2\.css"
	else
		jsgrep="\.js"
		cssgrep="\.css"
	fi

	version=$folder/system/themes/$theme
	copyright="/* Contao Open Source CMS, (c) 2005-2016 Leo Feyer, LGPL license */"

	# CSS
	for file in $(ls $version/src | grep "$cssgrep"); do
		echo "$copyright" > $version/$file
		$css $version/src/$file >> $version/$file
		echo "  Created $version/$file"
	done

	# JS
	for file in $(ls $version/src | grep "$jsgrep"); do
		echo "$copyright" > $version/$file
		$js $version/src/$file >> $version/$file
		echo "  Created $version/$file"
	done
}

##
# assets/jquery/colorbox
##
colorbox()
{
	for version in $(ls -d $folder/assets/jquery/colorbox/*); do
		copyright="/* ColorBox v$(basename $version), (c) 2011 Jack Moore - jacklmoore.com, MIT license */"

		# CSS
		echo "$copyright" > $version/css/colorbox.min.css
		$css $version/css/colorbox.css >> $version/css/colorbox.min.css
		echo "  Created $version/css/colorbox.min.css"

		# JavaScript
		echo "$copyright" > $version/js/colorbox.min.js
		$js $version/js/colorbox.js >> $version/js/colorbox.min.js
		echo "  Created $version/js/colorbox.min.js"
	done
}

##
# assets/jquery/mediaelement
##
mediaelement()
{
	for version in $(ls -d $folder/assets/jquery/mediaelement/*); do
		copyright="/* MediaElement.js $(basename $version), (c) 2010-2013 John Dyer <http://j.hn>, MIT license */"

		# CSS
		for file in $(ls $version/css | grep -v ".min"); do
			echo "$copyright" > $version/css/${file/.css/.min.css}
			$css $version/css/$file >> $version/css/${file/.css/.min.css}
			echo "  Created $version/css/${file/.css/.min.css}"
		done

		# JavaScript
		for file in $(ls $version/js | grep -v ".min"); do
			echo "$copyright" > $version/js/${file/.js/.min.js}
			$js $version/js/$file >> $version/js/${file/.js/.min.js}
			echo "  Created $version/js/${file/.js/.min.js}"
		done

		# Concat the mediaelement.js and mediaelementplayer.js files
		{ cat $version/js/mediaelement.js; echo ""; cat $version/js/mediaelementplayer.js; } > $version/js/mediaelement-and-player.js
		{ cat $version/js/mediaelement.min.js; echo ""; cat $version/js/mediaelementplayer.min.js; } > $version/js/mediaelement-and-player.min.js
	done
}

##
# assets/jquery/tablesorter
##
tablesorter()
{
	for version in $(ls -d $folder/assets/jquery/tablesorter/*); do

  	# CSS
  	copyright="/* Contao Open Source CMS, (c) 2005-2016 Leo Feyer, LGPL license */"
	  echo "$copyright" > $version/css/tablesorter.min.css
	  $css $version/css/tablesorter.css >> $version/css/tablesorter.min.css
	  echo "  Created $version/css/tablesorter.min.css"

	  # JS
	  copyright="/* TableSorter 2.0.5b, (c) 2007 Christian Bach, http://tablesorter.com, MIT license */"
	  echo "$copyright" > $version/js/tablesorter.min.js
	  $js $version/js/tablesorter.js >> $version/js/tablesorter.min.js
	  echo "  Created $version/js/tablesorter.min.js"
	done
}

##
# assets/mootools/core
##
mootools()
{
	# Minify
	for version in $(ls -d $folder/assets/mootools/core/*); do
		mobile="/* MooTools mobile, (c) Christoph Pojer (@cpojer), MIT-style license */"
		copyright="/* MooTools $(basename $version), <http://mootools.net>, MIT-style license */"
		for file in $(ls $version | grep "\-uncompressed"); do
			if [[ $file == "mootools-mobile-uncompressed.js" ]]; then
				echo "$mobile" > $version/${file/-uncompressed/}
			else
				echo "$copyright" > $version/${file/-uncompressed/}
			fi
			$js $version/$file >> $version/${file/-uncompressed/}
			echo "  Created $version/${file/-uncompressed/}"
		done
	done

	# Concat the mootools-core.js and mootools-more.js files
	{ cat $version/mootools-core-uncompressed.js; echo ""; cat $version/mootools-more-uncompressed.js; echo ""; cat $version/mootools-mobile-uncompressed.js; } > $version/mootools-uncompressed.js
	{ cat $version/mootools-core.js; echo ""; cat $version/mootools-more.js; echo ""; cat $version/mootools-mobile.js; } > $version/mootools.js
}

##
# assets/mootools/chosen
##
chosen()
{
	version=$folder/assets/mootools/chosen
	copyright="/* Chosen by Patrick Filler, Jules Janssen, Jonnathan Soares, MIT-style license */"

	# CSS
	echo "$copyright" > $version/chosen.css
	$css $version/chosen-uncompressed.css >> $version/chosen.css
	echo "  Created $version/chosen.css"

	# JavaScript
	echo "$copyright" > $version/chosen.js
	$js $version/chosen-uncompressed.js >> $version/chosen.js
	echo "  Created $version/chosen.js"
}

##
# assets/mootools/colorpicker
##
colorpicker()
{
	for version in $(ls -d $folder/assets/mootools/colorpicker/*); do
		copyright="/* MooRainbow $(basename $version) by Djamil Legato (w00fz) and Christopher Beloch, MIT-style license */"

		# CSS
		echo "$copyright" > $version/css/mooRainbow.css
		$css $version/css/mooRainbow-uncompressed.css >> $version/css/mooRainbow.css
		echo "  Created $version/css/mooRainbow.css"

		# JavaScript
		echo "$copyright" > $version/js/mooRainbow.js
		$js $version/js/mooRainbow-uncompressed.js >> $version/js/mooRainbow.js
		echo "  Created $version/js/mooRainbow.js"
	done
}

##
# assets/mootools/datepicker
##
datepicker()
{
	for version in $(ls -d $folder/assets/mootools/datepicker/*); do
		copyright="/* DatePicker $(basename $version) by Arian Stolwijk, <http://mootools.net/forge/p/mootools_datepicker>, MIT license */"

		# CSS
		echo "$copyright" > $version/datepicker.css
		$css $version/src/datepicker_bootstrap/datepicker_bootstrap.css >> $version/datepicker.css
		echo "  Created $version/datepicker.css"

		# JavaScript
		files=('Picker.js' 'Picker.Attach.js' 'Picker.Date.js')
		echo "$copyright" > $version/datepicker.js
		for file in "${files[@]}"; do
			$js $version/src/$file >> $version/datepicker.js
		done
		echo "  Created $version/datepicker.js"
	done
}

##
# assets/mootools/mediabox
##
mediabox()
{
	for version in $(ls -d $folder/assets/mootools/mediabox/*); do
		copyright="/* mediaboxAdvanced v$(basename $version), (c) 2007-2010 John Einselen <http://iaian7.com>, MIT-style license */"

		# CSS
		for file in $(ls $version/css | grep "\-uncompressed\.css"); do
			echo "$copyright" > $version/css/${file/-uncompressed/}
			$css $version/css/$file >> $version/css/${file/-uncompressed/}
			echo "  Created $version/css/${file/-uncompressed/}"
		done

		# JavaScript
		echo "$copyright" > $version/js/mediabox.js
		$js $version/js/mediabox-uncompressed.js >> $version/js/mediabox.js
		echo "  Created $version/js/mediabox.js"
	done
}

##
# assets/mootools/mootao
##
mootao()
{
	version=$folder/assets/mootools/mootao
	copyright="/* Contao Open Source CMS, (c) 2005-2016 Leo Feyer, LGPL license */"
	echo "$copyright" > $version/Mootao.js
	$js $version/Mootao-uncompressed.js >> $version/Mootao.js
	echo "  Created $version/Mootao.js"
}

##
# assets/mootools/simplemodal
##
simplemodal()
{
	for version in $(ls -d $folder/assets/mootools/simplemodal/*); do
		copyright="/* Simple Modal $(basename $version), (c) 2011 Marco Dell’Anna <http://www.plasm.it>, MIT-style license */"

		# CSS
		echo "$copyright" > $version/css/simplemodal.css
		$css $version/css/simplemodal-uncompressed.css >> $version/css/simplemodal.css
		echo "  Created $version/css/simplemodal.css"

		# JavaScript
		echo "$copyright" > $version/js/simplemodal.js
		$js $version/js/simplemodal-uncompressed.js >> $version/js/simplemodal.js
		echo "  Created $version/js/simplemodal.js"
	done
}

##
# assets/mootools/slimbox
##
slimbox()
{
	for version in $(ls -d $folder/assets/mootools/slimbox/*); do
		copyright="/* Slimbox v$(basename $version), (c) 2007-2009 Christophe Beyls <http://www.digitalia.be>, MIT-style license */"

		# CSS
		echo "$copyright" > $version/css/slimbox.css
		$css $version/css/slimbox-uncompressed.css >> $version/css/slimbox.css
		echo "  Created $version/css/slimbox.css"

		# JavaScript
		echo "$copyright" > $version/js/slimbox.js
		$js $version/js/slimbox-uncompressed.js >> $version/js/slimbox.js
		echo "  Created $version/js/slimbox.js"
	done
}

##
# assets/mootools/stylect
##
stylect()
{
	version=$folder/assets/mootools/stylect
	copyright="/* Contao Open Source CMS, (c) 2005-2016 Leo Feyer, LGPL license */"

	# CSS
	echo "$copyright" > $version/css/stylect.css
	$css $version/css/stylect-uncompressed.css >> $version/css/stylect.css
	echo "  Created $version/css/stylect.css"

	# JS
	echo "$copyright" > $version/js/stylect.js
	$js $version/js/stylect-uncompressed.js >> $version/js/stylect.js
	echo "  Created $version/js/stylect.js"
}

##
# assets/mootools/tablesort
##
tablesort()
{
	version=$folder/assets/mootools/tablesort
	copyright="/* Contao Open Source CMS, (c) 2005-2016 Leo Feyer, LGPL license */"

	# CSS
	echo "$copyright" > $version/css/tablesort.css
	$css $version/css/tablesort-uncompressed.css >> $version/css/tablesort.css
	echo "  Created $version/css/tablesort.css"

	# JS
	echo "$copyright" > $version/js/tablesort.js
	$js $version/js/tablesort-uncompressed.js >> $version/js/tablesort.js
	echo "  Created $version/js/tablesort.js"
}

##
# assets/highlighter
##
highlighter()
{
	for version in $(ls -d $folder/assets/highlighter/*); do
		xregexp="/* XRegExp 1.5.0, (c) 2007-2010 Steven Levithan, http://xregexp.com */"
		apacheconf="/* ApacheConf for SyntaxHighlighter, (c) 2010-2011 Sandro Bilbeisi */"
		copyright="/* SyntaxHighlighter $(basename $version), (c) 2004-2010 Alex Gorbatchev, http://alexgorbatchev.com/SyntaxHighlighter */"

		# CSS
		echo "$copyright" > $version/shCore.css
		$css $version/src/shCore.css >> $version/shCore.css
		echo "  Created $version/shCore.css"

		# JavaScript
		for file in $(ls $version/src | grep "\.js"); do
			if [[ $file == "XRegExp.js" ]]; then
				echo "$xregexp" > $version/$file
			elif [[ $file == "shBrushApacheConf.js" ]]; then
				echo "$apacheconf" > $version/$file
			else
				echo "$copyright" > $version/$file
			fi
			$js $version/src/$file >> $version/$file
			echo "  Created $version/$file"
		done
	done
}

##
# assets/swipe
##
swipe()
{
	for version in $(ls -d $folder/assets/swipe/*); do
		copyright="/* Swipe $(basename $version), (C) Brad Birdsall 2012, MIT license */"

		# CSS
		echo "$copyright" > $version/css/swipe.min.css
		$css $version/css/swipe.css >> $version/css/swipe.min.css
		echo "  Created $version/css/swipe.min.css"

		# JavaScript
		echo "$copyright" > $version/js/swipe.min.js
		$js $version/js/swipe.js >> $version/js/swipe.min.js
		echo "  Created $version/js/swipe.min.js"
	done
}

##
# Dispatch
##
if [[ $1 == "" ]]; then
	usage
else

	# Get the installation path
	folder=$(cd $(dirname "$0") && pwd)
	folder=${folder/\/system\/bin/}

	# Check whether java is available
	if [[ $(which java) == "" ]]; then
		echo -e "  \033[0;31mJava is required to run this script\033[0m"
		exit 1
	fi

	# Check whether uglifyjs is available
	if [[ $(which uglifyjs) != "" ]]; then
		js="uglifyjs -nc --max-line-len 0"
		echo -e "  \033[0;33mWill be using uglifyjs to minify .js files\033[0m"
	else
		js="java -jar $folder/system/bin/yuicompressor-2.4.8.jar --charset UTF-8"
		echo -e "  \033[0;33mWill be using yuicompressor to minify .js files\033[0m"
	fi

	# CSS minifier
	css="java -jar $folder/system/bin/yuicompressor-2.4.8.jar --charset UTF-8"

	# Execute all functions
	if [[ $1 == "all" ]]; then
		typeset -F | while read line; do
			func=${line##* }
			if [[ $func != "usage" ]]; then
				$func
			fi
		done
	else
		$1 $2 $3
	fi

	# Purge the assets/js folder
	if [[ -d $folder/assets/js ]]; then
		for i in $(ls $folder/assets/js); do
			if [[ $i != "index.html" ]]; then
				unlink $folder/assets/js/$i
			fi
		done
	fi

	# Purge the assets/css folder
	if [[ -d $folder/assets/css ]]; then
		for i in $(ls $folder/assets/css); do
			if [[ $i != "index.html" ]]; then
				unlink $folder/assets/css/$i
			fi
		done
	fi
fi
