+// the recomendation is to check if the current commit is same as the one on any of the files/directories that need monitoring.
+
+register_shutdown_function ( 'unlock');
+lock();
+
+trigger_action('build_docs', array('Roo.js', 'Roo/', 'Array.js', 'Function.js', 'Date.js', 'String.js', 'Number.js'));
+
+$core = trigger_action('compile_core', explode("\n", file_get_contents(__DIR__). 'dependancy_core.txt'));
+$ui = trigger_action('compile_ui', explode("\n", file_get_contents(__DIR__). 'dependancy_ui.txt'));
+trigger_action('compile_bootstrap', explode("\n", file_get_contents(__DIR__). 'dependancy_bootstrap.txt'));
+trigger_action('compile_mailer', explode("\n", file_get_contents(__DIR__). 'dependancy_mailer.txt'));
+trigger_action('compile_calendar', explode("\n", file_get_contents(__DIR__). 'dependancy_calendar.txt'));
+
+trigger_action('compile_less', array('less'));
+trigger_action('compile_scss', array('scss'));
+
+if ($core || $ui) {
+ merge_files();
+}
+
+function lock()
+{
+ static $tries = 0;
+ clearstatcache();
+ if (file_exists(".git/pre-commit-running")) {
+ if (file_exists(".git/pre-commit-pending")) {
+ exit;
+ }
+ file_put_contents(".git/pre-commit-pending","\n");
+ sleep(30); // wait 30 seconds
+ unlink(".git/pre-commit-pending");
+ $tries++;
+ if ($tries > 3) {
+ exit;
+ }
+ }
+ file_put_contents(".git/pre-commit-running","\n");
+}
+function unlock()
+{
+ unlink(".git/pre-commit-running");
+}
+
+
+function changed_files()
+{
+ static $output = array();
+ if (!empty($output)) {
+ return $output;
+ }
+ $return = 0;
+ exec('git rev-parse --verify HEAD 2> /dev/null', $output, $return);
+ if ($return != 0) {
+ exit;
+ }
+
+ exec("git diff-index --cached --name-only 'HEAD'", $output);
+ return $output;
+}
+
+
+function trigger_action($fn, $files)
+{
+ $changed = changed_files();
+ foreach(changed_files() as $cg) {
+ foreach($files as $f) {
+ if ($f == $cg) {
+ $fn();
+ return true;
+ }
+ if (substr($f, -1,1) == '/' && substr($cg,0, strlen($f)) == $f) {
+ $fn();
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+function build_docs()
+{
+ exec("roojspacker -i buildSDK/dependancy_core.txt -i buildSDK/dependancy_ui.txt " .
+ "-i buildSDK/dependancy_bootstrap.txt -i buildSDK/dependancy_calendar.txt " .
+ "--doc-target=docs -D");