SUMMARY="IDE for the Arduino embedded hardware family"
DESCRIPTION="Arduino is an open-source tool used for building electronics projects. 
Arduino consists of a physical programmable circuit board (often referred to as a micro-controller) \
and a development environment, or IDE (Integrated Development Environment) that runs on your computer, \
used to write and upload computer code to the board."

HOMEPAGE="http://arduino.org"
LICENSE="GNU GPL v2"
COPYRIGHT="2015 Arduino S.r.l. - Italy"

SOURCE_URI="http://download.arduino.org/IDE/1.7.4/arduino-1.7.4.org-linux32.tar.xz"
CHECKSUM_SHA256="80611ec38c3176785717b39a454842b21d5d18664d8ba8051048a91f45f18762"
SOURCE_URI_2="git+https://github.com/scream3r/java-simple-serial-connector#f5564869f8a70503c29c0d24609245321ba39b2c"
SOURCE_URI_3="svn+https://github.com/arduino-org/Arduino/trunk/app/src/processing/app"
SOURCE_URI_4="svn+https://github.com/arduino-org/Arduino/trunk/arduino-core/src/processing"

REVISION="1"

ARCHITECTURES="x86 ?x86_64"
if [ $effectiveTargetArchitecture != x86_gcc2 ]; then
    ARCHITECTURES="$ARCHITECTURES x86_gcc2"
else
    ARCHITECTURES="$ARCHITECTURES !x86_gcc2"
fi
SECONDARY_ARCHITECTURES="x86"

PATCHES="arduino-1.7.4.patchset"
PATCHES_2="jssc_2.8.0.patchset"
PATCHES_3="JFileChooser-1.7.4.patchset"
PATCHES_4="SettingsFolder-1.7.4.patchset"

PROVIDES="
	arduino$secondaryArchSuffix = $portVersion
	app:Arduino$secondaryArchSuffix = $portVersion
	"

REQUIRES="
	haiku$secondaryArchSuffix
	openjdk$secondaryArchSuffix
	cmd:avrdude$secondaryArchSuffix
	avr_libc$secondaryArchSuffix
	avr_gcc$secondaryArchSuffix
	avr_binutils$secondaryArchSuffix
	"
BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	openjdk$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:gcc
	cmd:make
	cmd:java
	cmd:java_config
	cmd:javac
	cmd:jar
	"

SOURCE_DIR="arduino-﻿1.7.4-linux32"

DISABLE_SOURCE_PACKAGE=yes

