#!/bin/sh
exec scala "$0" "$@"
!#

/*

rgb-alarm

Copyright (C) 2010 Giovanni Di Mingo

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Author: Giovanni Di Mingo
Email:  giovanni@dimingo.com

*/

import java.util.Date
import java.io.BufferedReader;
import java.io.InputStreamReader;

val path = "/usr/local/bin/maven-lamp/"

println("Starting Maven processing with Alarm Lamp at " + new Date)

val rt = Runtime.getRuntime()

val p0 = rt.exec(path + "open-serial.sh")
p0.waitFor()
Thread.sleep(3000)

val p1 = rt.exec(path + "set-blue.sh")
p1.waitFor()
Thread.sleep(3000)

val input = new BufferedReader(new InputStreamReader(System.in))

var str: String = ""

while (str != null) {

	str = input.readLine()
	
	if (str != null) {
	
		println(str)
		
		if (str.contains("BUILD SUCCESSFUL")) {
		
			println("======================> OK")
			val p2 = rt.exec(path + "set-green.sh")
			p2.waitFor()
			
		} else if (str.contains("BUILD FAILURE")) {
		
			println("======================> KO")
			val p3 = rt.exec(path + "set-red.sh")
			p3.waitFor()
			
		}
		
	} // end if
	
} // end while

val x = rt.exec(path + "close-serial.sh")
x.waitFor()
