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"
COPYRIGHT="2016 Arduino S.r.l. - Italy"
LICENSE="GNU GPL v2"
REVISION="4"
SOURCE_URI="https://github.com/arduino-cli/arduino-version/releases/download/$portVersion/arduino-$portVersion-linux32.zip"
CHECKSUM_SHA256="c80560935109121c4a8db6b34404bbdeb45e421584a895ca2277babc468f3b6a"
SOURCE_DIR="arduino-$portVersion-linux32"
SOURCE_URI_2="https://github.com/scream3r/java-simple-serial-connector/archive/f5564869f8a70503c29c0d24609245321ba39b2c.tar.gz"
CHECKSUM_SHA256_2="3d874fbf2afa60f6c21db7da6d89cc584f42e6ff01b63238bf235776699379e6"
SOURCE_DIR_2="java-simple-serial-connector-f5564869f8a70503c29c0d24609245321ba39b2c"
SOURCE_URI_3="https://github.com/arduino-org/Arduino/archive/4f0efcc55f1adb750fbf29b50fc4d96195f26ced.tar.gz"
CHECKSUM_SHA256_3="5346a8cff057c454ff0cdbb84919636639985621ca8a73438002a780fc461eed"
SOURCE_DIR_3="Arduino-4f0efcc55f1adb750fbf29b50fc4d96195f26ced/app/src/processing/app"
SOURCE_URI_4="https://github.com/arduino-org/Arduino/archive/4f0efcc55f1adb750fbf29b50fc4d96195f26ced.tar.gz"
CHECKSUM_SHA256_4="5346a8cff057c454ff0cdbb84919636639985621ca8a73438002a780fc461eed"
SOURCE_DIR_4="Arduino-4f0efcc55f1adb750fbf29b50fc4d96195f26ced/arduino-core/src/processing"
PATCHES="arduino-$portVersion.patchset"
PATCHES_2="jssc_2.8.0.patchset"
PATCHES_3="JFileChooser-$portVersion.patchset"
PATCHES_4="SettingsFolder-$portVersion.patchset"
ADDITIONAL_FILES="
	arduino.rdef.in
	arduino-bin.rdef.in
	"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"
DISABLE_SOURCE_PACKAGE="yes"

PROVIDES="
	arduino$secondaryArchSuffix = $portVersion
	app:Arduino$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	avr_binutils$secondaryArchSuffix
	avr_gcc$secondaryArchSuffix
	avr_libc$secondaryArchSuffix
	cmd:avrdude
	java:runtime == 8
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	openjdk8$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:jar >= 1.8
	cmd:java >= 1.8
	cmd:javac >= 1.8
	cmd:make
	"

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

	if [ "$effectiveTargetArchitecture" = "x86_gcc2" ]; then
		STDLIB="stdc++.r4"
	else
		STDLIB="stdc++"
	fi

	gcc -shared \
		-I/system/$relativeLibDir/openjdk8/include \
		-I/system/$relativeLibDir/openjdk8/include/haiku \
		$sourceDir2/src/cpp/_nix_based/jssc.cpp \
		-l$STDLIB \
		-o $sourceDir2/build/libs/haiku/libjSSC-2.8_x86_64.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 -target 7 -source 1.7 -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 -target 7 -source 1.7 -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 -target 7 -source 1.7 -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/hardware/arduino

	cp -r {examples,libraries,reference,lib,tools} $arduinoDir
	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=/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
	cat > $arduinoDir/arduino.sh << EOF
#!/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="/system/$relativeLibDir/openjdk8/jre/bin:\${PATH}"
export JAVA_HOME="/system/$relativeLibDir/openjdk8"
Arduino processing.app.Base "\$@"
EOF

	chmod +x $arduinoDir/arduino.sh

	#Copy binary launcher
	cp "haiku/Arduino IDE" $arduinoDir
	mkdir -p $libDir/openjdk8/jre/bin
	cp /system/$relativeLibDir/openjdk8/jre/bin/java $libDir/openjdk8/jre/bin/Arduino

	cp revisions.txt $arduinoDir

	local APP_SIGNATURE="application/x-vnd.arduino"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	local LONG_INFO="$SUMMARY"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/arduino.rdef.in > arduino.rdef
	addResourcesToBinaries arduino.rdef "$arduinoDir/Arduino IDE"

	local APP_SIGNATURE="application/x-vnd.arduino-bin"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/arduino-bin.rdef.in > arduino-bin.rdef
	addResourcesToBinaries arduino-bin.rdef $libDir/openjdk8/jre/bin/Arduino
	mimeset -f $libDir/openjdk8/jre/bin/Arduino

	addAppDeskbarSymlink "$arduinoDir/Arduino IDE"
}