BUILD()
{
	cd haiku
	make

	mkdir -p $sourceDir2/build/jssc

	javac -encoding ISO-8859-1 $sourceDir2/src/java/jssc/*.java
	cp $sourceDir2/src/java/jssc/*.class $sourceDir2/build/jssc

	mkdir -p $sourceDir2/build/libs/haiku
	cd $sourceDir2/build/libs/haiku

	gcc -shared \
		-I/boot/system/lib/openjdk/include \
		-I/boot/system/lib/x86/openjdk/include \
		-I/boot/system/lib/openjdk/include/haiku \
		-I/boot/system/lib/x86/openjdk/include/haiku \
		$sourceDir2/src/cpp/_nix_based/jssc.cpp \
		-o $sourceDir2/build/libs/haiku/libjSSC-2.8_x86.so

	cp -r $sourceDir2/src/java/libs/* $sourceDir2/build/libs

	cd $sourceDir2/build
	jar cf jssc-2.8.0.jar jssc libs

	mkdir -p $sourceDir2/build/processing/app
	cp -f $sourceDir2/src/java/jssc/SerialPortList.java $sourceDir2/build/processing/app
	sed -i 's|package jssc;|package processing.app;|1' \
		$sourceDir2/build/processing/app/SerialPortList.java
	sed -i '/import java.util.regex.Pattern;/a import jssc.SerialNativeInterface;' \
		$sourceDir2/build/processing/app/SerialPortList.java

	javac -encoding ISO-8859-1 -cp ./jssc-2.8.0.jar:$sourceDir/lib/arduino-core.jar \
		$sourceDir2/build/processing/app/SerialPortList.java
	rm $sourceDir2/build/processing/app/SerialPortList.java	

	cd $sourceDir
	javac -encoding ISO-8859-1 -cp java/lib/rt.jar:java/lib/tools.jar:./lib/pde.jar:./lib/arduino-core.jar \
		$sourceDir3/Base.java $sourceDir3/Sketch.java

	mkdir -p $sourceDir3/processing/app
	cp -f $sourceDir3/*.class $sourceDir3/processing/app

	javac -encoding ISO-8859-1 \
		-cp java/lib/rt.jar:java/lib/tools.jar:./lib/pde.jar:./lib/arduino-core.jar:./lib/jna.jar \
		$sourceDir4/app/Platform.java

	mkdir -p $sourceDir4/processing/app
	cp -f $sourceDir4/app/*.class $sourceDir4/processing/app
}

INSTALL()
{
	arduinoDir=$appsDir/Arduino
	mkdir -p $arduinoDir
	
	cp -r examples $arduinoDir
	cp -r libraries $arduinoDir
	cp -r reference $arduinoDir
	cp -r lib $arduinoDir
	cp -r tools $arduinoDir

	mkdir -p $arduinoDir/hardware/arduino
	cp -r hardware/arduino/avr $arduinoDir/hardware/arduino

	#Patch for AVR tools
	sed -i "s|^compiler.path=.*|compiler.path=$binDir/|g" \
		$arduinoDir/hardware/arduino/avr/platform.txt
	sed -i "s|^tools.avrdude.cmd.path=.*|tools.avrdude.cmd.path=$binDir/avrdude|g" \
		$arduinoDir/hardware/arduino/avr/platform.txt
	sed -i "s|^tools.avrdude.config.path=.*|tools.avrdude.config.path=/boot/system/settings/avrdude.conf|g" \
		$arduinoDir/hardware/arduino/avr/platform.txt
	#Patch for SAM tools
	#sed -i "s|^compiler.path=.*|compiler.path=$binDir/|g" \
	#	$arduinoDir/hardware/arduino/sam/platform.txt
	#Patch for SAMD tools
	#sed -i "s|^compiler.path=.*|compiler.path=$binDir/|g" \
	#	$arduinoDir/hardware/arduino/samd/platform.txt

	#Disable check for update
	sed -i 's|^update.check.*|update.check = false|g' $arduinoDir/lib/preferences.txt
	#Set default serial port
	sed -i 's|^serial.port=.*|serial.port=/dev/ports/usb0|g' $arduinoDir/lib/preferences.txt
	#Add command for opening
	echo "browser = /bin/open" >> $arduinoDir/lib/preferences.txt
	echo "launcher = /bin/open" >> $arduinoDir/lib/preferences.txt

	#Add fake libastylej.so library
	rm $arduinoDir/lib/libastylej.so
	ln -s $libDir/libroot.so $arduinoDir/lib/libastylej.so

	cp -f $sourceDir2/build/jssc-2.8.0.jar $arduinoDir/lib

	#Add patched SerialList class into arduino-core.jar
	cd $sourceDir2/build
	jar -uf $arduinoDir/lib/arduino-core.jar \
		processing/app/SerialPortList.class \
		processing/app/SerialPortList$1.class

	#Add patch for JFileChooser using
	cd $sourceDir3
	jar -uf $arduinoDir/lib/pde.jar \
		processing/app/*.class

	#Add patch for settings directory
	cd $sourceDir4
	jar -uf $arduinoDir/lib/arduino-core.jar \
		processing/app/*.class

	cd $sourceDir

	#Generate launcher script
	echo '#!/bin/sh
APPDIR="$(dirname -- "$(readlink -f -- "${0}")" )"
cd "$APPDIR"
for LIB in \
    java/lib/rt.jar \
    java/lib/tools.jar \
    lib/*.jar \
    ;
do
    CLASSPATH="${CLASSPATH}:${LIB}"
done
export CLASSPATH
LD_LIBRARY_PATH=`pwd`/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export LD_LIBRARY_PATH
export PATH="${APPDIR}/java/bin:${PATH}"
java processing.app.Base "$@"
' > $arduinoDir/arduino.sh

	chmod +x $arduinoDir/arduino.sh

	#Copy binary launcher
	cp "haiku/Arduino IDE" $arduinoDir	

	cp revisions.txt $arduinoDir

	addAppDeskbarSymlink "$arduinoDir/Arduino IDE"
}
