#!/bin/sh

source /etc/os-release

# unclutter: hide the mouse cursor
# xdotool: emulate keyboard key
# cec-utils: cec-client
sudo apt-get install -y unclutter xdotool

# screenshot

if [ "$BOARD_ID" = 'rpi' ]; then
  ##
  # Install raspi2png
  # Screenshot tool working with livestreamer and omxplayer
  sudo apt-get install libpng12-dev
  sudo wget https://github.com/AndrewFromMelbourne/raspi2png/raw/master/raspi2png -O /usr/local/bin/screenshot-screen
  sudo chmod +x /usr/local/bin/screenshot-screen
else
  sudo apt-get install -y scrot
fi

# cec

#if [ $ID = 'raspbian' ]; then
#  sudo apt-get install -y cmake liblockdev1-dev libudev-dev libxrandr-dev python-dev swig
#  ​
#  cd
#  git clone https://github.com/Pulse-Eight/platform.git
#  mkdir platform/build
#  cd platform/build
#  cmake ..
#  make
#  sudo make install
#  ​
#  cd
#  git clone https://github.com/Pulse-Eight/libcec.git
#  mkdir libcec/build
#  cd libcec/build
#  cmake -DRPI_INCLUDE_DIR=/opt/vc/include -DRPI_LIB_DIR=/opt/vc/lib ..
#  make -j4
#  sudo make install
#  sudo ldconfig
#  ​
#  cd
#  rm -rf libcec platform
#else
  sudo apt-get install -y cec-utils
#fi

# LXDE

sudo apt-get autoremove --purge -y light-locker
sudo apt-get install -y lxde-core lxappearance lightdm
sudo apt-get purge xscreensaver

BASEDIR=$(dirname "$0")

sudo mkdir -p "/home/$POOLIOT_USER/.config/openbox/" "/home/$POOLIOT_USER/.config/lxsession/LXDE/"

## lxsession/LXDE/autostart
if [ ! -f /home/$POOLIOT_USER/.config/lxsession/LXDE/autostart.backup ]; then
  sudo cp /home/$POOLIOT_USER/.config/lxsession/LXDE/autostart /home/$POOLIOT_USER/.config/lxsession/LXDE/autostart.backup
fi

echo '#@lxpanel --profile LXDE
#@pcmanfm --desktop --profile LXDE
pooliot-client display lxde-started' | sudo tee /home/$POOLIOT_USER/.config/lxsession/LXDE/autostart

## openbox/lxde-rc.xml

if [ ! -f /home/$POOLIOT_USER/.config/openbox/lxde-rc.xml.backup ]; then
  cp /home/$POOLIOT_USER/.config/openbox/lxde-rc.xml /home/$POOLIOT_USER/.config/openbox/lxde-rc.xml.backup || echo
fi

cat "$BASEDIR/lxde-rc.xml" | sudo tee "/home/$POOLIOT_USER/.config/openbox/lxde-rc.xml"

## chown
sudo chown -R $POOLIOT_USER:$POOLIOT_USER "/home/$POOLIOT_USER/.config/openbox/" "/home/$POOLIOT_USER/.config/lxsession/"
