#!/bin/bash

# Note the latest openHAB sources can be downloaded here: 
# http://www.openhab.org/getting-started/downloads.html

# Navigate to install location
cd ~/openHAB

# Preparation of the image
sudo apt-get -y update 
sudo apt-get -y upgrade

# Install Java runtime
#If nothing helps - try apt search openjdk
#sudo apt-get install -y oracle-java7-jdk
#sudo apt-get -y install default-jre
#sudo apt-get install -y oracle-java8-jdk
sudo apt-get install -y openjdk-8-jdk
#Beginning with openjdk-9-jre/ fixes in openHAB are required.
#sudo apt-get remove openjdk*

# Install openHAB to /opt/openhab
sudo mkdir /opt/openhab
sudo chown pi /opt/openhab
cd /opt/openhab

# Get openHAB runtime
# see also https://www.openhab.org/download/ for newer versions
wget https://bintray.com/artifact/download/openhab/bin/distribution-1.8.3-runtime.zip
# Unzip runtime, remove zip file
unzip distribution*.zip
rm distribution*.zip

# Download addons as we need the exec-binding
cd addons
mkdir plugins
cd plugins
wget https://bintray.com/artifact/download/openhab/bin/distribution-1.8.3-addons.zip
unzip distribution*.zip
rm distribution*.zip
cp org.openhab.binding.exec*.jar ..
cd ../..

# Copy default configuration.
cp configurations/openhab_default.cfg configurations/openhab.cfg

# Now copy the Hubo demo files.
cp ~/openHAB/hubo.items   /opt/openhab/configurations/items/hubo.items
cp ~/openHAB/hubo.sitemap /opt/openhab/configurations/sitemaps/hubo.sitemap
cp ~/openHAB/ReadDS1820   /opt/openhab/configurations/scripts/ReadDS1820

# Rebuild Hubo library in case the wrong installer had been chosen before.
~/HuboDemo/./build

# Copying Hubo library to lib space.
sudo cp ~/HuboDemo/libhubo.so /usr/lib/libhubo.so.1
sudo chmod 755 /usr/lib/libhubo.so.1

# Create directory Automation to store executables.
sudo rm -r ~/HuboDemo/Automation/
mkdir ~/HuboDemo/Automation

# Make Hubo-Demos accessible with no sudo
sudo cp         ~/HuboDemo/GetAnalogInput/GetAnalogInput      ~/HuboDemo/Automation/GetAnalogInput
sudo chown root ~/HuboDemo/Automation/GetAnalogInput
sudo chmod 755  ~/HuboDemo/Automation/GetAnalogInput
sudo chmod u+s  ~/HuboDemo/Automation/GetAnalogInput

sudo cp         ~/HuboDemo/GetDigitalInput/GetDigitalInput    ~/HuboDemo/Automation/GetDigitalInput
sudo chown root ~/HuboDemo/Automation/GetDigitalInput
sudo chmod 755  ~/HuboDemo/Automation/GetDigitalInput
sudo chmod u+s  ~/HuboDemo/Automation/GetDigitalInput

sudo cp         ~/HuboDemo/SetDigitalOutput/SetDigitalOutput  ~/HuboDemo/Automation/SetDigitalOutput
sudo chown root ~/HuboDemo/Automation/SetDigitalOutput
sudo chmod 755  ~/HuboDemo/Automation/SetDigitalOutput
sudo chmod u+s  ~/HuboDemo/Automation/SetDigitalOutput

# RCSocket is supported for Raspberry Pi.
sudo cp         ~/HuboDemo/RCSocket/RCSocket                  ~/HuboDemo/Automation/RCSocket
sudo chown root ~/HuboDemo/Automation/RCSocket
sudo chmod 755  ~/HuboDemo/Automation/RCSocket
sudo chmod u+s  ~/HuboDemo/Automation/RCSocket

echo
echo To run type /opt/openhab/./start.sh 
echo 
echo ... wait loooong ...
echo
echo You can now access openHAB through your browser: http://raspberrypi:8080/openhab.app?sitemap=hubo# 
echo
